
    p)f'                    R   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 ddlmZ  G d dej@                  jB                        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)WSGIEnvironment)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               J/var/www/html/flask-app/venv/lib/python3.12/site-packages/flask/testing.pyr)   zEnvironBuilder.__init__0   s,    	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      r5   
json_dumpszEnvironBuilder.json_dumpsX   s$     #txx}}""31&11r6   )r   NNN)r'   r   r#   strr+   
str | Noner,   r=   r-   r=   r.   t.Anyr/   r>   returnNone)r:   r>   r/   r>   r?   r<   )__name__
__module____qualname____doc__r)   r;   __classcell__r4   s   @r5   r   r      so    0 # $!%&:&: &: 	&:
 &: &: &: &: 
&:P2r6   r    c                 X    t         st        j                  j                  d      a t         S )Nwerkzeug)_werkzeug_version	importlibmetadataversion r6   r5   _get_werkzeug_versionrO   d   s$     %..66zBr6   c                       e Zd ZU dZded<   d fdZe	 	 	 	 	 	 dd       ZddZ	 	 	 	 	 	 d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_stackrO   environ_base)r*   r.   r/   r4   s      r5   r)   zFlaskClient.__init__}   sK    $)&) %<>'k&!*+@+B*CD
r6   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	TypeErrorrR   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          r5   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)rY   rV   rW   append)r*   otherouts      r5   _copy_environzFlaskClient._copy_environ   s<    ,"",e,  595G5G5N5NC12
r6   c                    | j                  |j                  di             |d<   t        | j                  g|i |}	 |j	                         |j                          S # |j                          w xY w)NrY   )ru   r   r   rR   get_requestclose)r*   r.   r/   builders       r5   _request_from_builder_argsz&FlaskClient._request_from_builder_args   sb     "&!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'   rY   r{   )r%   rI   testr   dictBaseRequestr   ru   rY   rw   from_environrR   ra   rz   rX   rx   r(   openr8   json_modulerW   popenter_context)
r*   r|   r}   r.   r/   ry   r`   responsecmr4   s
            r5   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    r6   c                B    | j                   rt        d      d| _         | S )NzCannot nest client invocationsT)rV   rd   r*   s    r5   	__enter__zFlaskClient.__enter__   s$      ?@@ $r6   c                F    d| _         | j                  j                          y )NF)rV   rX   rx   )r*   exc_type	exc_valuetbs       r5   __exit__zFlaskClient.__exit__   s     !&!!#r6   )r.   r>   r/   r>   r?   r@   )r.   r>   r/   r>   r?   zt.Iterator[SessionMixin])rs   r   r?   r   )r.   ztuple[t.Any, ...]r/   zdict[str, t.Any]r?   r   )
r.   r>   r|   r   r}   r   r/   r>   r?   r   )r?   rQ   )r   ztype | Noner   zBaseException | Noner   zTracebackType | Noner?   r@   )rA   rB   rC   rD   __annotations__r)   r   rp   ru   rz   r   r   r   rE   rF   s   @r5   rQ   rQ   m   s     
 /
/
&+/
	!/
 /
b	%	/?			 !&	++ + 	+
 + 
+Z$$ ($ !	$
 
$r6   rQ   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 )NrN   )r'   r(   r)   )r*   r'   r/   r4   s      r5   r)   zFlaskCliRunner.__init__  s    "6"r6   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.
        r:   c                      j                   S )N)r'   r   s   r5   <lambda>z'FlaskCliRunner.invoke.<locals>.<lambda>(  s    $(( r6   )
create_app)r'   clir   r(   invoke)r*   r   r.   r/   r4   s   `   r5   r   zFlaskCliRunner.invoke  sD    " ;((,,C&2BCF5Mw~c42622r6   )r'   r   r/   r>   r?   r@   )NN)r   r>   r.   r>   r/   r>   r?   r>   )rA   rB   rC   rD   r)   r   rE   rF   s   @r5   r   r   	  s<    
#
 0433',3?D3	3 3r6   r   )r?   r<   )&
__future__r   importlib.metadatarK   typingt
contextlibr   r   r   typesr   urllib.parser   werkzeug.testrI   click.testingr	   r
   werkzeug.wrappersr   r   r   r   sessionsr   TYPE_CHECKING_typeshed.wsgir   r   r'   r   r   r   rJ   rO   rQ   r   rN   r6   r5   <module>r      s    "   %     !  #   4  "??.*D2X]]11 D2N  Y$& Y$x!3Y !3r6   