U a)(@sddlZddlmZddlmZddlmZddlZddl m Z ddlm Z ddl m Z ddlmZd d lmZd d lmZd d lmZd d lmZejrd dlmZd dlmZGdddejj Z Gddde Z!Gddde Z"dS)N)contextmanager)copy) TracebackType) CliRunner)Client) url_parse)Request)_request_ctx_stack) ScriptInfo)dumps) SessionMixin)Flask)Responsec sbeZdZdZd deejeejeejeejejddfdd Zejejedd d Z Z S) EnvironBuilderaAn :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)apppathbase_url subdomain url_schemeargskwargsreturnc s|s |s |r(|dk t|p|ks(td|dkr|jdp>d}|jd} |r\|d|}|dkrn|jd}t|} | jp~|d| jp|d| d}| j}| j rt | j t rd nd } || | j 7}||_ t j||f||dS) Nz8Cannot pass "subdomain" or "url_scheme" with "base_url".Z SERVER_NAME localhostZAPPLICATION_ROOT.ZPREFERRED_URL_SCHEMEz://r??)boolAssertionErrorconfiggetrschemenetloclstriprquery isinstancebytesrsuper__init__) selfrrrrrrrZ http_hostZapp_rooturlsep __class__NC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-e702oxwa\flask\testing.pyr),s0   $zEnvironBuilder.__init__)objrrcKs|d|jt|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*r1rr/r/r0r3TszEnvironBuilder.json_dumps)rNNN) __name__ __module__ __qualname____doc__strtOptionalAnyr)r3 __classcell__r/r/r-r0rs (rcseZdZUdZded<dZejejddfdd Ze ejejej e ddfdd d Z dddd eje e e ejd d fddZddddZeeeddddZZS) FlaskClientaEWorks 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)rrrcs&tj||ddtjd|_dS)Nz 127.0.0.1z werkzeug/)Z REMOTE_ADDRZHTTP_USER_AGENT)r(r)werkzeug __version__ environ_base)r*rrr-r/r0r)ps zFlaskClient.__init__c os|jdkrtd|j}|di}|j|tj}|j||}|j}| ||j }|dkrhtdt |z |VW5t X| } ||s|||| | |j j} |j|j j| W5QRXdS)aWhen 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>r2Z inject_wsgir topZtest_request_contextsession_interfaceZ open_sessionrequestpushpopresponse_classZis_null_sessionZ save_sessionZget_wsgi_headersenvironZ extract_wsgi) r*rrrrBZ outer_reqctxcrEsessrespheadersr/r/r0session_transactionws0       zFlaskClient.session_transactionas_tuplebufferedfollow_redirectsr)rrQrRrSrrc sd}fdd}|st|dkr|d}t|tjjrXt|} || jpHi| _| }nDt|tr~tj |j |id}nt|t rt|}||j |_ |dkr|| di|d<tj f||} z | }W5| Xtj||||dS)Ncsj|djiS)Nzflask._preserve_context)rApreserve_context)otherr*r/r0 copy_environs z&FlaskClient.open..copy_environr r)rrArArP)lenr&r?testrrrA get_requestdictZ from_environr> BaseRequestrJr!closer(open) r*rQrRrSrrrFrWargZbuilderr-rVr0r^s:         zFlaskClient.open)rcCs|jrtdd|_|S)NzCannot nest client invocationsT)rTrCrVr/r/r0 __enter__szFlaskClient.__enter__)exc_type exc_valuetbrcCs,d|_tj}|dk r(|jr(|qq(qdS)NF)rTr rDZ preservedrH)r*rarbrcrDr/r/r0__exit__s  zFlaskClient.__exit__)r4r5r6r7__annotations__rTr9r;r)r Generatorr rOrr^r`type BaseExceptionrrdr<r/r/r-r0r=^s4 91r=csNeZdZdZdejddfdd Zd ejejejejdfdd ZZS) 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`. rN)rrrc s||_tjf|dSN)rr(r))r*rrr-r/r0r)szFlaskCliRunner.__init__)clirrrc s@|dkrjj}d|kr.tfddd|d<tj||f|S)acInvokes a CLI command in an isolated environment. See :meth:`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. Nr1csjSrj)rr/rVr/r0z'FlaskCliRunner.invoke..)Z create_app)rrkr r(invoke)r*rkrrr-rVr0rns zFlaskCliRunner.invoke)NN) r4r5r6r7r9r;r)rnr<r/r/r-r0risri)#typingr9 contextlibrrtypesrZ werkzeug.testr?Z click.testingrrZ werkzeug.urlsrZwerkzeug.wrappersrr\r rkr jsonr r3sessionsr TYPE_CHECKINGrrwrappersrrYrr=rir/r/r/r0s&             H