o
    àý°j…'  ã                   @  s  d dl mZ d dl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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rid dlmZ d dlmZ ddlm Z  G dd„ dej!j"ƒZ"da#ddd„Z$G dd„ deƒZ%G dd„ deƒZ&dS )é    )ÚannotationsN)Úcontextmanager)Ú	ExitStack)Úcopy)ÚTracebackType)Úurlsplit)Ú	CliRunner)ÚResult)ÚClient)ÚRequesté   )Ú
ScriptInfo)ÚSessionMixin)ÚWSGIEnvironment)ÚTestResponse)ÚFlaskc                      s6   e Zd ZdZ				dd‡ fdd„Z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`.
    ú/NÚappr   ÚpathÚstrÚbase_urlú
str | NoneÚ	subdomainÚ
url_schemeÚargsút.AnyÚkwargsÚreturnÚNonec                   sÞ   |s|s|r|d ut |p|ƒksJ dƒ‚|d u r\|j d¡pd}|jd }	|r.|› d|› �}|d u r7|jd }t|ƒ}
|
jp?|› d|
jpE|› d|	 d¡› �}|
j}|
jr\|› d	|
j› �}|| _	t
ƒ j||g|¢R i |¤Ž d S )
Nz8Cannot pass "subdomain" or "url_scheme" with "base_url".ÚSERVER_NAMEÚ	localhostÚAPPLICATION_ROOTÚ.ÚPREFERRED_URL_SCHEMEz://r   ú?)ÚboolÚconfigÚgetr   ÚschemeÚnetlocÚlstripr   Úqueryr   ÚsuperÚ__init__)Úselfr   r   r   r   r   r   r   Ú	http_hostÚapp_rootÚurl©Ú	__class__© ú€/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/flask/testing.pyr.   1   s.   

þý

ÿÿ zEnvironBuilder.__init__Úobjc                 K  s   | j jj|fi |¤Ž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/   r7   r   r5   r5   r6   Ú
json_dumpsX   s   zEnvironBuilder.json_dumps)r   NNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   r:   Ú__classcell__r5   r5   r3   r6   r      s    ú'r   Ú r   r   c                   C  s   t stj d¡a t S )NÚwerkzeug)Ú_werkzeug_versionÚ	importlibÚmetadataÚversionr5   r5   r5   r6   Ú_get_werkzeug_versiond   s   rF   c                      sx   e Zd ZU dZded< d)‡ fd	d
„Zed*dd„ƒZd+dd„Zd,dd„Z	dddœd-‡ fdd„Z
d.dd „Zd/d'd(„Z‡  ZS )0Ú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   r   r   c                   s>   t ƒ j|i |¤Ž d| _g | _tƒ | _ddtƒ › �dœ| _d S )NFz	127.0.0.1z	Werkzeug/)ÚREMOTE_ADDRÚHTTP_USER_AGENT)r-   r.   Úpreserve_contextÚ_new_contextsr   Ú_context_stackrF   Úenviron_base)r/   r   r   r3   r5   r6   r.   }   s   
þzFlaskClient.__init__út.Iterator[SessionMixin]c                 o  sú   � | j du r
tdƒ‚| j}|j|i |¤Ž}|  |jj¡ |� |j ||j¡}W d  ƒ n1 s1w   Y  |du r>t	dƒ‚|V  | 
¡ }|j |¡rMdS |� |j |||¡ W d  ƒ n1 sbw   Y  |  |jj d¡d |jj|j d¡¡ 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>Cookies are disabled. Create a client with 'use_cookies=True'.z'Session backend did not open a session.ú:r   z
Set-Cookie)Ú_cookiesÚ	TypeErrorrH   Útest_request_contextÚ_add_cookies_to_wsgiÚrequestÚenvironÚsession_interfaceÚopen_sessionÚRuntimeErrorÚresponse_classÚis_null_sessionÚsave_sessionÚ_update_cookies_from_responseÚhostÚ	partitionr   ÚheadersÚgetlist)r/   r   r   r   ÚctxÚsessÚrespr5   r5   r6   Úsession_transaction‡   s2   €
ÿÿÿ
ýzFlaskClient.session_transactionÚotherr   c                 C  s$   i | j ¥|¥}| jr| jj|d< |S )Nzwerkzeug.debug.preserve_context)rN   rK   rL   Úappend)r/   rf   Úoutr5   r5   r6   Ú_copy_environ¹   s   zFlaskClient._copy_environútuple[t.Any, ...]údict[str, t.Any]ÚBaseRequestc                 C  sL   |   | di ¡¡|d< t| jg|¢R i |¤Ž}z	| ¡ W | ¡  S | ¡  w )NrN   )ri   r(   r   rH   Úget_requestÚclose)r/   r   r   Úbuilderr5   r5   r6   Ú_request_from_builder_argsÁ   s
   z&FlaskClient._request_from_builder_argsF©ÚbufferedÚfollow_redirectsrr   r&   rs   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D ]}| j |¡ qm| j ¡  |S )Nr   )r   rN   rq   )Ú
isinstancerA   Útestr   Údictrl   r   ri   rN   rm   Úfrom_environrH   rV   rp   rM   rn   r-   Úopenr8   Újson_modulerL   Úenter_contextÚclear)	r/   rr   rs   r   r   ro   rU   ÚresponseÚcmr3   r5   r6   rx   Ì   s6   ÿ
ÿþ
ý


zFlaskClient.openc                 C  s   | j rtdƒ‚d| _ | S )NzCannot nest client invocationsT)rK   rY   ©r/   r5   r5   r6   Ú	__enter__ù   s   zFlaskClient.__enter__Úexc_typeútype | NoneÚ	exc_valueúBaseException | NoneÚtbúTracebackType | Nonec                 C  s   d| _ | j ¡  d S )NF)rK   rM   rn   )r/   r€   r‚   r„   r5   r5   r6   Ú__exit__ÿ   s   zFlaskClient.__exit__)r   r   r   r   r   r   )r   r   r   r   r   rO   )rf   r   r   r   )r   rj   r   rk   r   rl   )
r   r   rr   r&   rs   r&   r   r   r   r   )r   rG   )r€   r�   r‚   rƒ   r„   r…   r   r   )r;   r<   r=   r>   Ú__annotations__r.   r   re   ri   rp   rx   r   r†   r?   r5   r5   r3   r6   rG   m   s   
 

1
ü
-rG   c                      s4   e Zd ZdZd‡ fdd	„Z	
d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   r   r   r   r   r   c                   s   || _ tƒ jdi |¤Ž d S )Nr5   )r   r-   r.   )r/   r   r   r3   r5   r6   r.     s   zFlaskCliRunner.__init__NÚclir   r	   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.
        Nr7   c                     s   ˆ j S )N)r   r5   r~   r5   r6   Ú<lambda>(  s    z'FlaskCliRunner.invoke.<locals>.<lambda>)Ú
create_app)r   r‰   r   r-   Úinvoke)r/   r‰   r   r   r3   r~   r6   rŒ     s
   zFlaskCliRunner.invoke)r   r   r   r   r   r   )NN)r‰   r   r   r   r   r   r   r	   )r;   r<   r=   r>   r.   rŒ   r?   r5   r5   r3   r6   rˆ   	  s
    ÿrˆ   )r   r   )'Ú
__future__r   Úimportlib.metadatarC   ÚtypingÚtÚ
contextlibr   r   r   Útypesr   Úurllib.parser   Úwerkzeug.testrA   Úclick.testingr   r	   r
   Úwerkzeug.wrappersr   rl   r‰   r   Úsessionsr   ÚTYPE_CHECKINGÚ_typeshed.wsgir   r   r   r   ru   r   rB   rF   rG   rˆ   r5   r5   r5   r6   Ú<module>   s2    F
	 