ó
ù¢TQc           @  sƒ  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z y  d  d l	 m
 Z
 m Z m Z Wn7 e k
 r¹ d  d l m
 Z
 d  d l m Z m Z n Xd  d l m Z d  d	 l m Z m Z d  d
 l m Z d  d l m Z d  d l m Z m Z m Z d  d l m Z d  d l m Z m  Z  m! Z! d  d l" m# Z# d  d l$ m% Z% d  d l& m' Z' d  d l( m) Z) m* Z* d  d l+ m, Z, d  d l- m. Z. d  d l/ m0 Z0 d  d l1 m2 Z2 d  d l3 m4 Z4 d+ Z5 d Z6 d e6 Z7 e j8 d ƒ Z9 d e: f d  „  ƒ  YZ; d! „  Z< d" e f d# „  ƒ  YZ= d$ „  Z> d% „  Z? d& „  Z@ d' e: f d( „  ƒ  YZA d) eA f d* „  ƒ  YZB d S(,   iÿÿÿÿ(   t   unicode_literalsN(   t   copy(   t   BytesIO(   t   unquotet   urlparset   urlsplit(   R   (   R   R   (   t   settings(   t   authenticatet   login(   t   BaseHandler(   t   WSGIRequest(   t   request_startedt   request_finishedt   got_request_exception(   t   close_connection(   t   SimpleCookiet   HttpRequestt	   QueryDict(   t   TemplateDoesNotExist(   t   signals(   t   curry(   t   force_bytest	   force_str(   t	   urlencode(   t   import_module(   t   is_iterable(   t   six(   t   ContextListu   Clientu   RequestFactoryu   encode_fileu   encode_multipartu   BoUnDaRyStRiNgu    multipart/form-data; boundary=%su   .*; charset=([\w\d-]+);?t   FakePayloadc           B  s8   e  Z d  Z d d „ Z d „  Z d d „ Z d „  Z RS(   u  
    A wrapper around BytesIO that restricts what can be read since data from
    the network can't be seeked and cannot be read outside of its content
    length. This makes sure that views can't do anything under the test client
    that wouldn't work in Real Life.
    c         C  s>   t  ƒ  |  _ d |  _ t |  _ | d  k	 r: |  j | ƒ n  d  S(   Ni    (   R   t   _FakePayload__contentt   _FakePayload__lent   Falset   read_startedt   Nonet   write(   t   selft   content(    (    s+   ../Django//lib/python/django/test/client.pyt   __init__/   s
    		c         C  s   |  j  S(   N(   R   (   R#   (    (    s+   ../Django//lib/python/django/test/client.pyt   __len__6   s    c         C  sƒ   |  j  s% |  j j d ƒ t |  _  n  | d  k rC |  j p= d } n  |  j | k s^ t d ƒ ‚ |  j j | ƒ } |  j | 8_ | S(   Ni    uF   Cannot read more than the available bytes from the HTTP incoming data.(   R    R   t   seekt   TrueR!   R   t   AssertionErrort   read(   R#   t	   num_bytesR$   (    (    s+   ../Django//lib/python/django/test/client.pyR*   9   s    	c         C  sM   |  j  r t d ƒ ‚ n  t | ƒ } |  j j | ƒ |  j t | ƒ 7_ d  S(   Nu.   Unable to write a payload after he's been read(   R    t
   ValueErrorR   R   R"   R   t   len(   R#   R$   (    (    s+   ../Django//lib/python/django/test/client.pyR"   D   s
    	N(   t   __name__t
   __module__t   __doc__R!   R%   R&   R*   R"   (    (    (    s+   ../Django//lib/python/django/test/client.pyR   (   s
   	c         c  sC   z x |  D] } | Vq
 WWd  t  j t ƒ | ƒ  t  j t ƒ Xd  S(   N(   R   t
   disconnectR   t   connect(   t   iterablet   closet   item(    (    s+   ../Django//lib/python/django/test/client.pyt   closing_iterator_wrapperL   s    t   ClientHandlerc           B  s#   e  Z d  Z e d „ Z d „  Z RS(   uœ   
    A HTTP Handler that can be used for testing purposes.
    Uses the WSGI interface to compose requests, but returns
    the raw HttpResponse object
    c         O  s&   | |  _  t t |  ƒ j | | Ž  d  S(   N(   t   enforce_csrf_checkst   superR7   R%   (   R#   R8   t   argst   kwargs(    (    s+   ../Django//lib/python/django/test/client.pyR%   \   s    	c         C  s³   d d l  m } |  j d  k r, |  j ƒ  n  t j d |  j ƒ t | ƒ } |  j	 | _
 |  j | ƒ } | j r‹ t | j | j ƒ | _ n$ t j t ƒ | j ƒ  t j t ƒ | S(   Niÿÿÿÿ(   R   t   sender(   t   django.confR   t   _request_middlewareR!   t   load_middlewareR   t   sendt	   __class__R
   R8   t   _dont_enforce_csrf_checkst   get_responset	   streamingR6   t   streaming_contentR4   R   R1   R   R2   (   R#   t   environR   t   requestt   response(    (    s+   ../Django//lib/python/django/test/client.pyt   __call__`   s    	
(   R.   R/   R0   R(   R%   RI   (    (    (    s+   ../Django//lib/python/django/test/client.pyR7   V   s   c         K  s?   |  j  d g  ƒ j | ƒ |  j  d t ƒ  ƒ j t | ƒ ƒ d S(   uœ   
    Stores templates and contexts that are rendered.

    The context is copied so that it is an accurate representation at the time
    of rendering.
    u	   templatesu   contextN(   t
   setdefaultt   appendR   R   (   t   storet   signalR<   t   templatet   contextR;   (    (    s+   ../Django//lib/python/django/test/client.pyt   store_rendered_templates|   s    c   	   	   C  sV  g  } d „  } d „  } x| j  ƒ  D]\ } } | | ƒ rY | j t |  | | ƒ ƒ q% t | t j ƒ rî t | ƒ rî x­ | D]h } | | ƒ r­ | j t |  | | ƒ ƒ q | j g  d |  d | d | g D] } | | ƒ ^ qÎ ƒ q Wq% | j g  d |  d | d | g D] } | | ƒ ^ qƒ q% W| j | d |  ƒ d g ƒ d j | ƒ S(	   u,  
    Encodes multipart POST data from a dictionary of form values.

    The key will be used as the form data name; the value will be transmitted
    as content. If the value is a file, the contents of the file will be sent
    as an application/octet-stream; otherwise, str(value) will be sent.
    c         S  s   t  |  t j ƒ S(   N(   R   R   t   DEFAULT_CHARSET(   t   s(    (    s+   ../Django//lib/python/django/test/client.pyt   <lambda>   s    c         S  s   t  |  d ƒ o t |  j ƒ S(   Nu   read(   t   hasattrt   callableR*   (   t   thing(    (    s+   ../Django//lib/python/django/test/client.pyRS   ’   s    u   --%su)   Content-Disposition: form-data; name="%s"u    u   --%s--t    s   
(   t   itemst   extendt   encode_filet
   isinstanceR   t   string_typesR   t   join(	   t   boundaryt   datat   linest   to_bytest   is_filet   keyt   valueR5   t   val(    (    s+   ../Django//lib/python/django/test/client.pyt   encode_multipart†   s0    			'	$
c         C  s€   d „  } t  j | j ƒ d } | d  k r4 d } n  | d |  ƒ | d | t j j | j ƒ f ƒ | d | ƒ d | j ƒ  g S(   Nc         S  s   t  |  t j ƒ S(   N(   R   R   RQ   (   RR   (    (    s+   ../Django//lib/python/django/test/client.pyRS   ´   s    i    u   application/octet-streamu   --%su8   Content-Disposition: form-data; name="%s"; filename="%s"u   Content-Type: %sRW   (   t	   mimetypest
   guess_typet   nameR!   t   ost   patht   basenameR*   (   R^   Rc   t   fileRa   t   content_type(    (    s+   ../Django//lib/python/django/test/client.pyRZ   ³   s    		t   RequestFactoryc           B  sž   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z i  d „ Z i  e	 d „ Z
 i  d „ Z d	 d
 d „ Z d	 d
 d „ Z d	 d
 d „ Z d	 d
 d „ Z RS(   uV  
    Class that lets you create mock Request objects for use in testing.

    Usage:

    rf = RequestFactory()
    get_request = rf.get('/hello/')
    post_request = rf.post('/submit/', {'foo': 'bar'})

    Once you have a request object you can pass it to any view function,
    just as if that view had been hooked up using a URLconf.
    c         K  s%   | |  _  t ƒ  |  _ t ƒ  |  _ d  S(   N(   t   defaultsR   t   cookiesR   t   errors(   R#   Rp   (    (    s+   ../Django//lib/python/django/test/client.pyR%   Ï   s    	c         K  sÞ   i |  j  j d d d d ƒ d 6t d ƒ d 6t d ƒ d	 6t d
 ƒ d 6t d ƒ d 6t d ƒ d 6t d ƒ d 6t d ƒ d 6d d 6t d ƒ d 6t d ƒ d 6|  j d 6t d 6t d 6t d 6} | j |  j ƒ | j | ƒ | S(   u5   
        The base environment for a request.
        t   headeru    t   sepu   ; u   HTTP_COOKIEu   /u	   PATH_INFOu	   127.0.0.1u   REMOTE_ADDRu   GETu   REQUEST_METHODu   SCRIPT_NAMEu
   testserveru   SERVER_NAMEu   80u   SERVER_PORTu   HTTP/1.1u   SERVER_PROTOCOLi   i    u   wsgi.versionu   httpu   wsgi.url_schemeRW   u
   wsgi.inputu   wsgi.errorsu   wsgi.multiprocessu   wsgi.multithreadu   wsgi.run_once(   i   i    (	   Rq   t   outputt   strR   Rr   R(   R   t   updateRp   (   R#   RG   RF   (    (    s+   ../Django//lib/python/django/test/client.pyt   _base_environÔ   s&    

c         K  s   t  |  j |   ƒ S(   u#   Construct a generic request object.(   R
   Rx   (   R#   RG   (    (    s+   ../Django//lib/python/django/test/client.pyRG   ñ   s    c         C  s]   | t  k r t t | ƒ St j | ƒ } | r@ | j d ƒ } n	 t j } t | d | ƒSd  S(   Ni   t   encoding(	   t   MULTIPART_CONTENTRf   t   BOUNDARYt   CONTENT_TYPE_REt   matcht   groupR   RQ   R   (   R#   R_   Rn   R}   t   charset(    (    s+   ../Django//lib/python/django/test/client.pyt   _encode_dataõ   s    	c         C  so   t  | d ƒ } | d r; | t d ƒ t  | d ƒ 7} n  t | ƒ } t j rk | j d ƒ j d ƒ } n  | S(   Ni   i   u   ;u   utf-8u
   iso-8859-1(   R   Rv   R   R   t   PY3t   encodet   decode(   R#   t   parsedRk   (    (    s+   ../Django//lib/python/django/test/client.pyt	   _get_path  s    
!	c         K  sy   t  | ƒ } i t d ƒ d 6|  j | ƒ d 6t | d t ƒpK t | d ƒ d 6t d ƒ d 6} | j | ƒ |  j |   S(	   u   Construct a GET request.u   text/html; charset=utf-8u   CONTENT_TYPEu	   PATH_INFOt   doseqi   u   QUERY_STRINGu   GETu   REQUEST_METHOD(   R   Rv   R…   R   R(   R   Rw   RG   (   R#   Rk   R_   t   extraR„   t   r(    (    s+   ../Django//lib/python/django/test/client.pyt   get  s    #c         K  s   |  j  | | ƒ } t | ƒ } i t | ƒ d 6| d 6|  j | ƒ d 6t | d ƒ d 6t d ƒ d 6t | ƒ d 6} | j | ƒ |  j |   S(	   u   Construct a POST request.u   CONTENT_LENGTHu   CONTENT_TYPEu	   PATH_INFOi   u   QUERY_STRINGu   POSTu   REQUEST_METHODu
   wsgi.input(	   R€   R   R-   R…   R   Rv   R   Rw   RG   (   R#   Rk   R_   Rn   R‡   t	   post_dataR„   Rˆ   (    (    s+   ../Django//lib/python/django/test/client.pyt   post  s    c         K  sy   t  | ƒ } i t d ƒ d 6|  j | ƒ d 6t | d t ƒpK t | d ƒ d 6t d ƒ d 6} | j | ƒ |  j |   S(	   u   Construct a HEAD request.u   text/html; charset=utf-8u   CONTENT_TYPEu	   PATH_INFOR†   i   u   QUERY_STRINGu   HEADu   REQUEST_METHOD(   R   Rv   R…   R   R(   R   Rw   RG   (   R#   Rk   R_   R‡   R„   Rˆ   (    (    s+   ../Django//lib/python/django/test/client.pyt   head+  s    #u    u   application/octet-streamc         K  s   |  j  d | | | |  S(   u   Construct an OPTIONS request.u   OPTIONS(   t   generic(   R#   Rk   R_   Rn   R‡   (    (    s+   ../Django//lib/python/django/test/client.pyt   options8  s    c         K  s   |  j  d | | | |  S(   u   Construct a PUT request.u   PUT(   R   (   R#   Rk   R_   Rn   R‡   (    (    s+   ../Django//lib/python/django/test/client.pyt   put=  s    c         K  s   |  j  d | | | |  S(   u   Construct a DELETE request.u   DELETE(   R   (   R#   Rk   R_   Rn   R‡   (    (    s+   ../Django//lib/python/django/test/client.pyt   deleteB  s    c         K  s©   t  | ƒ } t | t j ƒ } i |  j | ƒ d 6t | d ƒ d 6t | ƒ d 6} | r | j i t | ƒ d 6t | ƒ d 6t	 | ƒ d 6ƒ n  | j | ƒ |  j
 |   S(   Nu	   PATH_INFOi   u   QUERY_STRINGu   REQUEST_METHODu   CONTENT_LENGTHu   CONTENT_TYPEu
   wsgi.input(   R   R   R   RQ   R…   R   Rv   Rw   R-   R   RG   (   R#   t   methodRk   R_   Rn   R‡   R„   Rˆ   (    (    s+   ../Django//lib/python/django/test/client.pyR   G  s    	(   R.   R/   R0   R%   Rx   RG   R€   R…   R‰   Rz   R‹   RŒ   RŽ   R   R   R   (    (    (    s+   ../Django//lib/python/django/test/client.pyRo   Â   s   					t   Clientc           B  sÂ   e  Z d  Z e d „ Z d „  Z d „  Z e e ƒ Z d „  Z	 i  e d „ Z
 i  e e d „ Z i  e d „ Z d d	 e d
 „ Z d d	 e d „ Z d d	 e d „ Z d „  Z d „  Z d „  Z RS(   u¾  
    A class that can act as a client for testing purposes.

    It allows the user to compose GET and POST requests, and
    obtain the response that the server gave to those requests.
    The server Response objects are annotated with the details
    of the contexts and templates that were rendered during the
    process of serving the request.

    Client objects are stateful - they will retain cookie (and
    thus session) details for the lifetime of the Client instance.

    This is not intended as a replacement for Twill/Selenium or
    the like - it is here to allow testing against the
    contexts and templates produced by a view, rather than the
    HTML rendered to the end-user.
    c         K  s2   t  t |  ƒ j |   t | ƒ |  _ d  |  _ d  S(   N(   R9   R’   R%   R7   t   handlerR!   t   exc_info(   R#   R8   Rp   (    (    s+   ../Django//lib/python/django/test/client.pyR%   k  s    c         K  s   t  j ƒ  |  _ d S(   uF   
        Stores exceptions when they are generated by a view.
        N(   t   sysR”   (   R#   R;   (    (    s+   ../Django//lib/python/django/test/client.pyt   store_exc_infop  s    c         C  sS   d t  j k rO t t  j ƒ } |  j j t  j d ƒ } | rO | j | j	 ƒ Sn  i  S(   u8   
        Obtains the current session variables.
        u   django.contrib.sessionsN(
   R   t   INSTALLED_APPSR   t   SESSION_ENGINERq   R‰   t   SESSION_COOKIE_NAMER!   t   SessionStoreRd   (   R#   t   enginet   cookie(    (    s+   ../Django//lib/python/django/test/client.pyt   _sessionv  s    c         K  su  |  j  |   } i  } t t | ƒ } t j j | d d ƒt j |  j d d ƒzú y |  j | ƒ } Wn( t	 k
 r } | j
 d
 k r‘ ‚  q‘ n X|  j r¼ |  j } d	 |  _ t j | Œ  n  |  | _ | | _ | j d g  ƒ | _ | j d ƒ | _ | j r&t | j ƒ d k r&| j d | _ n  | j rE|  j j | j ƒ n  | SWd	 t j j d d ƒ t j d d ƒ Xd	 S(   u  
        The master request method. Composes the environment dictionary
        and passes to the handler, returning the result of the handler.
        Assumes defaults for the query environment, which can be overridden
        using the arguments to the request.
        t   dispatch_uidu   template-renderu   request-exceptionu   500.htmlu	   templatesu   contexti   i    N(   u   500.html(   Rx   R   RP   R   t   template_renderedR2   R   R–   R“   R   R:   R”   R!   R   t   reraiset   clientRG   R‰   t	   templatesRO   R-   Rq   Rw   R1   (   R#   RG   RF   R_   t   on_template_renderRH   t   eR”   (    (    s+   ../Django//lib/python/django/test/client.pyRG   ƒ  s4    
						c         K  s@   t  t |  ƒ j | d | | } | r< |  j | |  } n  | S(   u@   
        Requests a response from the server using GET.
        R_   (   R9   R’   R‰   t   _handle_redirects(   R#   Rk   R_   t   followR‡   RH   (    (    s+   ../Django//lib/python/django/test/client.pyR‰   Á  s    !c         K  sF   t  t |  ƒ j | d | d | | } | rB |  j | |  } n  | S(   uA   
        Requests a response from the server using POST.
        R_   Rn   (   R9   R’   R‹   R¥   (   R#   Rk   R_   Rn   R¦   R‡   RH   (    (    s+   ../Django//lib/python/django/test/client.pyR‹   Ê  s    'c         K  s@   t  t |  ƒ j | d | | } | r< |  j | |  } n  | S(   u@   
        Request a response from the server using HEAD.
        R_   (   R9   R’   RŒ   R¥   (   R#   Rk   R_   R¦   R‡   RH   (    (    s+   ../Django//lib/python/django/test/client.pyRŒ   Ô  s    !u    u   application/octet-streamc         K  sF   t  t |  ƒ j | d | d | | } | rB |  j | |  } n  | S(   uC   
        Request a response from the server using OPTIONS.
        R_   Rn   (   R9   R’   RŽ   R¥   (   R#   Rk   R_   Rn   R¦   R‡   RH   (    (    s+   ../Django//lib/python/django/test/client.pyRŽ   Ý  s
    c         K  sF   t  t |  ƒ j | d | d | | } | rB |  j | |  } n  | S(   u:   
        Send a resource to the server using PUT.
        R_   Rn   (   R9   R’   R   R¥   (   R#   Rk   R_   Rn   R¦   R‡   RH   (    (    s+   ../Django//lib/python/django/test/client.pyR   è  s
    c         K  sF   t  t |  ƒ j | d | d | | } | rB |  j | |  } n  | S(   u6   
        Send a DELETE request to the server.
        R_   Rn   (   R9   R’   R   R¥   (   R#   Rk   R_   Rn   R¦   R‡   RH   (    (    s+   ../Django//lib/python/django/test/client.pyR   ó  s
    c         K  sô   t  |   } | rì | j rì d t j k rì t t j ƒ } t ƒ  } |  j rZ |  j | _ n | j ƒ  | _ t	 | | ƒ | j j
 ƒ  t j } | j j |  j | <i d d 6d d 6t j d 6t j pÆ d d 6d d 6} |  j | j | ƒ t St Sd S(	   u  
        Sets the Factory to appear as if it has successfully logged into a site.

        Returns True if login is possible; False if the provided credentials
        are incorrect, or the user is inactive, or if the sessions framework is
        not available.
        u   django.contrib.sessionsu   max-ageu   /u   pathu   domainu   secureu   expiresN(   R   t	   is_activeR   R—   R   R˜   R   t   sessionRš   R   t   saveR™   t   session_keyRq   R!   t   SESSION_COOKIE_DOMAINt   SESSION_COOKIE_SECURERw   R(   R   (   R#   t   credentialst   userR›   RG   t   session_cookiet   cookie_data(    (    s+   ../Django//lib/python/django/test/client.pyR   þ  s*    			

c         C  sV   t  t j ƒ j ƒ  } |  j j t j ƒ } | rF | j d | j ƒ n  t	 ƒ  |  _ d S(   u‡   
        Removes the authenticated user's cookies and session object.

        Causes the authenticated user to be logged out.
        Rª   N(
   R   R   R˜   Rš   Rq   R‰   R™   R   Rd   R   (   R#   R¨   R¯   (    (    s+   ../Django//lib/python/django/test/client.pyt   logout&  s
    c         K  sú   g  | _  xê | j d k rõ | d } | j  } | j | | j f ƒ t | ƒ } | j ri | j | d <n  | j r‚ | j | d <n  | j r¡ t | j ƒ | d <n  |  j | j	 t
 | j ƒ d	 t | } | | _  | j  d
 | j  d d
 !k r Pq q W| S(   uH   Follows any redirects by requesting responses from the server using GET.i-  i.  i/  i3  u   Locationu   wsgi.url_schemeu   SERVER_NAMEu   SERVER_PORTR¦   iÿÿÿÿi    (   i-  i.  i/  i3  (   t   redirect_chaint   status_codeRK   R   t   schemet   hostnamet   portRv   R‰   Rk   R   t   queryR   (   R#   RH   R‡   t   urlR²   (    (    s+   ../Django//lib/python/django/test/client.pyR¥   2  s"    	
				'	(   R.   R/   R0   R   R%   R–   R   t   propertyR¨   RG   R‰   Rz   R‹   RŒ   RŽ   R   R   R   R±   R¥   (    (    (    s+   ../Django//lib/python/django/test/client.pyR’   Y  s&   		
	>			


	(	(   u   Clientu   RequestFactoryu   encode_fileu   encode_multipart(C   t
   __future__R    R•   Rj   t   reRg   R   t   ioR   t   urllib.parseR   R   R   t   ImportErrort   urllibR=   R   t   django.contrib.authR   R   t   django.core.handlers.baseR	   t   django.core.handlers.wsgiR
   t   django.core.signalsR   R   R   t	   django.dbR   t   django.httpR   R   R   t   django.templateR   t   django.testR   t   django.utils.functionalR   t   django.utils.encodingR   R   t   django.utils.httpR   t   django.utils.importlibR   t   django.utils.itercompatR   t   django.utilsR   t   django.test.utilsR   t   __all__R{   Rz   t   compileR|   t   objectR   R6   R7   RP   Rf   RZ   Ro   R’   (    (    (    s+   ../Django//lib/python/django/test/client.pyt   <module>   sN    
$	
&	
	-	—