
    6je)                        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 ej        rd dl
mZ d
dlmZ  G d dej        j                  Z G d de          Z G d de          ZdS )    N)contextmanager)	ExitStack)copy)TracebackType)urlsplit)	CliRunner)Client)Request   )
ScriptInfo)SessionMixin)TestResponse)Flaskc                        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Zdej        dej        defdZ	 x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|r!|d ut          |p|          k    s
J d            ||j                            d          pd}|j        d         }	|r| d| }||j        d         }t          |          }
|
j        p| d|
j        p| d|	                    d           }|
j        }|
j        r+t          |
j        t                    rd	nd
}|||
j        z   z  }|| _         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   query
isinstancebytesr   super__init__)selfr   r   r   r   r   r   r   	http_hostapp_rooturlsep	__class__s               -lib/python3.11/site-packages/flask/testing.pyr-   zEnvironBuilder.__init__,   s}     	F	 	FZ 	FD #
 
=
 =
 =
 F	=
 =
 =
 
}55DIz"45H 7(66966	! Z(>?
4..C:+ + +
0Gi + +OOC((+ +  8Dy ((E::Cddci'x9$999&99999    objc                 2     | 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.   r6   r   s      r4   
json_dumpszEnvironBuilder.json_dumpsT   s#     #tx}"311&111r5   )r   NNN)__name__
__module____qualname____doc__strtOptionalAnyr-   r:   __classcell__r3   s   @r4   r   r      s         0 $(%)&*&: &:&: &: *S/	&:
 :c?&: JsO&: u&: %&: 
&: &: &: &: &: &:P2ae 2qu 2 2 2 2 2 2 2 2 2r5   r   c                   N    e Zd ZU dZded<   dej        dej        ddf fdZedej        dej        dej	        e
ddf         fd	            Zd
 Zd Zddddej        dededej        ddf
 fdZddZdej        e         dej        e         dej        e         ddfdZ x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                      t                      j        |i | d| _        g | _        t	                      | _        ddt          j         d| _        d S )NFz	127.0.0.1z	werkzeug/)REMOTE_ADDRHTTP_USER_AGENT)	r,   r-   preserve_context_new_contextsr   _context_stackwerkzeug__version__environ_base)r.   r   r   r3   s      r4   r-   zFlaskClient.__init__m   sa    $)&))) %>@'kk&A8+?AA
 
r5   c              /     K   t          | d          r| j        n| j        }|t          d          | j        } |j        |i |}t          | d          r |                     |j        j                   n$| j        	                    |j        j                   |5  |j
                            ||j                  }ddd           n# 1 swxY w Y   |t          d          |V  |                                }|j
                            |          rdS |5  |j
                            |||           ddd           n# 1 swxY w Y   t          | d          r	 |                     |j        j                            d          d         |j        j        |j                            d	                     dS # t          $ rU |                     |j        j                            d          d         |j                            d	                     Y dS w xY w| j                            |j        j        |j                   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.
        _cookiesNz>Cookies are disabled. Create a client with 'use_cookies=True'._add_cookies_to_wsgiz'Session backend did not open a session._update_cookies_from_response:r   z
Set-Cookie)hasattrrR   
cookie_jar	TypeErrorrG   test_request_contextrS   requestenvironinject_wsgisession_interfaceopen_sessionRuntimeErrorresponse_classis_null_sessionsave_sessionrT   host	partitionr   headersgetlistextract_wsgi)r.   r   r   cookie_storager   ctxsessresps           r4   session_transactionzFlaskClient.session_transactionw   s     * +2$
*C*CX!P   &c&7774/00 	=%%ck&9::::O''(;<<< 	H 	H(55c3;GGD	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H <HIII


!!## 0066 	F 	@ 	@!..sD$???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 4899 	22K$..s33A6K$L((66    
    22K$..s33A6L((66      O((#T\    s8   !C		CCEEE AF> >AHHc                 H    i | j         |}| j        r| j        j        |d<   |S )Nzwerkzeug.debug.preserve_context)rP   rK   rL   append)r.   otherouts      r4   _copy_environzFlaskClient._copy_environ   s6    ,",e,  	O595G5NC12
r5   c                    |                      |                    di                     |d<   t          | j        g|R i |}	 |                                |                                 S # |                                 w xY w)NrP   )rq   r%   r   rG   get_requestclose)r.   r   r   builders       r4   _request_from_builder_argsz&FlaskClient._request_from_builder_args   s}    !%!3!3FJJ~r4R4R!S!S~ !1CDCCCFCC	&&((MMOOOOGMMOOOOs   A+ +BFbufferedfollow_redirectsrx   ry   r   c                   |rAt          |d         t          j        j        t          t
          f          rt          |d         t          j        j                  rKt          |d                   }|                     |j        pi           |_        |	                                }nt          |d         t                    rNt          
                    |d         | j        |                     i                     	                                }nKt          |d                   }|                     |j                  |_        n|                     ||          }| j                                         t!                                          |||          }| j        j        |_        | j        r:| j                                        }| j                            |           | j        :|S )Nr   )r   rP   rw   )r*   rN   testr   dictBaseRequestr   rq   rP   rs   from_environrG   r[   rv   rM   rt   r,   openr8   json_modulerL   popenter_context)
r.   rx   ry   r   r   ru   rZ   responsecmr3   s
            r4   r   zFlaskClient.open   s     	DJGhm2D+F
 
 	D $q'8=#?@@ FtAw--'+'9'9':N:TRT'U'U$!--//DGT** F(55G!1@R@RSU@V@V 6  +-- 
 tAw--"&"4"4W_"E"E 55dFCCG
 	!!###77<<-   
 

  $/4   	2#''))B--b111   	2 r5   c                 @    | j         rt          d          d| _         | S )NzCannot nest client invocationsT)rK   r_   r.   s    r4   	__enter__zFlaskClient.__enter__   s*      	A?@@@ $r5   exc_type	exc_valuetbc                 F    d| _         | j                                         d S )NF)rK   rM   rt   )r.   r   r   r   s       r4   __exit__zFlaskClient.__exit__  s'     !&!!#####r5   )r   rF   )r;   r<   r=   r>   __annotations__r@   rB   r-   r   	Generatorr   rl   rq   rv   r#   r   r   rA   typeBaseExceptionr   r   rC   rD   s   @r4   rF   rF   ]   s          
ae 
qu 
 
 
 
 
 
 
 DUD&'eD	
\4-	.D D D ^DL     !&	+ + +u+ + 	+
 %+ 
+ + + + + +Z   $*T"$ :m,$ J}%	$
 
$ $ $ $ $ $ $ $r5   rF   c            	            e Zd ZdZdddej        ddf fdZ	 ddej        d	ej        dej        dej        f fd
Z x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                 H    || _          t                      j        di | d S )N )r   r,   r-   )r.   r   r   r3   s      r4   r-   zFlaskCliRunner.__init__  s-    ""6"""""r5   clir   c                      | j         j        }d|vrt           f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.
        Nr6   c                       j         S )N)r   r   s   r4   <lambda>z'FlaskCliRunner.invoke.<locals>.<lambda>+  s	    $( r5   )
create_app)r   r   r   r,   invoke)r.   r   r   r   r3   s   `   r4   r   zFlaskCliRunner.invoke  s]    " ;(,C&2B2B2B2BCCCF5Muww~c4226222r5   )NN)	r;   r<   r=   r>   r@   rB   r-   r   rC   rD   s   @r4   r   r     s         
#G #qu # # # # # # #
 043 353'(u3?@u3	
3 3 3 3 3 3 3 3 3 3r5   r   )typingr@   
contextlibr   r   r   typesr   urllib.parser   werkzeug.testrN   click.testingr   r	   werkzeug.wrappersr
   r}   r   r   sessionsr   TYPE_CHECKINGr   r   r   r{   r   rF   r   r   r5   r4   <module>r      s       % % % % % %                         ! ! ! ! ! !     # # # # # #             4 4 4 4 4 4       " " " " " "? ******D2 D2 D2 D2 D2X]1 D2 D2 D2Nl$ l$ l$ l$ l$& l$ l$ l$^!3 !3 !3 !3 !3Y !3 !3 !3 !3 !3r5   