
    +h!'                    F   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 ej4                  rd dlmZ ddlmZ  G d dej<                  j>                        Zda ddZ! G d de      Z" G d de      Z#y)    )annotationsN)contextmanager)	ExitStack)copy)TracebackType)urlsplit)	CliRunner)Client)Request   )
ScriptInfo)SessionMixin)TestResponse)Flaskc                  T     e Zd ZdZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZd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`.
    c                "   |s|s|r|d ut        |xs |      k7  sJ d       ||j                  j                  d      xs d}|j                  d   }	|r| d| }||j                  d   }t        |      }
|
j                  xs | d|
j
                  xs | d|	j                  d       }|
j                  }|
j                  r0t        |
j                  t              rd	nd
}|||
j                  z   z  }|| _        t        | 4  ||g|i | y )Nz8Cannot pass "subdomain" or "url_scheme" with "base_url".SERVER_NAME	localhostAPPLICATION_ROOT.PREFERRED_URL_SCHEMEz:///   ??)boolconfiggetr   schemenetloclstrippathquery
isinstancebytesappsuper__init__)selfr&   r"   base_url	subdomain
url_schemeargskwargs	http_hostapp_rooturlsep	__class__s               @/var/www/html/venv/lib/python3.12/site-packages/flask/testing.pyr(   zEnvironBuilder.__init__/   s1    	ZD #
=
 	F F		F 
 

}5DIzz"45H(k9+6	! ZZ(>?
4.C::+,C

0Gi/HHOOC()+  88Dyy(E:dcii'x9$9&9    c                P     | 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)   objr.   s      r4   
json_dumpszEnvironBuilder.json_dumpsW   s$     #txx}}""31&11r5   )r   NNN)r&   r   r"   strr*   
str | Noner+   r<   r,   r<   r-   t.Anyr.   r=   returnNone)r9   r=   r.   r=   r>   r;   )__name__
__module____qualname____doc__r(   r:   __classcell__r3   s   @r4   r   r      so    0 # $!%&:&: &: 	&:
 &: &: &: &: 
&:P2r5   r    c                 X    t         st        j                  j                  d      a t         S )Nwerkzeug)_werkzeug_version	importlibmetadataversion r5   r4   _get_werkzeug_versionrN   c   s$     %..66zBr5   c                       e Zd ZU dZded<   d fdZe	 	 	 	 	 	 dd       Zd Zd Z	ddd		 	 	 	 	 	 	 	 	 d fd
Z
ddZ	 	 	 	 	 	 	 	 d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   applicationc                    t        |   |i | d| _        g | _        t	               | _        ddt                d| _        y )NFz	127.0.0.1z	Werkzeug/)REMOTE_ADDRHTTP_USER_AGENT)r'   r(   preserve_context_new_contextsr   _context_stackrN   environ_base)r)   r-   r.   r3   s      r4   r(   zFlaskClient.__init__|   sK    $)&) %<>'k&!*+@+B*CD
r5   c              /    K   | j                   t        d      | j                  } |j                  |i |}| j	                  |j
                  j                         |5  |j                  j                  ||j
                        }ddd       t        d      | |j                         }|j                  j                  |      ry|5  |j                  j                  |||       ddd       | j                  |j
                  j                  j                  d      d   |j
                  j                   |j"                  j%                  d             y# 1 sw Y   xY w# 1 sw Y   |xY ww)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	TypeErrorrQ   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resps          r4   session_transactionzFlaskClient.session_transaction   sR    ( == P  &c&&77!!#++"5"56 	H((55c3;;GD	H <HII
!!#  006 	@!!..sD$?	@ 	**KK&&s+A.KKLL  .	
	H 	H	@ 	@s8   AE2'EAE2E&+A/E2E#E2&E/+E2c                p    i | j                   |}| j                  r| j                  j                  |d<   |S )Nzwerkzeug.debug.preserve_context)rX   rU   rV   append)r)   otherouts      r4   _copy_environzFlaskClient._copy_environ   s<    ,"",e,  595G5G5N5NC12
r5   c                    | j                  |j                  di             |d<   t        | j                  g|i |}	 |j	                         |j                          S # |j                          w xY w)NrX   )rt   r   r   rQ   get_requestclose)r)   r-   r.   builders       r4   _request_from_builder_argsz&FlaskClient._request_from_builder_args   s`    !%!3!3FJJ~r4R!S~ !1!1CDCFC	&&(MMOGMMOs   A A1Fbufferedfollow_redirectsc                  |r!t        |d   t        j                  j                  t        t
        f      rt        |d   t        j                  j                        rCt        |d         }| j                  |j                  xs i       |_        |j                         }nt        |d   t              rCt        j                  |d   | j                  | j                  i             j                         }nAt        |d         }| j                  |j                        |_        n| j                  ||      }| j                  j                          t         	| E  |||      }| j                  j$                  |_        | j(                  rB| j(                  j+                         }| j                  j-                  |       | j(                  rB|S )Nr   )r&   rX   rz   )r$   rH   testr   dictBaseRequestr   rt   rX   rv   from_environrQ   r`   ry   rW   rw   r'   openr7   json_modulerV   popenter_context)
r)   r{   r|   r-   r.   rx   r_   responsecmr3   s
            r4   r   zFlaskClient.open   s    JGhmm22D+F
 $q'8==#?#?@tAw-'+'9'9':N:N:TRT'U$!--/DGT*(55G!1!1@R@RSU@V 6 +- 
 tAw-"&"4"4W__"E 55dFCG
 	!!#7<-   

  $//44   ##'')B--b1    r5   c                B    | j                   rt        d      d| _         | S )NzCannot nest client invocationsT)rU   rc   r)   s    r4   	__enter__zFlaskClient.__enter__   s$      ?@@ $r5   c                F    d| _         | j                  j                          y )NF)rU   rW   rw   )r)   exc_type	exc_valuetbs       r4   __exit__zFlaskClient.__exit__   s     !&!!#r5   )r-   r=   r.   r=   r>   r?   )r-   r=   r.   r=   r>   z%t.Generator[SessionMixin, None, None])
r-   r=   r{   r   r|   r   r.   r=   r>   r   )r>   rP   )r   ztype | Noner   zBaseException | Noner   zTracebackType | Noner>   r?   )r@   rA   rB   rC   __annotations__r(   r   ro   rt   ry   r   r   r   rD   rE   s   @r4   rP   rP   l   s     
 /
/
&+/
	./
 /
b !&	++ + 	+
 + 
+Z$$ ($ !	$
 
$r5   rP   c                  B     e Zd ZdZd fdZ	 d	 	 	 	 	 	 	 d 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`.
    c                2    || _         t        |   di | y )NrM   )r&   r'   r(   )r)   r&   r.   r3   s      r4   r(   zFlaskCliRunner.__init__  s    "6"r5   c                     | j                   j                  }d|vrt         fd      |d<   t           ||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.
        r9   c                      j                   S )N)r&   r   s   r4   <lambda>z'FlaskCliRunner.invoke.<locals>.<lambda>%  s    $(( r5   )
create_app)r&   clir   r'   invoke)r)   r   r-   r.   r3   s   `   r4   r   zFlaskCliRunner.invoke  sD    " ;((,,C&2BCF5Mw~c42622r5   )r&   r   r.   r=   r>   r?   )NN)r   r=   r-   r=   r.   r=   r>   r=   )r@   rA   rB   rC   r(   r   rD   rE   s   @r4   r   r     s<    
#
 0433',3?D3	3 3r5   r   )r>   r;   )$
__future__r   importlib.metadatarJ   typingt
contextlibr   r   r   typesr   urllib.parser   werkzeug.testrH   click.testingr	   r
   werkzeug.wrappersr   r   r   r   sessionsr   TYPE_CHECKINGr   r&   r   r~   r   rI   rN   rP   r   rM   r5   r4   <module>r      s}    "   %     !  #   4  "??*D2X]]11 D2N  W$& W$t!3Y !3r5   