
    g                        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 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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 d dl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)m*Z*m+Z+m,Z, d dl-m.Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z: dZ;dZ<de<z  Z= e:d      Z> e:d      Z? e@e
j                  e
j                  e
j                  e
j                  e
j                  g      ZF G d deG      ZH G d  d!e      ZId" ZJd# ZKd$ ZL G d% d&e      ZM G d' d(e      ZNd) ZOd* ZPd+ ZQ G d, d-      ZR G d. d/eR      ZS G d0 d1      ZT G d2 d3eTeR      ZU G d4 d5eTeS      ZVy)6    N)Iterable)copy)partial)
HTTPStatus)import_module)BytesIOIOBase)unquote_to_bytesurljoinurlparseurlsplit)sync_to_async)settings)ASGIRequest)BaseHandler)LimitedStreamWSGIRequest)DjangoJSONEncoder)got_request_exceptionrequest_finishedrequest_started)close_old_connections)HttpHeadersHttpRequest	QueryDictSimpleCookie)signals)ContextListresolve)force_bytes)SimpleLazyObject)	urlencode)_lazy_re_compile)AsyncClientAsyncRequestFactoryClientRedirectCycleErrorRequestFactoryencode_fileencode_multipartBoUnDaRyStRiNgz multipart/form-data; boundary=%sz.*; charset=([\w-]+);?z^application\/(.+\+)?jsonc                   "     e Zd ZdZ fdZ xZS )r(   z9The test client has been asked to follow a redirect loop.c                 T    t         |   |       || _        |j                  | _        y N)super__init__last_responseredirect_chain)selfmessager2   	__class__s      j/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/test/client.pyr1   zRedirectCycleError.__init__>   s&    !*+::    )__name__
__module____qualname____doc__r1   __classcell__r6   s   @r7   r(   r(   ;   s    C; ;r8   r(   c                   4    e Zd ZdZddZd Zd	dZd	dZd Zy)
FakePayloada  
    A wrapper around BytesIO that restricts what can be read since data from
    the network can't be sought 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.
    Nc                 f    t               | _        d| _        d| _        || j	                  |       y y )Nr   F)r   _FakePayload__content_FakePayload__lenread_startedwrite)r4   initial_bytess     r7   r1   zFakePayload.__init__L   s1     
!$JJ}% %r8   c                     | j                   S r/   )rC   )r4   s    r7   __len__zFakePayload.__len__S   s    zzr8   c                &   | j                   s"| j                  j                  d       d| _         |dk(  s|| j                  }| j                  |k\  sJ d       | j                  j	                  |      }| xj                  t        |      z  c_        |S Nr   TzFCannot read more than the available bytes from the HTTP incoming data.)rD   rB   seekrC   readlenr4   sizecontents      r7   rM   zFakePayload.readV   s      NN" $D2:::DJJ$	TS	T..%%d+

c'l"
r8   c                &   | j                   s"| j                  j                  d       d| _         |dk(  s|| j                  }| j                  |k\  sJ d       | j                  j	                  |      }| xj                  t        |      z  c_        |S rJ   )rD   rB   rL   rC   readlinerN   rO   s      r7   rS   zFakePayload.readlinec   s      NN" $D2:::DJJ$	TS	T..))$/

c'l"
r8   c                    | j                   rt        d      t        |      }| j                  j	                  |       | xj
                  t        |      z  c_        y )Nz.Unable to write a payload after it's been read)rD   
ValueErrorr!   rB   rE   rC   rN   )r4   brQ   s      r7   rE   zFakePayload.writep   sD    MNNa.W%

c'l"
r8   r/   )rK   )	r9   r:   r;   r<   r1   rH   rM   rS   rE    r8   r7   r@   r@   D   s     &#r8   r@   c              #     K   	 | E d {    t        j                  t                |        t        j                  t               y 7 ># t        j                  t                |        t        j                  t               w xY wwr/   r   
disconnectr   connect)iterablecloses     r7   closing_iterator_wrapperr^   x   s`     8##$9:  !67	 	##$9:  !67s+   BA
 AA
 :BA
 
;BBc                &  K   	 | 2 3 d {   }| 7 
6 	 t        j                  t                |        t        j                  t               y # t        j                  t                |        t        j                  t               w xY wwr/   rY   )r\   r]   chunks      r7   aclosing_iterator_wrapperra      sm     8# 	%K	8 	##$9:  !67 	##$9:  !67s5   BA A A :B;BBc                     d|j                   cxk  rdk  sn |j                   dv r|j                  rg |_        nd|_        | j                  dk(  r|j                  r	g |_        |S d|_        |S )z
    Simulate the behavior of most web servers by removing the content of
    responses for HEAD requests, 1xx, 204, and 304 responses. Ensure
    compliance with RFC 9112 Section 6.3.
    d      )   i0  r8   HEAD)status_code	streamingstreaming_contentrQ   method)requestresponses     r7   conditional_content_removalrm      sp     h""(S(H,@,@J,N)+H&"H~~)+H& O  #HOr8   c                   *     e Zd ZdZd fd	Zd Z xZS )ClientHandlerz
    An HTTP Handler that can be used for testing purposes. Use the WSGI
    interface to compose requests, but return the raw HttpResponse object with
    the originating WSGIRequest attached to its ``wsgi_request`` attribute.
    c                 2    || _         t        |   |i | y r/   enforce_csrf_checksr0   r1   r4   rr   argskwargsr6   s       r7   r1   zClientHandler.__init__       #6 $)&)r8   c                    | j                   | j                          t        j                  t               t        j
                  | j                  |       t        j                  t               t        |      }| j                   |_
        | j                  |      }t        ||       ||_        |j                  rZ|j                  r't!        |j"                  |j$                        |_        |S t'        |j"                  |j$                        |_        |S t)        j                  t               |j%                          t)        j                  t               |S )N)senderenviron)_middleware_chainload_middlewarer   rZ   r   sendr6   r[   r   rr   _dont_enforce_csrf_checksget_responserm   wsgi_requestrh   is_asyncra   ri   r]   r^   r   )r4   ry   rk   rl   s       r7   __call__zClientHandler.__call__   s    !!)  """#89DNNGD 56g&
 150H0H,H) $$W- 	$GX6 !(   -F...*  .F...* 	 ''(=>NN$$%:;r8   Tr9   r:   r;   r<   r1   r   r=   r>   s   @r7   ro   ro      s    *)r8   ro   c                   *     e Zd ZdZd fd	Zd Z xZS )AsyncClientHandlerz"An async version of ClientHandler.c                 2    || _         t        |   |i | y r/   rq   rs   s       r7   r1   zAsyncClientHandler.__init__   rv   r8   c           	        K   | j                   | j                  d       d|v r|j                  d      }nt        d      }t	        j
                  t               t	        j                  | j                  |       d {    t	        j                  t               t        |t        |t        |                  }| j                   |_        | j                  |       d {   }t!        ||       ||_        |j$                  rZ|j&                  r't)        |j*                  |j,                        |_        |S t/        |j*                  |j,                        |_        |S t1        j
                  t                t3        |j,                  d              d {    t1        j                  t               |S 7 97 7 $w)NT)r   
_body_file )rx   scopeF)thread_sensitive)rz   r{   popr@   r   rZ   r   asendr6   r[   r   r   rN   rr   r}   get_response_asyncrm   asgi_requestrh   r   ra   ri   r]   r^   r   r   )r4   r   	body_filerk   rl   s        r7   r   zAsyncClientHandler.__call__   sy     !!)  $ /5 		,/I#BI""#89##4>>GGG 56e]9c)n%MN 150H0H,H)0099#GX6 !(  -F...*  .F...* 	 ''(=>G-GIII$$%:;= 	H :& Js8   A=F??F8 A"F?"F;#B5F?F= F?;F?=F?r   r   r>   s   @r7   r   r      s    ,**r8   r   c                     | j                  dg       j                  |       d| vrt               | d<   | d   j                  t        |             y)z
    Store templates and contexts that are rendered.

    The context is copied so that it is an accurate representation at the time
    of rendering.
    	templatescontextN)
setdefaultappendr   r   )storesignalrx   templater   ru   s         r7   store_rendered_templatesr   	  sJ     
["%,,X6&=i	)DM*r8   c           	      2   g }d d }|j                         D ]  \  }}|t        d|z         ||      r|j                  t        | ||             ;t	        |t
              sat	        |t              rQ|D ]K  } ||      r|j                  t        | ||             (|j                  fdd| z  d|z  d|fD               M |j                  fdd| z  d|z  d|fD                |j                   d	| z        d
g       dj                  |      S )a+  
    Encode 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                 6    t        | t        j                        S r/   r!   r   DEFAULT_CHARSETss    r7   to_bytesz"encode_multipart.<locals>.to_bytes       1h6677r8   c                 H    t        | d      xr t        | j                        S )NrM   )hasattrcallablerM   )things    r7   is_filez!encode_multipart.<locals>.is_file$  s    uf%>(5::*>>r8   zeCannot encode None for key '%s' as POST data. Did you mean to pass an empty string or omit the value?c              3   .   K   | ]  } |        y wr/   rW   .0valr   s     r7   	<genexpr>z#encode_multipart.<locals>.<genexpr>7  s!      !$C !$   --%sz)Content-Disposition: form-data; name="%s"r   c              3   .   K   | ]  } |        y wr/   rW   r   s     r7   r   z#encode_multipart.<locals>.<genexpr>A  s!      C r   z--%s--r8   s   
)items	TypeErrorextendr*   
isinstancestrr   join)boundarydatalinesr   keyvalueitemr   s          @r7   r+   r+     s>    E8? jjl
U==?BC  U^LLXsE:;E3'Juh,G4=LLXsD!ABLL ! #X-G#M 	$! 	  LL  X%?#E	 / #B 
LLX()	
 <<r8   c                    d }t        |d      xr t        |j                  t              }|r)t        j
                  j                  |j                        nd}t        |d      r|j                  }n|rt        j                  |      d   }nd }|d}|xs |} |d| z         |d|d	|d
       |d|z        d ||j                               gS )Nc                 6    t        | t        j                        S r/   r   r   s    r7   r   zencode_file.<locals>.to_bytesU  r   r8   namer   content_typer   application/octet-streamr   z&Content-Disposition: form-data; name="z"; filename=""zContent-Type: %sr8   )r   r   r   r   ospathbasenamer   	mimetypes
guess_typerM   )r   r   filer   file_has_string_namefilenamer   s          r7   r*   r*   T  s    8
 #40OZ		35O.Brww		*Ht^$((	 ++H5a813H("#JMxX	
 	#l23 r8   c                       e Zd ZdZeddddZd Zd Zd Zd Z	d	 Z
	 dddddZded
fddddZ	 dddddZdddddZ	 	 	 dddddZ	 	 	 dddddZ	 	 	 dddddZ	 	 	 dddddZ	 	 	 dddddZy)r)   aV  
    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.
    N)json_encoderheadersquery_paramsc                    || _         || _        t               | _        t	               | _        |r.| j                  j                  t        j                  |             |rt        |d      | j                  d<   y y )NTdoseqQUERY_STRING)
r   defaultsr   cookiesr   errorsupdater   to_wsgi_namesr#   )r4   r   r   r   r   s        r7   r1   zRequestFactory.__init__  sb     ) #~iMM  !:!:7!CD,5l$,ODMM.) r8   c                     dj                  t        d | j                  j                         D                    ddddddd	d
dt	        d      | j
                  dddd| j                  |S )z5
        The base environment for a request.
        z; c              3   R   K   | ]  }|j                   d |j                   ! yw)=N)r   coded_valuer   morsels     r7   r   z/RequestFactory._base_environ.<locals>.<genexpr>  s'      "7  &zz6+=+=>"7s   %'/	127.0.0.1GETr   
testserver80zHTTP/1.1)   r   httpr8   TF)HTTP_COOKIE	PATH_INFOREMOTE_ADDRREQUEST_METHODSCRIPT_NAMESERVER_NAMESERVER_PORTSERVER_PROTOCOLzwsgi.versionwsgi.url_scheme
wsgi.inputzwsgi.errorszwsgi.multiprocesszwsgi.multithreadzwsgi.run_once)r   sortedr   valuesr@   r   r   r4   rk   s     r7   _base_environzRequestFactory._base_environ  s      99 "&,,"5"5"7  &#')"%%c*;;!% %")
* mm+
, -
 	
r8   c                 8    t         | j                  di |      S )#Construct a generic request object.rW   )r   r   r   s     r7   rk   zRequestFactory.request  s    -4--8899r8   c                     |t         u rt        t        |      S t        j	                  |      }|r|d   }nt
        j                  }t        ||      S )Nr   )encoding)MULTIPART_CONTENTr+   BOUNDARYCONTENT_TYPE_REmatchr   r   r!   )r4   r   r   r   charsets        r7   _encode_datazRequestFactory._encode_data  sN    ,,#Hd33 $)),7E("22tg66r8   c                     t         j                  |      xr t        |t        t        t
        f      }|r!t        j                  || j                        S |S )zu
        Return encoded JSON if data is a dict, list, or tuple and content_type
        is application/json.
        )cls)	JSON_CONTENT_TYPE_REr   r   dictlisttuplejsondumpsr   )r4   r   r   should_encodes       r7   _encode_jsonzRequestFactory._encode_json  sN    
 -22<@ 
Z4u%F
 ;Htzz$D$5$56QTQr8   c                     |j                   }|j                  r|d|j                  z   z  }t        |      }|j                  d      S )N;
iso-8859-1)r   paramsr
   decode)r4   parsedr   s      r7   	_get_pathzRequestFactory._get_path  s@    {{==C&--''D% {{<((r8   Fr   r   c                h    |r|rt        d      |xs |}|i n|} | j                  d|f|||d|S )zConstruct a GET request.7query_params and data arguments are mutually exclusive.r   securer   r   rU   genericr4   r   r   r  r   r   extras          r7   getzRequestFactory.get  s^     DVWW+|)1r|t||
 %
 
 	
r8   c                    | j                  |i n||      }| j                  ||      } | j                  d|||f|||d|S )zConstruct a POST request.POSTr  )r  r   r  )	r4   r   r   r   r  r   r   r  	post_datas	            r7   postzRequestFactory.post  sg       t||L%%dL9	t||		

 %	
 	
 		
r8   c                h    |r|rt        d      |xs |}|i n|} | j                  d|f|||d|S )zConstruct a HEAD request.r  rf   r  r  r  s          r7   headzRequestFactory.head  s^     DVWW+|)1r|t||
 %
 
 	
r8   c                2     | j                   d|f|||d|S )zConstruct a TRACE request.TRACEr  r  )r4   r   r  r   r   r  s         r7   tracezRequestFactory.trace  s5    t||
 %
 
 	
r8   c                6     | j                   d|||f|||d|S )zConstruct an OPTIONS request.OPTIONSr  r  r4   r   r   r   r  r   r   r  s           r7   optionszRequestFactory.options  s=     t||		

 %	
 	
 		
r8   c                Z    | j                  ||      } | j                  d|||f|||d|S )zConstruct a PUT request.PUTr  r  r  r"  s           r7   putzRequestFactory.put5  sO       |4t||		

 %	
 	
 		
r8   c                Z    | j                  ||      } | j                  d|||f|||d|S )zConstruct a PATCH request.PATCHr  r&  r"  s           r7   patchzRequestFactory.patchM  sO       |4t||		

 %	
 	
 		
r8   c                Z    | j                  ||      } | j                  d|||f|||d|S )zConstruct a DELETE request.DELETEr  r&  r"  s           r7   deletezRequestFactory.deletee  sO       |4t||		

 %	
 	
 		
r8   c                $   t        t        |            }	t        |t        j                        }| j                  |	      ||rdnd|rdndd}
|r0|
j                  t        t        |            |t        |      d       |r$|j                  t        j                  |             |rt        |d      |d	<   |
j                  |       |
j                  d	      s'|	d
   j                         j                  d      }||
d	<    | j                  di |
S )$Construct an arbitrary HTTP request.443r   httpsr   )r   r   r   r   )CONTENT_LENGTHCONTENT_TYPEr   Tr   r      r  rW   )r   r   r!   r   r   r  r   rN   r@   r   r   r#   r  encoder	  rk   )r4   rj   r   r   r   r  r   r   r  r
  rquery_strings               r7   r  zRequestFactory.generic}  s     #d)$4!9!9:/$$*5*0wf	
 HH&)#d)n$0"-d"3 LL227;<$-l$$GE.!	uu^$!!9++-44\BL ,Ant|| a  r8   )NF)Fr   r   F)r9   r:   r;   r<   r   r1   r   rk   r   r  r  r  r   r  r  r  r#  r'  r*  r-  r  rW   r8   r7   r)   r)   r  s     'P"
B:
7R	) ',
8<4
( &
 
6 ',
8<4
"	
4d 	
 /
 
4 /
 
6 /
 
6 /
 
8 /'! '!r8   r)   c                   2    e Zd ZdZd Zd Z	 	 	 dddddZy)r&   a  
    Class that lets you create mock ASGI-like Request objects for use in
    testing. Usage:

    rf = AsyncRequestFactory()
    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,
    including synchronous ones. The reason we have a separate class here is:
    a) this makes ASGIRequest subclasses, and
    b) AsyncClient can subclass it.
    c           
          ddiddddgdddg d	| j                   |}|d
   j                  ddj                  t        d | j                  j                         D                    f       |S )zThe base scope for a request.versionz3.0r   z1.1r   r   )r   r   r   )asgitypehttp_versionclientserverschemerj   r   r   s   cookies   ; c              3   p   K   | ].  }|j                   d |j                  j                  d       0 yw)r   asciiN)r   r   r5  r   s     r7   r   z2AsyncRequestFactory._base_scope.<locals>.<genexpr>  s2      &;F %+JJ0B0BCKKGT&;s   46)r   r   r   r   r   r   )r4   rk   r   s      r7   _base_scopezAsyncRequestFactory._base_scope  s     &!"A&*
 mm
 
 	i

 &*ll&9&9&; 
	
 r8   c           	          d|v r|j                  d      }nt        d      }t         | j                  di |t	        |t        |                  S )r   r   r   rW   )r   r@   r   rD  r   rN   )r4   rk   r   s      r7   rk   zAsyncRequestFactory.request  sP    
 7"L1I#BID'w'y#i.)Q
 	
r8   Nr  c                   t        t        |            }	t        |t        j                        }|| j                  |	      d|rdndf|rdnddgd}
|rX|
d   j                  d	t        t        |            j                  d
      fd|j                  d
      fg       t        |      |
d<   |rt        |d      |
d<   n"|j                  dd      x}r||
d<   n|	d   |
d<   |r$|j                  t        j                  |             |
dxx   |j                         D cg c]5  \  }}|j!                         j                  d
      |j                  d      f7 c}}z  cc<    | j"                  di |
S c c}}w )r/  r   r0  r   r1  r   )s   hosts
   testserver)rj   r   r@  rA  r   r   s   content-lengthrC  s   content-typer   Tr   r7  r   Nr4  latin1rW   )r   r   r!   r   r   r  r   rN   r5  r@   r#   r   r   r   to_asgi_namesr   lowerrk   )r4   rj   r   r   r   r  r   r   r  r
  r   r7  r   r   s                 r7   r  zAsyncRequestFactory.generic  sx    #d)$4!9!9:NN6*"VE>!'gV01
 iL&CI(=(=g(FG$l&9&9'&BC *$/AlO ),d CAn"YY~t<<\< ,An !'q	AnLL227;<	)#kkm
+
U YY[(%,,x*@A+
 	
 t|| a  	
s   :E4r8  )r9   r:   r;   r<   rD  rk   r  rW   r8   r7   r&   r&     s.    <
$ /,! ,!r8   r&   c                       e Zd ZdZd Zd Zed        Zd Zd Z	d Z
dd	Zdd
Zd ZddZddZd Zd Zd Zd ZddddddZd Zy)ClientMixinzC
    Mixin with common methods between Client and AsyncClient.
    c                 6    t        j                         | _        y)z3Store exceptions when they are generated by a view.N)sysexc_info)r4   ru   s     r7   store_exc_infozClientMixin.store_exc_info  s    r8   c                     | j                   |_         | j                   r&| j                   \  }}}d| _         | j                  r|yy)z
        Look for a signaled exception, clear the current context exception
        data, re-raise the signaled exception, and clear the signaled exception
        from the local cache.
        N)rN  raise_request_exception)r4   rl   _	exc_values       r7   check_exceptionzClientMixin.check_exception  sE     !MM=="mmOAy! DM++ , r8   c                 R   t        t        j                        }| j                  j	                  t        j
                        }|r|j                  |j                        S |j                         }|j                          |j                  | j                  t        j
                  <   |S )z%Return the current session variables.)
r   r   SESSION_ENGINEr   r  SESSION_COOKIE_NAMESessionStorer   savesession_keyr4   enginecookiesessions       r7   r^  zClientMixin.session'  s|     x667!!(">">?&&v||44%%'5<5H5HX112r8   c                 n  K   t        t        j                        }| j                  j	                  t        j
                        }|r|j                  |j                        S |j                         }|j                          d {    |j                  | j                  t        j
                  <   |S 7 -wr/   )
r   r   rV  r   r  rW  rX  r   asaverZ  r[  s       r7   asessionzClientMixin.asession3  s     x667!!(">">?&&v||44%%'mmo5<5H5HX112 	s   BB5B3.B5c                 H    ddl m}  |di |}|r| j                  |       yy)z
        Set the Factory to appear as if it has successfully logged into a site.

        Return True if login is possible or False if the provided credentials
        are incorrect.
        r   )authenticateTFrW   )django.contrib.authrc  _login)r4   credentialsrc  users       r7   loginzClientMixin.login=  s)     	5*k*KKr8   c                 x   K   ddl m}  |di | d{   }|r| j                  |       d{    yy7 !7 w)zSee login().r   )aauthenticateNTFrW   )rd  rj  _alogin)r4   rf  rj  rg  s       r7   aloginzClientMixin.aloginL  s=     5"1[11,,t$$$	 2$s   :6:8::Nc                 Z    || j                         }||_        | j                  ||       y r/   )_get_backendbackendre  r4   rg  ro  s      r7   force_loginzClientMixin.force_loginV  s*    ?'')GD'"r8   c                 v   K   || j                         }||_        | j                  ||       d {    y 7 wr/   )rn  ro  rk  rp  s      r7   aforce_loginzClientMixin.aforce_login\  s4     ?'')Gll4)))s   /979c                 j    ddl m} t        j                  D ]  } ||      }t	        |d      s|c S  y )Nr   )load_backendget_user)rd  ru  r   AUTHENTICATION_BACKENDSr   )r4   ru  backend_pathro  s       r7   rn  zClientMixin._get_backendb  s/    4$<<L"<0Gw
+## =r8   c                 &   ddl m} t               }| j                  r| j                  |_        n.t	        t
        j                        }|j                         |_         ||||       |j                  j                          | j                  |       y )Nr   )rh  )
rd  rh  r   r^  r   r   rV  rX  rY  _set_login_cookies)r4   rg  ro  rh  rk   r\  s         r7   re  zClientMixin._loginj  sj    - -<<"llGO"8#:#:;F$113GOgtW%(r8   c                 b  K   ddl m} t               }| j                          d {   }|r||_        n.t        t        j                        }|j                         |_         ||||       d {    |j                  j                          d {    | j                  |       y 7 7 :7 w)Nr   )rl  )rd  rl  r   ra  r^  r   r   rV  rX  r`  rz  )r4   rg  ro  rl  rk   r^  r\  s          r7   rk  zClientMixin._aloginy  s     . -'%GO"8#:#:;F$113GOWdG,,,oo##%%%( ( 	-%s4   $B/B)A	B/0B+1!B/B-B/+B/-B/c                     t         j                  }|j                  j                  | j                  |<   d dt         j
                  t         j                  xs d d d}| j                  |   j                  |       y )Nr   )zmax-ager   domainr  expires)r   rW  r^  rZ  r   SESSION_COOKIE_DOMAINSESSION_COOKIE_SECUREr   )r4   rk   session_cookiecookie_datas       r7   rz  zClientMixin._set_login_cookies  se    !55'.'B'B^$4444<
 	^$++K8r8   c                    ddl m}m} t               }| j                  r| j                  |_         ||      |_        n.t        t        j                        }|j                         |_         ||       t               | _        y)z<Log out the user by removing the cookies and session object.r   )rv  logoutN)rd  rv  r  r   r^  rg  r   r   rV  rX  r   r   )r4   rv  r  rk   r\  s        r7   r  zClientMixin.logout  sZ    8-<<"llGO#G,GL"8#:#:;F$113GOw#~r8   c                 D  K   ddl m}m} t               }| j	                          d{   }|r||_         ||       d{   |_        n.t        t        j                        }|j                         |_         ||       d{    t               | _        y7 q7 Z7 w)zSee logout().r   )	aget_useralogoutN)rd  r  r  r   ra  r^  rg  r   r   rV  rX  r   r   )r4   r  r  rk   r^  r\  s         r7   r  zClientMixin.alogout  s     :-'%GO!*7!33GL"8#:#:;F$113GOg#~ ( 4 	s4   &B BB BAB BB B B c                 0   t        |d      st        j                  |j                  d            st	        d|j                  d      z        t        j                  |j                  j                  |j                        fi ||_
        |j                  S )N_jsonzContent-Typez3Content-Type header is "%s", not "application/json")r   r   r   r  rU   r  loadsrQ   r	  r   r  )r4   rl   r  s      r7   _parse_jsonzClientMixin._parse_json  s    x)'--hll>.JK Ill>23  "ZZ  ''(8(89=BHN ~~r8   r   r   r   r   r   c          	         |j                   }|j                  }|j                  ||j                  f       t	        |      }	|	j
                  r|	j
                  |d<   |	j                  r|	j                  |d<   |	j                  |d<   |	j                  rt        |	j                        |d<   |	j                  }
|
s|	j                  rd}
|
j                  d      st        |j                  d   |
      }
|j                  t        j                  t        j                   fv r=|j                  d   j#                         }|dvr|	j$                  |d	<   t'        | |      }n#| j(                  }t+        |	j$                        }d
} ||
f||d||d|S )z9Follow a single redirect contained in response using GET.r   r   	HTTP_HOSTr   r   r   r   )r  r  r   NF)r   r   followr   r   )urlr3   r   rg   r   rA  hostnameportr   r   netloc
startswithr   rk   r   TEMPORARY_REDIRECTPERMANENT_REDIRECTrI  querygetattrr  r   )r4   rl   r   r   r   r   r  response_urlr3   r  r   request_methods               r7   _follow_redirectzClientMixin._follow_redirect  st     ||!00|X-A-ABC|$::'*zzE#$<<#&<<E- !$E+88#&sxx=E- xx

Ds#8++K8$?D))))$
 
 &--.>?EEGN_4(+		n%$T>:N!XXNSYY'DL
%%
 
 	
r8   c                     |j                   }|d   |dd v rt        d|      t        |      dkD  rt        d|      y)zU
        Raise a RedirectCycleError if response contains too many redirects.
        rK   NzRedirect loop detected.)r2      zToo many redirects.)r3   r(   rN   )r4   rl   r3   s      r7   _ensure_redirects_not_cyclicz(ClientMixin._ensure_redirects_not_cyclic  sU     "00"!44 %%>hWW~# %%:(SS	 $r8   r/   )r9   r:   r;   r<   rO  rT  propertyr^  ra  rh  rl  rq  rs  rn  re  rk  rz  r  r  r  r  r  rW   r8   r7   rK  rK    s{    '  	 	#*$))"9&&
  7
rTr8   rK  c                        e Zd ZdZ	 	 dddd fdZd Z	 	 	 dddd fdZdeddfddd fdZ	 	 	 dddd fd	Z		 	 	 	 dddd fd
Z
	 	 	 	 dddd fdZ	 	 	 	 dddd fdZ	 	 	 	 dddd fdZ	 	 	 dddd fdZ	 	 	 	 ddZ xZS )r'   a  
    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.
    FNr  c                    t        |   d||d| t        |      | _        || _        d | _        d | _        d | _        y Nr  rW   )r0   r1   ro   handlerrQ  rN  r  r   r4   rr   rQ  r   r   r   r6   s         r7   r1   zClient.__init__  sF     	P|PxP$%89'>$
r8   c                 
    | j                   di }i }t        t        |      }dt              z  }t        j
                  j                  ||       dt              z  }t        j                  | j                  |       	 | j                  |      }t        j
                  j                  |       t        j                  |       | j                  |       | |_        |_        |j                  dg       |_        |j                  d      |_        t        | j"                  |      |_        t'        |j(                  dd      t+        fd      |_        |j                   r,t/        |j                         d	k(  r|j                   d
   |_        |j0                  r%| j0                  j3                  |j0                         |S # t        j
                  j                  |       t        j                  |       w xY w)a  
        Make a generic request. Compose the environment dictionary and pass
        to the handler, return the result of the handler. Assume defaults for
        the query environment, which can be overridden using the arguments to
        the request.
        template-render-%sdispatch_uidrequest-exception-%sr   r   urlconfNc                  $    t         d         S )Nr   r  r   rk   r  s   r7   <lambda>z Client.request.<locals>.<lambda>O  s    GGK0'Br8   r   r   rW   )r   r   r   idr   template_renderedr[   r   rO  r  rZ   rT  r?  rk   r  r   r   r  r  r  r   r"   resolver_matchrN   r   r   )	r4   rk   ry   r   on_template_render
signal_uidexception_uidrl   r  s	    `      @r7   rk   zClient.request,  s    %$$$/w/ $%=tD)BwK7
!!))*<:)V.G<%%d&9&9V	I||G,H%%00j0I!,,-HX&"!XXk2688I. 0 0(;(//DA"2B#
 H$4$4 5 :'//2HLL 0 011 %%00j0I!,,-Hs   G
 
8Hc                    || _         || _        t        	|   |f||||d|}|r | j                  |f|||d|}|S )-Request a response from the server using GET.r   r  r   r   r   r   r   )r  r   r0   r  _handle_redirects
r4   r   r   r  r  r   r   r  rl   r6   s
            r7   r  z
Client.get[  st     
7;
%
 
 -t--#W<SXH r8   c          	          || _         || _        t        
|   |f|||||d|}	|r | j                  |	f||||d|}	|	S ).Request a response from the server using POST.r   r   r  r   r   r  )r  r   r0   r  r  r4   r   r   r   r  r  r   r   r  rl   r6   s             r7   r  zClient.postw  s~     
7<
%%
 
 -t--)) H r8   c                    || _         || _        t        	|   |f||||d|}|r | j                  |f|||d|}|S ).Request a response from the server using HEAD.r  r  )r  r   r0   r  r  r  s
            r7   r  zClient.head  st     
7<
%
 
 -t--#W<SXH r8   c          	          || _         || _        t        
|   |f|||||d|}	|r | j                  |	f||||d|}	|	S )1Request a response from the server using OPTIONS.r  r  )r  r   r0   r#  r  r  s             r7   r#  zClient.options  s~     
7?
%%
 
 -t--)) H r8   c          	          || _         || _        t        
|   |f|||||d|}	|r | j                  |	f||||d|}	|	S )(Send a resource to the server using PUT.r  r  )r  r   r0   r'  r  r  s             r7   r'  z
Client.put  s~     
7;
%%
 
 -t--)) H r8   c          	          || _         || _        t        
|   |f|||||d|}	|r | j                  |	f||||d|}	|	S )*Send a resource to the server using PATCH.r  r  )r  r   r0   r*  r  r  s             r7   r*  zClient.patch  s~     
7=
%%
 
 -t--)) H r8   c          	          || _         || _        t        
|   |f|||||d|}	|r | j                  |	f||||d|}	|	S )$Send a DELETE request to the server.r  r  )r  r   r0   r-  r  r  s             r7   r-  zClient.delete  s~     
7>
%%
 
 -t--)) H r8   c                    || _         || _        t        	|   |f||||d|}|r | j                  |f|||d|}|S )#Send a TRACE request to the server.r  r  )r  r   r0   r  r  r  s
            r7   r  zClient.traceB  st     
7=
%
 
 -t--#W<SXH r8   c                     g |_         |j                  t        v rO|j                   } | j                  |f||||d|}||_         | j	                  |       |j                  t        v rO|S )Y
        Follow any redirects by requesting responses from the server using GET.
        r  r3   rg   REDIRECT_STATUS_CODESr  r  r4   rl   r   r   r   r   r  r3   s           r7   r  zClient._handle_redirects^  s     #%""&;;%44N,t,,)) H '5H#--h7 ""&;; r8   FTNFFr   r   FFr   FFr   r   NN)r9   r:   r;   r<   r1   rk   r  r   r  r  r#  r'  r*  r-  r  r  r=   r>   s   @r7   r'   r'   	  s7   ( " $
  -d  > &! !L  > /! !L /! !L /! !L /! !L  > r8   r'   c                        e Zd ZdZ	 	 dddd fdZd Z	 	 	 dddd fdZdeddfddd fdZ	 	 	 dddd fd	Z		 	 	 	 dddd fd
Z
	 	 	 	 dddd fdZ	 	 	 	 dddd fdZ	 	 	 	 dddd fdZ	 	 	 dddd fdZ	 	 	 	 ddZ xZS )r%   z
    An async version of Client that creates ASGIRequests and calls through an
    async request path.

    Does not currently support "follow" on its methods.
    FNr  c                    t        |   d||d| t        |      | _        || _        d | _        d | _        d | _        y r  )r0   r1   r   r  rQ  rN  r  r   r  s         r7   r1   zAsyncClient.__init__  sF     	P|PxP)*=>'>$
r8   c                 (  K    | j                   di }i }t        t        |      }dt              z  }t        j
                  j                  ||       dt              z  }t        j                  | j                  |       	 | j                  |       d{   }t        j
                  j                  |       t        j                  |       | j                  |       | |_        |_        |j                  dg       |_        |j                  d      |_        t        | j"                  |      |_        t'        |j(                  dd      t+        fd      |_        |j                   r,t/        |j                         d	k(  r|j                   d
   |_        |j0                  r%| j0                  j3                  |j0                         |S 7 8# t        j
                  j                  |       t        j                  |       w xY ww)a   
        Make a generic request. Compose the scope dictionary and pass to the
        handler, return the result of the handler. Assume defaults for the
        query environment, which can be overridden using the arguments to the
        request.
        r  r  r  Nr   r   r  c                  $    t         d         S )Nr   r  r   r  s   r7   r  z%AsyncClient.request.<locals>.<lambda>  s    GGFOW=r8   r   r   rW   )rD  r   r   r  r   r  r[   r   rO  r  rZ   rT  r?  rk   r  r   r   r  r  r  r   r"   r  rN   r   r   )	r4   rk   r   r   r  r  r  rl   r  s	    `      @r7   rk   zAsyncClient.request  s     !  +7+ $%=tD)BwK7
!!))*<:)V.G<%%d&9&9V	I!\\%00H%%00j0I!,,-HX&"!XXk2688I. 0 0(;(//DA"2=#
 H$4$4 5 :'//2HLL 0 015 1%%00j0I!,,-Hs1   BHG GG !D3HG 8HHc                   K   || _         || _        t        	|   |f||||d| d{   }|r | j                  |f|||d| d{   }|S 7 '7 w)r  r  Nr  )r  r   r0   r  _ahandle_redirectsr  s
            r7   r  zAsyncClient.get  s      

%
 
 
 4T44#W<SX H 
!   'AA AAAAc          	         K   || _         || _        t        
|   |f|||||d| d{   }	|r  | j                  |	f||||d| d{   }	|	S 7 (7 w)r  r  Nr  )r  r   r0   r  r  r  s             r7   r  zAsyncClient.post  s      

%%
 
 
 4T44))  H %
!   (AA!AAAAc                   K   || _         || _        t        	|   |f||||d| d{   }|r | j                  |f|||d| d{   }|S 7 '7 w)r  r  Nr  )r  r   r0   r  r  r  s
            r7   r  zAsyncClient.head  s      

%
 
 
 4T44#W<SX H 
r  c          	         K   || _         || _        t        
|   |f|||||d| d{   }	|r  | j                  |	f||||d| d{   }	|	S 7 (7 w)r  r  Nr  )r  r   r0   r#  r  r  s             r7   r#  zAsyncClient.options  s      

%%
 
 
 4T44))  H %
r  c          	         K   || _         || _        t        
|   |f|||||d| d{   }	|r  | j                  |	f||||d| d{   }	|	S 7 (7 w)r  r  Nr  )r  r   r0   r'  r  r  s             r7   r'  zAsyncClient.put>  s      

%%
 
 
 4T44))  H %
r  c          	         K   || _         || _        t        
|   |f|||||d| d{   }	|r  | j                  |	f||||d| d{   }	|	S 7 (7 w)r  r  Nr  )r  r   r0   r*  r  r  s             r7   r*  zAsyncClient.patcha  s      

%%
 
 
 4T44))  H %
r  c          	         K   || _         || _        t        
|   |f|||||d| d{   }	|r  | j                  |	f||||d| d{   }	|	S 7 (7 w)r  r  Nr  )r  r   r0   r-  r  r  s             r7   r-  zAsyncClient.delete  s      

%%
 
 
 4T44))  H %
r  c                   K   || _         || _        t        	|   |f||||d| d{   }|r | j                  |f|||d| d{   }|S 7 '7 w)r  r  Nr  )r  r   r0   r  r  r  s
            r7   r  zAsyncClient.trace  s      

%
 
 
 4T44#W<SX H 
r  c                    K   g |_         |j                  t        v rW|j                   } | j                  |f||||d| d{   }||_         | j	                  |       |j                  t        v rW|S 7 1w)r  r  Nr  r  s           r7   r  zAsyncClient._ahandle_redirects  s      #%""&;;%44N2T22))  H '5H#--h7 ""&;; s   AA7A5.A73A7r  r  r  r  r  )r9   r:   r;   r<   r1   rk   r  r   r  r  r#  r'  r*  r-  r  r  r=   r>   s   @r7   r%   r%   z  s7    " $
  ,b  > &! !L  > /! !L /! !L /! !L /! !L  > r8   r%   )Wr  r   r   rM  collections.abcr   r   	functoolsr   r   r   	importlibr   ior   r	   urllib.parser
   r   r   r   asgiref.syncr   django.confr   django.core.handlers.asgir   django.core.handlers.baser   django.core.handlers.wsgir   r   django.core.serializers.jsonr   django.core.signalsr   r   r   	django.dbr   django.httpr   r   r   r   django.testr   django.test.utilsr   django.urlsr    django.utils.encodingr!   django.utils.functionalr"   django.utils.httpr#   django.utils.regex_helperr$   __all__r   r   r   r   	frozensetMOVED_PERMANENTLYFOUND	SEE_OTHERr  r  r  	Exceptionr(   r@   r^   ra   rm   ro   r   r   r+   r*   r)   r&   rK  r'   r%   rW   r8   r7   <module>r      sk     	 
 $    #  F F &   1 1 @ : X X + I I  )  - 4 ' 6 6A "#<='(DE !$$%%%% ; ;1#& 1#h88&4K 4n1 1h
+;|<r! r!j	g!. g!TuT uTpn[. nbb+2 br8   