o
    ]Ocd)                     @   s   d dl Zd dlmZ d dlmZ d dlmZ d dlmZ d dlZ	d dl
mZ d dlmZ d dlmZ d d	lmZ d
dlmZ d
dlmZ d
dlmZ ejrYd dlmZ d
dlmZ G dd de	jjZG dd deZG dd deZdS )    N)contextmanager)	ExitStack)copy)TracebackType)	CliRunner)Client)	url_parse)Request   )
ScriptInfo)_cv_request)SessionMixin)TestResponse)Flaskc                       s|   e Zd ZdZ				ddddedeje deje d	eje d
ejdejddf fddZdejdejdefddZ	  Z
S )EnvironBuildera  An :class:`~werkzeug.test.EnvironBuilder`, that takes defaults from the
    application.

    :param app: The Flask application to configure the environment from.
    :param path: URL path being requested.
    :param base_url: Base URL where the app is being served, which
        ``path`` is relative to. If not given, built from
        :data:`PREFERRED_URL_SCHEME`, ``subdomain``,
        :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`.
    :param subdomain: Subdomain name to append to :data:`SERVER_NAME`.
    :param url_scheme: Scheme to use instead of
        :data:`PREFERRED_URL_SCHEME`.
    :param json: If given, this is serialized as JSON and passed as
        ``data``. Also defaults ``content_type`` to
        ``application/json``.
    :param args: other positional arguments passed to
        :class:`~werkzeug.test.EnvironBuilder`.
    :param kwargs: other keyword arguments passed to
        :class:`~werkzeug.test.EnvironBuilder`.
    /Nappr   pathbase_url	subdomain
url_schemeargskwargsreturnc                    s   |s|s|r|d ut |p|ksJ d|d u re|jdpd}|jd }	|r.| d| }|d u r7|jd }t|}
|
jp?| d|
jpE| d|	d }|
j}|
jret	|
jt
r\d	nd
}|||
j 7 }|| _t j||g|R i | d S )Nz8Cannot pass "subdomain" or "url_scheme" with "base_url".ZSERVER_NAME	localhostZAPPLICATION_ROOT.ZPREFERRED_URL_SCHEMEz://r      ??)boolconfiggetr   schemenetloclstripr   query
isinstancebytesr   super__init__)selfr   r   r   r   r   r   r   Z	http_hostZapp_rooturlsep	__class__ /D:\Flask\env\Lib\site-packages\flask/testing.pyr(   -   s4   


 zEnvironBuilder.__init__objc                 K   s   | j jj|fi |S )zSerialize ``obj`` to a JSON-formatted string.

        The serialization will be configured according to the config associated
        with this EnvironBuilder's ``app``.
        )r   jsondumps)r)   r0   r   r.   r.   r/   
json_dumpsU   s   zEnvironBuilder.json_dumps)r   NNN)__name__
__module____qualname____doc__strtOptionalAnyr(   r3   __classcell__r.   r.   r,   r/   r      s0    	"(r   c                       s   e Zd ZU dZded< dejdejddf fdd	Zedejdejdej	e
ddf fd
dZdd Zdd Zddddejdededejddf
 fddZdddZdeje deje deje ddfddZ  ZS )FlaskClienta  Works like a regular Werkzeug test client but has knowledge about
    Flask's contexts to defer the cleanup of the request context until
    the end of a ``with`` block. For general information about how to
    use this class refer to :class:`werkzeug.test.Client`.

    .. versionchanged:: 0.12
       `app.test_client()` includes preset default environment, which can be
       set after instantiation of the `app.test_client()` object in
       `client.environ_base`.

    Basic usage is outlined in the :doc:`/testing` chapter.
    r   applicationr   r   r   Nc                    s>   t  j|i | d| _g | _t | _ddtj d| _d S )NFz	127.0.0.1z	werkzeug/)ZREMOTE_ADDRZHTTP_USER_AGENT)	r'   r(   preserve_context_new_contextsr   _context_stackwerkzeug__version__environ_base)r)   r   r   r,   r.   r/   r(   n   s   
zFlaskClient.__init__c              
   o   s   | j du r
td| j}|di }| j | td}|j|i |Q}|j}|	||j
}|du r9tdt|}	z
|V  W t|	 nt|	 w | }
||s_||||
 |
|j
j}| j |j
j| W d   dS 1 szw   Y  dS )a  When used in combination with a ``with`` statement this opens a
        session transaction.  This can be used to modify the session that
        the test client uses.  Once the ``with`` block is left the session is
        stored back.

        ::

            with client.session_transaction() as session:
                session['value'] = 42

        Internally this is implemented by going through a temporary test
        request context and since session handling could depend on
        request variables this function accepts the same arguments as
        :meth:`~flask.Flask.test_request_context` which are directly
        passed through.
        Nz:Session transactions only make sense with cookies enabled.environ_overridesz?Session backend did not open a session. Check the configuration)Z
cookie_jarRuntimeErrorr>   
setdefaultZinject_wsgir   r    Ztest_request_contextsession_interfaceZopen_sessionrequestsetresetresponse_classZis_null_sessionZsave_sessionZget_wsgi_headersenvironZextract_wsgi)r)   r   r   r   rE   Zouter_reqctxcrH   sesstokenrespheadersr.   r.   r/   session_transactionx   s4   



"zFlaskClient.session_transactionc                 C   s$   i | j |}| jr| jj|d< |S )Nzwerkzeug.debug.preserve_context)rD   r?   r@   append)r)   otheroutr.   r.   r/   _copy_environ   s   zFlaskClient._copy_environc                 C   sL   |  |di |d< t| jg|R i |}z	| W |  S |  w )NrD   )rW   r    r   r>   get_requestclose)r)   r   r   builderr.   r.   r/   _request_from_builder_args   s
   z&FlaskClient._request_from_builder_argsFbufferedfollow_redirectsr]   r^   r   c          	         s   |rQt |d tjjttfrQt |d tjjr+t|d }| |jp#i |_|	 }n,t |d trCtj
|d | j| i d	 }nt|d }| |j|_n| ||}| j  t j|||d}| jj|_| jr{| j }| j| | jsm|S )Nr   )r   rD   r\   )r%   rB   testr   dictBaseRequestr   rW   rD   rX   Zfrom_environr>   rM   r[   rA   rY   r'   openr1   Zjson_moduler@   popenter_context)	r)   r]   r^   r   r   rZ   rI   responsecmr,   r.   r/   rb      s8   



zFlaskClient.openc                 C   s   | j rtdd| _ | S )NzCannot nest client invocationsT)r?   rF   r)   r.   r.   r/   	__enter__   s   zFlaskClient.__enter__exc_type	exc_valuetbc                 C   s   d| _ | j  d S )NF)r?   rA   rY   )r)   ri   rj   rk   r.   r.   r/   __exit__   s   zFlaskClient.__exit__)r   r=   )r4   r5   r6   r7   __annotations__r9   r;   r(   r   	Generatorr   rS   rW   r[   r   rb   rh   r:   typeBaseExceptionr   rl   r<   r.   r.   r,   r/   r=   ^   sJ   
 
6
-r=   c                	       sZ   e Zd ZdZdddejddf fddZ	dd	ejd
ejdejdejf fddZ  ZS )FlaskCliRunnerzA :class:`~click.testing.CliRunner` for testing a Flask app's
    CLI commands. Typically created using
    :meth:`~flask.Flask.test_cli_runner`. See :ref:`testing-cli`.
    r   r   r   r   Nc                    s   || _ t jdi | d S )Nr.   )r   r'   r(   )r)   r   r   r,   r.   r/   r(     s   zFlaskCliRunner.__init__clir   c                    sD   |du r j j}d|vrt fddd|d< t j||fi |S )ac  Invokes a CLI command in an isolated environment. See
        :meth:`CliRunner.invoke <click.testing.CliRunner.invoke>` for
        full method documentation. See :ref:`testing-cli` for examples.

        If the ``obj`` argument is not given, passes an instance of
        :class:`~flask.cli.ScriptInfo` that knows how to load the Flask
        app being tested.

        :param cli: Command object to invoke. Default is the app's
            :attr:`~flask.app.Flask.cli` group.
        :param args: List of strings to invoke the command with.

        :return: a :class:`~click.testing.Result` object.
        Nr0   c                      s    j S )N)r   r.   rg   r.   r/   <lambda>  s    z'FlaskCliRunner.invoke.<locals>.<lambda>)Z
create_app)r   rr   r   r'   invoke)r)   rr   r   r   r,   rg   r/   rt     s
   zFlaskCliRunner.invoke)NN)	r4   r5   r6   r7   r9   r;   r(   rt   r<   r.   r.   r,   r/   rq      s    rq   ) typingr9   
contextlibr   r   r   typesr   Zwerkzeug.testrB   Zclick.testingr   r   Zwerkzeug.urlsr   Zwerkzeug.wrappersr	   ra   rr   r   globalsr   sessionsr   TYPE_CHECKINGr   r   r   r_   r   r=   rq   r.   r.   r.   r/   <module>   s(    G  