o
    2g)(                     @   s   d dl 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 d	dlmZ d	dlmZ ejrYd	dlmZ d	dlmZ G dd dejj Z G dd deZ!G dd de
Z"dS )    N)contextmanager)copy)TracebackType)	CliRunner)Client)	url_parse)Request   )_request_ctx_stack)
ScriptInfo)dumps)SessionMixin)Flask)Responsec                       s|   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dZdejdejdef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base_url	subdomain
url_schemeargskwargsreturnc                    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__ \/var/www/mastermindingenieria.com/MONITOR/venv/lib/python3.10/site-packages/flask/testing.pyr+   ,   s4   


 zEnvironBuilder.__init__objc                 K   s   | d| j t|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   )
setdefaultr   
json_dumps)r,   r5   r   r3   r3   r4   r7   T   s   zEnvironBuilder.json_dumps)r   NNN)__name__
__module____qualname____doc__strtOptionalAnyr+   r7   __classcell__r3   r3   r1   r4   r      s0    	"(r   c                       s   e Zd ZU dZded< dZdejdejddf fd	d
Ze	dejdejdej
eddf fddZdddddejdedededejddf fddZdddZdedededdfddZ  ZS )FlaskClientaE  Works like a regular Werkzeug test client but has some knowledge about
    how Flask works to defer the cleanup of the request context stack to the
    end of a ``with`` body when used in a ``with`` statement.  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   applicationFr   r   r   Nc                    s*   t  j|i | ddtj d| _d S )Nz	127.0.0.1z	werkzeug/)REMOTE_ADDRHTTP_USER_AGENT)r*   r+   werkzeug__version__environ_base)r,   r   r   r1   r3   r4   r+   p   s   
zFlaskClient.__init__c              	   o   s    | j du r
td| j}|di }| j | tj}|j|i |O}|j}|	||j
}|du r7tdt| z	|V  W t  nt  w | }	||s[||||	 |	|j
j}
| j |j
j|
 W d   dS 1 svw   Y  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:Session transactions only make sense with cookies enabled.environ_overridesz?Session backend did not open a session. Check the configuration)
cookie_jarRuntimeErrorrB   r6   inject_wsgir
   toptest_request_contextsession_interfaceopen_sessionrequestpushpopresponse_classis_null_sessionsave_sessionget_wsgi_headersenvironextract_wsgi)r,   r   r   r   rH   outer_reqctxcrN   sessrespheadersr3   r3   r4   session_transactionw   s4   


"zFlaskClient.session_transactionas_tuplebufferedfollow_redirectsr`   ra   rb   r   c          
         s  d } fdd}|sNt |dkrN|d }t|tjjr,t|}	||	jp$i |	_|	 }n"t|tr?tj	| j
|i d }nt|trNt|}||j|_|d u rx||di |d< t j
g|R i |}	z
|	 }W |	  n|	  w t j||||dS )Nc                    s   i  j | d jiS )Nzflask._preserve_context)rG   preserve_context)otherr,   r3   r4   copy_environ   s   z&FlaskClient.open.<locals>.copy_environr	   r   )r   rG   rG   r_   )lenr(   rE   testr   r   rG   get_requestdictfrom_environrB   BaseRequestrW   r#   closer*   open)
r,   r`   ra   rb   r   r   rP   rf   argbuilderr1   re   r4   rn      s:   	



zFlaskClient.openc                 C   s   | j rtdd| _ | S )NzCannot nest client invocationsT)rc   rJ   re   r3   r3   r4   	__enter__   s   zFlaskClient.__enter__exc_type	exc_valuetbc                 C   s,   d| _ 	 tj}|d ur|jr|  nd S q)NF)rc   r
   rL   	preservedrR   )r,   rr   rs   rt   rL   r3   r3   r4   __exit__   s   
zFlaskClient.__exit__)r   rA   )r8   r9   r:   r;   __annotations__rc   r=   r?   r+   r   	Generatorr   r^   r!   rn   rq   typeBaseExceptionr   rv   r@   r3   r3   r1   r4   rA   ^   sN   
 9
1rA   c                	       sZ   e Zd ZdZdddejddf fddZ	dd	ejd
ejdejdejf 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   Nc                    s   || _ t jdi | d S )Nr3   )r   r*   r+   )r,   r   r   r1   r3   r4   r+      s   zFlaskCliRunner.__init__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.
        Nr5   c                      s    j S )N)r   r3   re   r3   r4   <lambda>  s    z'FlaskCliRunner.invoke.<locals>.<lambda>)
create_app)r   r|   r   r*   invoke)r,   r|   r   r   r1   re   r4   r     s
   zFlaskCliRunner.invoke)NN)	r8   r9   r:   r;   r=   r?   r+   r   r@   r3   r3   r1   r4   r{      s    r{   )#typingr=   
contextlibr   r   typesr   werkzeug.testrE   click.testingr   r   werkzeug.urlsr   werkzeug.wrappersr   rl    r
   r|   r   jsonr   r7   sessionsr   TYPE_CHECKINGr   r   wrappersr   rh   r   rA   r{   r3   r3   r3   r4   <module>   s(    H 