U
    ÒZjd)  ã                   @   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r²d 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                
       sb   e Zd ZdZddeeje eje eje ejejddœ‡ fdd„Zejejedœ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`.
    ú/Nr   )ÚappÚpathÚbase_urlÚ	subdomainÚ
url_schemeÚargsÚkwargsÚreturnc                    sê   |s|s|r(|d k	t |p|ƒks(tdƒ‚|d krÊ|j d¡p>d}|jd }	|r\|› d|› �}|d krn|jd }t|ƒ}
|
jp~|› d|
jpŠ|› d|	 d¡› �}|
j}|
j	rÊt
|
j	tƒr¸d	nd
}|||
j	 7 }|| _tƒ j||f|ž|Ž d S )Nz8Cannot pass "subdomain" or "url_scheme" with "base_url".ZSERVER_NAMEÚ	localhostZAPPLICATION_ROOTÚ.ZPREFERRED_URL_SCHEMEz://r   ó   ?ú?)ÚboolÚAssertionErrorÚ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__© úP/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/flask/testing.pyr)   -   s0    
ÿþü

$ÿzEnvironBuilder.__init__)Úobjr   r   c                 K   s   | j jj|f|ŽS )z³Serialize ``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*   r1   r   r/   r/   r0   Ú
json_dumpsU   s    zEnvironBuilder.json_dumps)r   NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚtÚOptionalÚAnyr)   r4   Ú__classcell__r/   r/   r-   r0   r      s        ú÷(r   c                       sÈ   e Zd ZU dZded< ejejddœ‡ fdd„Zeejejej	e
ddf dœdd	„ƒZd
d„ Zdd„ Zdddœejeeejddœ‡ fdd„Zd dœdd„Zeje eje eje ddœ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   ÚapplicationN)r   r   r   c                    s:   t ƒ j||Ž 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/   r0   r)   n   s    
þzFlaskClient.__init__c              
   o   sÜ   | j dkrtdƒ‚| j}| di ¡}| j  |¡ t d¡}|j||Ž�Œ}|j}| 	||j
¡}|dkrltdƒ‚t |¡}	z
|V  W 5 t |	¡ X | ¡ }
| |¡s®| |||
¡ |
 |j
j¡}| j  |j
j|¡ W 5 Q R X 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   rF   Zouter_reqctxÚcrI   ÚsessÚtokenÚrespÚheadersr/   r/   r0   Úsession_transactionx   s0    
ÿ
ÿ


zFlaskClient.session_transactionc                 C   s    | j |–}| jr| jj|d< |S )Nzwerkzeug.debug.preserve_context)rE   r@   rA   Úappend)r*   ÚotherÚoutr/   r/   r0   Ú_copy_environ¯   s    
zFlaskClient._copy_environc                 C   sD   |   | di ¡¡|d< t| jf|ž|Ž}z| ¡ W ¢S | ¡  X d S )NrE   )rX   r!   r   r?   ÚcloseÚget_request)r*   r   r   Úbuilderr/   r/   r0   Ú_request_from_builder_args·   s
    z&FlaskClient._request_from_builder_argsF©ÚbufferedÚfollow_redirectsr   )r   r^   r_   r   r   c          	         sö   |r¢t |d tjjttfƒr¢t |d tjjƒrVt|d ƒ}|  |jpFi ¡|_| 	¡ }q®t |d tƒr†tj
|d | j|  i ¡d� 	¡ }q®t|d ƒ}|  |j¡|_n|  ||¡}| j ¡  tƒ j|||d�}| jj|_| jrò| j ¡ }| j |¡ qÔ|S )Nr   )r   rE   r]   )r&   rC   Útestr   ÚdictÚBaseRequestr   rX   rE   rZ   Zfrom_environr?   rN   r\   rB   rY   r(   Úopenr2   Zjson_modulerA   ÚpopÚenter_context)	r*   r^   r_   r   r   r[   rJ   ÚresponseÚcmr-   r/   r0   rc   À   s8     ÿ
  ÿ
ý

zFlaskClient.open)r   c                 C   s   | j rtdƒ‚d| _ | S )NzCannot nest client invocationsT)r@   rG   ©r*   r/   r/   r0   Ú	__enter__í   s    zFlaskClient.__enter__)Úexc_typeÚ	exc_valueÚtbr   c                 C   s   d| _ | j ¡  d S )NF)r@   rB   rY   )r*   rj   rk   rl   r/   r/   r0   Ú__exit__ó   s    zFlaskClient.__exit__)r5   r6   r7   r8   Ú__annotations__r:   r<   r)   r   Ú	Generatorr   rT   rX   r\   r   rc   ri   r;   ÚtypeÚBaseExceptionr   rm   r=   r/   r/   r-   r0   r>   ^   s2   

 þ6üú-ûr>   c                       sN   e Zd ZdZdejddœ‡ fdd„Zd
ejejejejdœ‡ fdd	„Z‡  ZS )ÚFlaskCliRunnerz®A :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   N)r   r   r   c                    s   || _ tƒ jf |Ž d S ©N)r   r(   r)   )r*   r   r   r-   r/   r0   r)     s    zFlaskCliRunner.__init__)Úclir   r   r   c                    s@   |dkrˆ j j}d|kr.t‡ fdd„d�|d< tƒ j||f|Ž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.
        Nr1   c                      s   ˆ j S rs   )r   r/   rh   r/   r0   Ú<lambda>  ó    z'FlaskCliRunner.invoke.<locals>.<lambda>)Z
create_app)r   rt   r   r(   Úinvoke)r*   rt   r   r   r-   rh   r0   rw     s
    zFlaskCliRunner.invoke)NN)	r5   r6   r7   r8   r:   r<   r)   rw   r=   r/   r/   r-   r0   rr   ý   s      ÿ  þrr   ) Útypingr:   Ú
contextlibr   r   r   Útypesr   Zwerkzeug.testrC   Zclick.testingr   r   Zwerkzeug.urlsr   Zwerkzeug.wrappersr	   rb   rt   r   Úglobalsr   Úsessionsr   ÚTYPE_CHECKINGr   r   r   r`   r   r>   rr   r/   r/   r/   r0   Ú<module>   s&   G  