B
    Ǉb(                 @   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rd 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   )
ScriptInfo)_request_ctx_stack)dumps)SessionMixin)TestResponse)Flaskc            
       sb   e Zd ZdZddeeje eje eje ejejdd fddZejeje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`.
    /Nr   )apppathbase_url	subdomain
url_schemeargskwargsreturnc                s   |s|s|r(|d k	t |p|ks(td|d kr|jdp>d}|jd }	|r\| d| }|d krn|jd }t|}
|
jp~| d|
jp| d|	d }|
j}|
j	rt
|
j	trd	nd
}|||
j	 7 }|| _t j||f|| d S )Nz8Cannot pass "subdomain" or "url_scheme" with "base_url".ZSERVER_NAME	localhostZAPPLICATION_ROOT.ZPREFERRED_URL_SCHEMEz://r      ??)boolAssertionErrorconfiggetr   schemenetloclstripr   query
isinstancebytesr   super__init__)selfr   r   r   r   r   r   r   Z	http_hostZapp_rooturlsep)	__class__ a/var/www/downstreamdata.science/rtclock/rtclock-venv/lib/python3.7/site-packages/flask/testing.pyr)   -   s(    


&zEnvironBuilder.__init__)objr   r   c             K   s   | d| j t|f|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*   r0   r   r.   r.   r/   r2   U   s    zEnvironBuilder.json_dumps)r   NNN)__name__
__module____qualname____doc__strtOptionalAnyr)   r2   __classcell__r.   r.   )r-   r/   r      s      r   c                   s   e Zd ZU dZded< dZejejdd fddZe	ejejej
eddf dd	d
Zdd Zdd Zdddejeeejdd fddZd dddZeje eje eje dd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   applicationFN)r   r   r   c                s&   t  j|| ddtj d| _d S )Nz	127.0.0.1z	werkzeug/)ZREMOTE_ADDRZHTTP_USER_AGENT)r(   r)   werkzeug__version__environ_base)r*   r   r   )r-   r.   r/   r)   q   s    zFlaskClient.__init__c          	   o   s   | j dkrtd| j}|di }| j | tj}|j||}|j}|	||j
}|dkrhtdt| z
|V  W dt  X | }	||s||||	 |	|j
j}
| j |j
j|
 W dQ R X 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)Z
cookie_jarRuntimeErrorr=   r1   Zinject_wsgir   topZtest_request_contextsession_interfaceZopen_sessionrequestpushpopresponse_classZis_null_sessionZsave_sessionZget_wsgi_headersenvironZextract_wsgi)r*   r   r   r   rA   Zouter_reqctxcrD   sessrespheadersr.   r.   r/   session_transactionx   s,    





zFlaskClient.session_transactionc             C   s   | j |d| jiS )Nzflask._preserve_context)r@   preserve_context)r*   otherr.   r.   r/   _copy_environ   s    zFlaskClient._copy_environc             C   s@   |  |di |d< t| jf||}z| S |  X d S )Nr@   )rQ   r!   r   r=   get_requestclose)r*   r   r   builderr.   r.   r/   _request_from_builder_args   s
    z&FlaskClient._request_from_builder_args)bufferedfollow_redirectsr   )r   rV   rW   r   r   c               s   |rt |d tjjttfrt |d tjjrVt|d }| |jpFi |_|	 }qt |d trtj
|d | j| i d	 }qt|d }| |j|_n| ||}t j|||dS )Nr   )r   r@   )rV   rW   )r&   r>   testr   dictBaseRequestr   rQ   r@   rR   Zfrom_environr=   rI   rU   r(   open)r*   rV   rW   r   r   rT   rE   )r-   r.   r/   r[      s     
zFlaskClient.open)r   c             C   s   | j rtdd| _ | S )NzCannot nest client invocationsT)rO   rB   )r*   r.   r.   r/   	__enter__   s    zFlaskClient.__enter__)exc_type	exc_valuetbr   c             C   s0   d| _ x$tj}|d k	r&|jr&|  qP qW d S )NF)rO   r   rC   Z	preservedrG   )r*   r]   r^   r_   rC   r.   r.   r/   __exit__   s    
zFlaskClient.__exit__)r3   r4   r5   r6   __annotations__rO   r8   r:   r)   r   	Generatorr   rN   rQ   rU   r   r[   r\   r9   typeBaseExceptionr   r`   r;   r.   r.   )r-   r/   r<   _   s$   
4r<   c                   sN   e Zd ZdZdejdd fddZd
ejejejej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   N)r   r   r   c                s   || _ t jf | d S )N)r   r(   r)   )r*   r   r   )r-   r.   r/   r)      s    zFlaskCliRunner.__init__)clir   r   r   c                s@   |dkr j j}d|kr.t fddd|d< t j||f|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.
        Nr0   c                  s    j S )N)r   r.   )r*   r.   r/   <lambda>      z'FlaskCliRunner.invoke.<locals>.<lambda>)Z
create_app)r   rf   r
   r(   invoke)r*   rf   r   r   )r-   )r*   r/   ri     s
    zFlaskCliRunner.invoke)NN)	r3   r4   r5   r6   r8   r:   r)   ri   r;   r.   r.   )r-   r/   re      s   re   )"typingr8   
contextlibr   r   typesr   Zwerkzeug.testr>   Zclick.testingr   r   Zwerkzeug.urlsr   Zwerkzeug.wrappersr   rZ   rf   r
   globalsr   jsonr   r2   sessionsr   TYPE_CHECKINGr   r   r   rX   r   r<   re   r.   r.   r.   r/   <module>   s&   H 