o
    'f'                     @  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 ejrcd 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)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 re|jdpd}|jd }	|r.| d| }|d u r7|jd }t|}
|
jp?| d|
jpE| d|	d }|
j}|
jret	|
jt
r\d	nd
}|||
j 7 }|| _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__ E/home/ubuntu/flask/venv/lib/python3.10/site-packages/flask/testing.pyr0   0   s4   


 zEnvironBuilder.__init__objc                 K  s   | 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)r1   r:   r   r8   r8   r9   
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   )r:   r   r   r   r   r   )__name__
__module____qualname____doc__r0   r=   __classcell__r8   r8   r6   r9   r      s    (r    r   r   c                   C  s   t stjda t S )Nwerkzeug)_werkzeug_version	importlibmetadataversionr8   r8   r8   r9   _get_werkzeug_versiond   s   rI   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/   r0   preserve_context_new_contextsr   _context_stackrI   environ_base)r1   r   r   r6   r8   r9   r0   }   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	TypeErrorrK   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)r1   r   r   r   ctxsessrespr8   r8   r9   session_transaction   s2   

zFlaskClient.session_transactionotherr   c                 C  s$   i | j |}| jr| jj|d< |S )Nzwerkzeug.debug.preserve_context)rQ   rN   rO   append)r1   ri   outr8   r8   r9   _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 )NrQ   )rl   r(   r   rK   get_requestclose)r1   r   r   builderr8   r8   r9   _request_from_builder_args   s
   z&FlaskClient._request_from_builder_argsFbufferedfollow_redirectsru   r&   rv   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r{| j }| j| | jsm|S )Nr   )r   rQ   rt   )r-   rD   testr   dictro   r   rl   rQ   rp   from_environrK   rY   rs   rP   rq   r/   openr;   json_modulerO   popenter_context)	r1   ru   rv   r   r   rr   rX   responsecmr6   r8   r9   rz      s8   



zFlaskClient.openc                 C  s   | j rtdd| _ | S )NzCannot nest client invocationsT)rN   r\   r1   r8   r8   r9   	__enter__   s   zFlaskClient.__enter__exc_typetype | None	exc_valueBaseException | NonetbTracebackType | Nonec                 C  s   d| _ | j  d S )NF)rN   rP   rq   )r1   r   r   r   r8   r8   r9   __exit__   s   zFlaskClient.__exit__)r   r   r   r   r   r   )r   r   r   r   r   rR   )ri   r   r   r   )r   rm   r   rn   r   ro   )
r   r   ru   r&   rv   r&   r   r   r   r   )r   rJ   )r   r   r   r   r   r   r   r   )r>   r?   r@   rA   __annotations__r0   r   rh   rl   rs   rz   r   r   rB   r8   r8   r6   r9   rJ   m   s   
 

1

-rJ   c                      s4   e Zd ZdZd fdd	Z	
dd fddZ  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   r   r   c                   s   || _ t jdi | d S )Nr8   )r   r/   r0   )r1   r   r   r6   r8   r9   r0     s   zFlaskCliRunner.__init__Nclir   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.
        Nr:   c                     s    j S )N)r   r8   r   r8   r9   <lambda>(  s    z'FlaskCliRunner.invoke.<locals>.<lambda>)
create_app)r   r   r   r/   invoke)r1   r   r   r   r6   r   r9   r     s
   zFlaskCliRunner.invoke)r   r   r   r   r   r   )NN)r   r   r   r   r   r   r   r   )r>   r?   r@   rA   r0   r   rB   r8   r8   r6   r9   r   	  s
    r   )r   r   )&
__future__r   importlib.metadatarF   typingt
contextlibr   r   r   typesr   urllib.parser   werkzeug.testrD   click.testingr   r	   werkzeug.wrappersr
   ro   r   r   sessionsr   TYPE_CHECKING_typeshed.wsgir   r   r   r   rw   r   rE   rI   rJ   r   r8   r8   r8   r9   <module>   s0    G
	 