ó
¹×-]c           @   sã   d  Z  d d l 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 e j j f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sÙ   
    flask.testing
    ~~~~~~~~~~~~~

    Implements test support helpers.  This module is lazily imported
    and usually not used in production environments.

    :copyright: 2010 Pallets
    :license: BSD-3-Clause
iÿÿÿÿN(   t   contextmanager(   t	   CliRunner(   t   Client(   t	   url_parsei   (   t   _request_ctx_stack(   t
   ScriptInfo(   t   dumpst   EnvironBuilderc           B   s,   e  Z d  Z d d d d d „ Z d „  Z RS(   s½  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`.
    t   /c         O   s]  | p | p | s= | d  k	 t | p( | ƒ k s= t d ƒ ‚ | d  k r1| j j d ƒ p^ d } | j d }	 | r‰ d j | | ƒ } n  | d  k r¥ | j d } n  t | ƒ }
 d j d |
 j pÆ | d	 |
 j pÕ | d
 |	 j	 d ƒ ƒ } |
 j
 } |
 j r1t |
 j t ƒ rd n d } | | |
 j 7} q1n  | |  _ t t |  ƒ j | | | | Ž d  S(   Ns8   Cannot pass "subdomain" or "url_scheme" with "base_url".t   SERVER_NAMEt	   localhostt   APPLICATION_ROOTs   {0}.{1}t   PREFERRED_URL_SCHEMEs   {scheme}://{netloc}/{path}t   schemet   netloct   pathR   t   ?(   t   Nonet   boolt   AssertionErrort   configt   gett   formatR   R   R   t   lstripR   t   queryt
   isinstancet   bytest   appt   superR   t   __init__(   t   selfR   R   t   base_urlt	   subdomaint
   url_schemet   argst   kwargst	   http_hostt   app_roott   urlt   sep(    (    s,   lib/python2.7/site-packages/flask/testing.pyR   /   s.    
						c         K   s    | j  d |  j ƒ t | |  S(   s³   Serialize ``obj`` to a JSON-formatted string.

        The serialization will be configured according to the config associated
        with this EnvironBuilder's ``app``.
        R   (   t
   setdefaultR   t
   json_dumps(   R   t   objR#   (    (    s,   lib/python2.7/site-packages/flask/testing.pyR)   X   s    N(   t   __name__t
   __module__t   __doc__R   R   R)   (    (    (    s,   lib/python2.7/site-packages/flask/testing.pyR      s   #c          O   s    t  j t d ƒ ƒ t |  | Ž  S(   s±   Create a :class:`flask.testing.EnvironBuilder`.

    .. deprecated: 1.1
        Will be removed in 1.2. Construct ``flask.testing.EnvironBuilder``
        directly instead.
    s‚   "make_test_environ_builder()" is deprecated and will be removed in 1.2. Construct "flask.testing.EnvironBuilder" directly instead.(   t   warningst   warnt   DeprecationWarningR   (   R"   R#   (    (    s,   lib/python2.7/site-packages/flask/testing.pyt   make_test_environ_builderb   s    
t   FlaskClientc           B   sG   e  Z d  Z e Z d „  Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 RS(   sD  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 :ref:`testing` chapter.
    c         O   s;   t  t |  ƒ j | | Ž  i d d 6d t j d 6|  _ d  S(   Ns	   127.0.0.1t   REMOTE_ADDRs	   werkzeug/t   HTTP_USER_AGENT(   R   R2   R   t   werkzeugt   __version__t   environ_base(   R   R"   R#   (    (    s,   lib/python2.7/site-packages/flask/testing.pyR   „   s    c         o   s-  |  j  d k r t d ƒ ‚ n  |  j } | j d i  ƒ } |  j  j | ƒ t j } | j | | Ž  Ã } | j	 } | j
 | | j ƒ } | d k r  t d ƒ ‚ n  t j | ƒ z	 | VWd t j ƒ  X| j ƒ  }	 | j | ƒ sõ | j | | |	 ƒ n  |	 j | j j ƒ }
 |  j  j | j j |
 ƒ Wd QXd S(   s’  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.
        s:   Session transactions only make sense with cookies enabled.t   environ_overridess?   Session backend did not open a session. Check the configurationN(   t
   cookie_jarR   t   RuntimeErrort   applicationR(   t   inject_wsgiR   t   topt   test_request_contextt   session_interfacet   open_sessiont   requestt   pusht   popt   response_classt   is_null_sessiont   save_sessiont   get_wsgi_headerst   environt   extract_wsgi(   R   R"   R#   R   R8   t   outer_reqctxt   cR?   t   sesst   respt   headers(    (    s,   lib/python2.7/site-packages/flask/testing.pyt   session_transaction‹   s,    			
	c      	   O   sR  | j  d t ƒ } | j  d t ƒ } | j  d t ƒ } | rÑ t | ƒ d k rÑ t | d t j j t f ƒ rÑ |  j j	 ƒ  } t | d t j j ƒ r° | j
 | d j ƒ  ƒ n | j
 | d ƒ |  j | d <n_ |  j | j d i  ƒ d <| j d |  j ƒ t |  j | | Ž } z | j ƒ  } Wd  | j ƒ  Xt j |  | d | d | d | ƒS(	   Nt   as_tuplet   bufferedt   follow_redirectsi   i    s   flask._preserve_contextR8   R7   (   RC   t   Falset   lenR   R5   t   testR   t   dictR7   t   copyt   updatet   get_environt   preserve_contextR(   R;   t   closeR   t   open(   R   R"   R#   RP   RQ   RR   RH   t   builder(    (    s,   lib/python2.7/site-packages/flask/testing.pyR\   À   s.    c         C   s%   |  j  r t d ƒ ‚ n  t |  _  |  S(   Ns   Cannot nest client invocations(   RZ   R:   t   True(   R   (    (    s,   lib/python2.7/site-packages/flask/testing.pyt	   __enter__æ   s    		c         C   sF   t  |  _ x6 t rA t j } | d  k	 r= | j r= | j ƒ  q Pq Wd  S(   N(   RS   RZ   R^   R   R=   R   t	   preservedRC   (   R   t   exc_typet	   exc_valuet   tbR=   (    (    s,   lib/python2.7/site-packages/flask/testing.pyt   __exit__ì   s    			(   R+   R,   R-   RS   RZ   R   R    RO   R\   R_   Rd   (    (    (    s,   lib/python2.7/site-packages/flask/testing.pyR2   s   s   	5	&	t   FlaskCliRunnerc           B   s&   e  Z d  Z d „  Z d d d „ Z RS(   s®   A :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         K   s#   | |  _  t t |  ƒ j |   d  S(   N(   R   R   Re   R   (   R   R   R#   (    (    s,   lib/python2.7/site-packages/flask/testing.pyR     s    	c            sb   | d k r ˆ  j j } n  d | k rF t d ‡  f d †  ƒ | d <n  t t ˆ  ƒ j | | |  S(   sc  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*   t
   create_appc              s   ˆ  j  S(   N(   R   (    (   R   (    s,   lib/python2.7/site-packages/flask/testing.pyt   <lambda>  t    N(   R   R   t   cliR   R   Re   t   invoke(   R   Ri   R"   R#   (    (   R   s,   lib/python2.7/site-packages/flask/testing.pyRj     s
    N(   R+   R,   R-   R   R   Rj   (    (    (    s,   lib/python2.7/site-packages/flask/testing.pyRe   ü   s   	(   R-   R.   t
   contextlibR    t   werkzeug.testR5   t   click.testingR   R   t   werkzeug.urlsR   Rh   R   Ri   R   t   jsonR   R)   RU   R   R1   R2   Re   (    (    (    s,   lib/python2.7/site-packages/flask/testing.pyt   <module>   s   I	‰