o
    tPfr                     @   s  d dl mZ d dlmZ e  d dlmZ d dlmZ d dl	Z	d dl
Z
d dlmZ d dlZd dlZd dlmZ d dlmZ d dlZd dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ G dd deeZdZej Z dddZ!G dd de"Z#dd Z$dd Z%G dd de&Z'e'j(Z)G dd dej*Z*G dd de&Z+G dd  d e+e*Z,G d!d" d"e,Z-G d#d$ d$e,Z.G d%d& d&e.Z/G d'd( d(e,Z0G d)d* d*e+e*Z1G d+d, d,e+e*Z2G d-d. d.e*Z3G d/d0 d0e*Z4G d1d2 d2e*Z5G d3d4 d4e*Z6G d5d6 d6e6Z7G d7d8 d8e*Z8G d9d: d:e*Z9G d;d< d<e*Z:G d=d> d>e*Z;G d?d@ d@e*Z<d dl=Ze>ej?dAZ@e@rAG dBdC dCe<ZAG dDdE dEe<ZBe@rUG dFdG dGeAeBZCG dHdI dIe*ZDG dJdK dKe*ZEG dLdM dMeEZFG dNdO dOe*ZGG dPdQ dQeGZHG dRdS dSeGZIG dTdU dUe*ZJG dVdW dWe*ZKG dXdY dYeKZLG dZd[ d[e*ZMG d\d] d]e*ZNG d^d_ d_e*ZOG d`da dae*ZPG dbdc dce*ZQG ddde dee*ZRG dfdg dgeMZSG dhdi die*ZTG djdk dke*ZUG dldm dme*ZVG dndo doe*ZWG dpdq dqe*ZXG drds dse*ZYG dtdu due*ZZG dvdw dwe*Z[G dxdy dye*Z\G dzd{ d{e*Z]dd|d}Z^G d~d dej_Z`G dd de*ZaG dd de*ZbG dd de*ZceddkrVee  dS dS )    )print_function)monkey)contextmanager)parse_qsN)BytesIO)	validator)PY3PYPY)ExpectedException)socket)pywsgi)Inputc                   @      e Zd ZdZdS )ExpectedAssertionErrorzAn expected assertion errorN)__name__
__module____qualname____doc__ r   r   Z/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/tests/test__pywsgi.pyr   .       r   Content-LengthOKInternal Server Error)     c                   @      e Zd ZdS )ConnectionClosedNr   r   r   r   r   r   r   r   :       r   c                 C   s   |   }|st|d}i }	 |    }|s	 ||fS |d}z
|dd\}}W n   td|f   | dd |D vsLJ d|||f |||< q)	Nlatin-1Tz:    zFailed to split: %rc                 S   s   h | ]}|  qS r   )lower).0xr   r   r   	<setcomp>N   s    zread_headers.<locals>.<setcomp>z$Header %r:%r sent more than once: %r)readliner   decodestripsplitprintr"   )fdresponse_lineheaderslinekeyvaluer   r   r   read_headers>   s&   
	
(r1   c                 c   sx    	 |   }| }t|d}|dkr$| d}|dks"J t|d S | |}|V  | d}|dks;J t|q)NT   r         
)r&   r(   intreadrepr)r+   r.   
chunk_sizecrlfdatar   r   r   iread_chunksS   s   



r;   c                   @   s^   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
e		dddZdS )Responsec                 C   s   || _ || _d | _d| _z4|d d dd\}}| _t|| _|d\}| _|dks2J t	|| jdv s>J t	| jW d S  t
yN   td|   w )	NF r3   /HTTP)1.01.1z	Error: %r)status_liner-   bodychunksr)   reasonr5   codeversionr7   	Exceptionr*   )selfrC   r-   rH   rG   r@   r   r   r   __init__d   s   
zResponse.__init__c                 c   s    | j V  | jV  | jV  d S N)rC   r-   rD   rJ   r   r   r   __iter__s   s   zResponse.__iter__c                 C   s"   | j j| j| j| j| jf}d| S )Nz0<%s status_line=%r headers=%r body=%r chunks=%r>)	__class__r   rC   r-   rD   rE   )rJ   argsr   r   r   __str__x   s   zResponse.__str__c                 C   sN   t |dr| j|v sJ d| j|| f d S | j|ks%J d| j|| f d S )N__contains__z$Unexpected code: %r (expected %r)
%s)hasattrrG   )rJ   rG   r   r   r   
assertCode|   s   
""zResponse.assertCodec                 C   "   | j |ksJ d| j || f d S )Nz&Unexpected reason: %r (expected %r)
%s)rF   )rJ   rF   r   r   r   assertReason      "zResponse.assertReasonc                 C   rU   )Nz'Unexpected version: %r (expected %r)
%s)rH   )rJ   rH   r   r   r   assertVersion   rW   zResponse.assertVersionc                 C   s.   | j |d}||ksJ d|||| f d S )NFz)Unexpected header %r: %r (expected %r)
%s)r-   get)rJ   headerr0   
real_valuer   r   r   assertHeader   s   
zResponse.assertHeaderc                 C   s:   t |trtr|d}| j|ksJ d| j|| f d S )Nasciiz$Unexpected body: %r (expected %r)
%s)
isinstancestrr   encoderD   )rJ   rD   r   r   r   
assertBody   s   
"zResponse.assertBodyr   defaultrB   Nc                 C   s2  t |\}}	| ||	}
|dur|
| |dkrt|}|dur&|
| |dur/|
| |
jdkr6|
S |durIt|trCt	|}|

d| d|	ddv rht|	v rYtd tt||
_d	|
j|
_nt|	v ryt|	t }|||
_n| |
_|dur|
| |dur||
jksJ ||
jf|
S )
zo
        Read an HTTP response, optionally perform assertions,
        and return the Response object.
        Nrb   d   r   chunkedTransfer-Encoding zhWARNING: server used chunked transfer-encoding despite having Content-Length header (libevent 1.x's bug)    )r1   rT   REASONSrY   rV   rX   rG   r^   r5   r_   r\   CONTENT_LENGTHr*   listr;   rE   joinrD   r6   ra   )clsr+   rG   rF   rH   rD   rE   content_length_status_liner-   rJ   numr   r   r   r6      s<   








zResponse.read)r   rb   rB   NNN)r   r   r   rK   rN   rQ   rT   rV   rX   r\   ra   classmethodr6   r   r   r   r   r<   b   s    r<   c                       s   e Zd ZdZeeZdZejZ	ej
ZG dd dejZdd Zdd Zdd	 Zejr7ejr7d
d Z fddZdd Zedd Zedd Zdd ZdZi ZdddZ  ZS )TestCaseNc                   @   s   e Zd ZeZdS )zTestCase.handler_classN)r   r   r   r   ApplicationErrorr   r   r   r   handler_class   r   rs   c                 C   s"   dd l }|d}||j |S )Nr   zgevent.tests.pywsgi)logging	getLoggersetLevelCRITICAL)rJ   rt   loggerr   r   r   init_logger   s   
zTestCase.init_loggerc                 C   s0   |    }| _tj| jdf|||| jd| _d S )Nr   )log	error_logrs   )ry   rx   r   
WSGIServerlisten_addrrs   server)rJ   applicationrx   r   r   r   init_server   s   zTestCase.init_serverc                 C   sj   | j }| jd ur| |}| | | j  | jjs#td | jjr| jj| _| js-J tj	
|  d S )NzWaiting on server port)r   r   r   r~   startserver_portr*   port	greentestrq   setUprJ   r   r   r   r   r      s   





zTestCase.setUpc                 C   s   | j t| |S rL   )close_on_teardownappendweakrefref)rJ   resourcer   r   r   _close_on_teardown   s   zTestCase._close_on_teardownc                    s$   dd | j D | _ tt|   d S )Nc                 S   s   g | ]
}| d ur| qS rL   r   )r#   rr   r   r   
<listcomp>   s    z5TestCase._tearDownCloseOnTearDown.<locals>.<listcomp>)r   superrq   _tearDownCloseOnTearDownrM   rO   r   r   r      s   z!TestCase._tearDownCloseOnTearDownc                 C   sx   t j|  | jd ur&tjd | j  W d    n1 s!w   Y  d | _t jr:dd l	}|
  |
  d S d S )N      ?r   )r   rq   tearDownr~   geventTimeout	start_newstopr	   gccollect)rJ   r   r   r   r   r      s   
zTestCase.tearDownc                 #   sl    t jjf  }tr& jfddG  fdddt}| }z
|V  W |  d S |  w )Nc                     sh   d|v r| d|d< d|v r| i |S d|d< | i |}|j  fdd}||_| |S )Nbufsize	bufferingmoderwbc                    s   t | tr
| d}  | S Nr]   )r^   r_   r`   )r:   _rconn_writer   r   write  s   

z1TestCase.connect.<locals>.makefile.<locals>.write)popr   r   )rP   kwargsrconnr   )conn_makefilerJ   r   r   makefile  s   
z"TestCase.connect.<locals>.makefilec                       s   e Zd Z fddZdS )zTestCase.connect.<locals>.proxyc                    s   |dkrS t  |S )Nr   )getattr)rJ   nameconnr   r   r   __getattribute__#  s   
z0TestCase.connect.<locals>.proxy.__getattribute__N)r   r   r   r   r   r   r   r   proxy"      r   )r   create_connectionconnect_addrr   r   r   objectclose)rJ   resultr   r   )r   r   r   rJ   r   connect  s   zTestCase.connectc              	   c   sZ    |   }z|jdd}|V  W |  n|  w W d    d S 1 s&w   Y  d S )Nr!   r   )r   r   r   )rJ   sockr   r   r   r   r   -  s   
"zTestCase.makefilec              	   O   s   |   6}|jdd}|d t|g|R i |W  d    W  d    S 1 s-w   Y  W d    d S 1 s=w   Y  d S )Nr!   r   #GET / HTTP/1.1
Host: localhost

)r   r   r   	read_http)rJ   rP   r   r   r+   r   r   r   urlopen6  s   

"zTestCase.urlopenrB   GETr?   c                 K   s^   | j  }|| |}ddd | D }|r|d n|}d}|t||| j|d }|S )N
c                 s   s    | ]}d | V  qdS )z%s: %sNr   )r#   itemr   r   r   	<genexpr>D  s    z*TestCase.format_request.<locals>.<genexpr>zE%(method)s %(path)s HTTP/%(http_ver)s
Host: localhost
%(headers)s
)methodpathhttp_verr-   )DEFAULT_EXTRA_CLIENT_HEADERScopyupdaterk   itemsdictHTTP_CLIENT_VERSION)rJ   r   r   r-   def_headersr   r   r   r   format_request?  s   

zTestCase.format_request)r   r?   )r   r   r   r~   staticmethodr   r   r   DEFAULT_BIND_ADDRr}   DEFAULT_LOCAL_HOST_ADDRr   r   WSGIHandlerrs   ry   r   r   CPYTHONPY2r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   rq      s,    	
&
rq   c                   @   sB   e Zd ZdZdZdZdd Zdd Zdd Ze	
d	d
d ZdS )CommonTestMixinr   Fc                 C   s   |   `}||   t|dd}|jddkr,| | jd |d fW  d    S | | j | j	r;|
dd || jdd t|d	d
dd}||   t|dd}||fW  d    S 1 sgw   Y  d S )Nhello worldrD   
Connectionr   ,Server closed connection, not expecting that
keep-alive	/notexistr     	Not Found	not foundrG   rF   rD   )r   r   r   r   r-   rY   
assertTrueEXPECT_CLOSEassertFalseEXPECT_KEEPALIVEr\   )rJ   r+   responsedne_responser   r   r   
test_basicZ  s    
$zCommonTestMixin.test_basicc                 C   s   t d}|  k}||  | jdd  t|dd ztjjd|d}zt|dd	d
d W |  n|  w W n  | j	yD   Y n t yZ } z||urP W Y d }~nd }~ww W d    d S W d    d S W d    d S 1 svw   Y  d S )Nz@HTTP pipelining not supported; the second request is thrown awayr   r   r   r   r   )	exceptionr   r   r   r   )
AssertionErrorr   r   r   r   r   r   r   r   PIPELINE_NOT_SUPPORTED_EXS)rJ   r   r+   timeoutexr   r   r   test_pipelinem  s.   
"zCommonTestMixin.test_pipelinec                 C   s   |   }||   t|}|jddkr(| | jd 	 W d    d S | | j | j	r7|
dd || jdd t| ||   z| }W n tjyl } z|jd tvrb W Y d }~nd }~ww | |d|f  W d    d S W d    d S 1 sw   Y  d S )Nr   r   r   r   )r   r   zCThe remote side is expected to close the connection, but it sent %r)r   r   r   r   r-   rY   r   r   r   r   r\   r&   r   errorrP   CONN_ABORTED_ERRORS)rJ   r+   r   r   r   r   r   r   test_connection_close  s<   
"z%CommonTestMixin.test_connection_closezNot surec                 C   s   g }t dD ]}|d qd|}|  #}d| }|| | }|dd }| |d W d    d S 1 s=w   Y  d S )Ni  r   r?   z%GET /%s HTTP/1.0
Host: localhost

r>   r!   414)ranger   rk   r   r   r&   r)   assertEqual)rJ   
path_parts_r   r+   requestr   statusr   r   r   test_006_reject_long_urls  s   


"z)CommonTestMixin.test_006_reject_long_urlsN)r   r   r   r   r   r   r   r   r   unittestskipr   r   r   r   r   r   U  s    r   c                       s>   e Zd ZdZdZdddZdd Z fddZd	d
 Z  Z	S )TestNoChunksNTc                 C   s.   |r|  | jd d S | | jd  d S )Nwsgi.input_terminated)r   last_environrY   r   )rJ   input_terminatedr   r   r   _check_environ  s   zTestNoChunks._check_environc                 C   sd   || _ |d }|dkr|ddg ddgS |dkr(|ddd	g}|d
 ddgS |ddg ddgS )N	PATH_INFOr?   200 OKzContent-Typez
text/plain   hello    world
/websocketz101 Switching Protocols)r   r   rg   s   upgrading tos	   websocket404 Not Found   not    found)r   )rJ   envstart_responser   r   r   r   r   r     s   zTestNoChunks.applicationc                    sZ   t t|  \}}|   | |j |dd |d ur+| |j |dd d S d S )Nr   119)r   r   r   r   r   rE   r\   )rJ   r   r   r   r   r   r     s   zTestNoChunks.test_basicc                 C   sn   |   }|| jdd t|dddd}W d    n1 s w   Y  | |j |   |dd d S )	Nr   r   r   r   r   r   r   r  )r   r   r   r   r   rE   r   r\   rJ   r+   r   r   r   r   test_dne  s   
zTestNoChunks.test_dne)T)
r   r   r   r   r   r   r   r   r  r   r   r   r   r   r     s    
	r   c                   @   s   e Zd Zdd Zdd ZdS )TestConnectionUpgradesc                 C   d   |   }|| jddd t|dd}W d    n1 sw   Y  | jdd | |j d S )Nr   upgrade)r   r   e   rG   Fr   r   r   r   r   r   r   rE   r  r   r   r   test_connection_upgrade     
z.TestConnectionUpgrades.test_connection_upgradec                 C   r
  )Nr   	websocket)r   Upgrader  r  Fr  r  r  r   r   r   test_upgrade_websocket  r  z-TestConnectionUpgrades.test_upgrade_websocketN)r   r   r   r  r  r   r   r   r   r	    s    r	  c                   @   s   e Zd ZdZefZdZdS )TestNoChunks10rA   TN)r   r   r   r   r   r   r   r   r   r   r   r    s    r  c                   @   s   e Zd ZddiZdZdZdS )TestNoChunks10KeepAliver   r   FTN)r   r   r   r   r   r   r   r   r   r   r    s
    r  c                   @      e Zd Zdd ZdS )TestExplicitContentLengthc                 C   sR   || _ | |d |d }|dkr|dddg ddgS |d	dd
g ddgS )Nr   r   r?   r   r   )r   r  r   r   r   )r   r  r  r  )r   r   rY   )rJ   r  r  r   r   r   r   r     s   z%TestExplicitContentLength.applicationNr   r   r   r   r   r   r   r   r    s    r  c                   @      e Zd Zedd ZdS )	TestYieldc                 c   s>    | d }|dkr|ddg dV  d S |ddg dV  d S )Nr   r?   r   r   s   hello worldr   	   not foundr   r  r  r   r   r   r   r   
  s   

zTestYield.applicationNr   r   r   r   r   r   r   r   r   r        r  c                   @   s   e Zd ZdZedd ZdS )TestBytearrayNc                 C   sB   | d }|dkr|ddg t dt dgS |ddg t dgS )	Nr   r?   r   r   r   r   r   r  	bytearrayr  r   r   r   r     s   
zTestBytearray.application)r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @       e Zd Zedd Zdd ZdS )TestMultiLineHeaderc                 C   s"   d| d v sJ |ddg dgS )Nztest.submitCONTENT_TYPEr   r   s   okr   r  r  r   r   r   r   $  s   zTestMultiLineHeader.applicationc                 C   sJ   d d}|  }|| t| W d   dS 1 sw   Y  dS )z
issue #116r   )POST / HTTP/1.0Host: localhostz9Content-Type: multipart/related; boundary="====XXXX====";z$ type="text/xml";start="test.submit"zContent-Length: 0rf   rf   Nrk   r   r   r   rJ   r   r+   r   r   r   test_multiline_116*  s
   



"z&TestMultiLineHeader.test_multiline_116N)r   r   r   r   r   r+  r   r   r   r   r$  #  s    
r$  c                   @   r#  )
TestGetArgc                 C   sR   | d  d}tr|d}t|ddgd }|ddg d	||f dgS )
N
wsgi.input   r]   ar!   r   r   r   za is %s, body is %s)r6   r   r'   r   rY   r`   )r  r  rD   r/  r   r   r   r   :  s   
zTestGetArg.applicationc                 C   sX   d d}|  }|| |d t|dd W d    d S 1 s%w   Y  d S )Nr   )r'  r(  zContent-Length: 3rf   za=a01234567890123456789za is a, body is a=ar   r)  r*  r   r   r   test_007_get_argC  s   



"zTestGetArg.test_007_get_argN)r   r   r   r   r   r1  r   r   r   r   r,  8  s    
r,  c                   @   s4   e Zd ZdZdd Zdd ZdZdd Zd	d
 ZdS )TestCloseIterNc                 C   s   |ddg | S Nr   r   r   rJ   r  r  r   r   r   r   Y  s   zTestCloseIter.applicationc                 c   s    t dV  dV  d S )N   Hello World   !r!  rM   r   r   r   rN   ]  s   

zTestCloseIter.__iter__Fc                 C   s   |  j d7  _ d S )Nr!   )closedrM   r   r   r   r   c     zTestCloseIter.closec                 C   s\   d| _ |  }|d t|dddgd W d    n1 s w   Y  | | j d d S )NFr   s   Hello World!r5  r6  rD   rE   r!   )r7  r   r   r   r   rJ   r+   r   r   r   test_close_is_calledf  s   

z"TestCloseIter.test_close_is_called)	r   r   r   r   r   rN   r7  r   r;  r   r   r   r   r2  T  s    r2  c                   @   s4   e Zd Zg dZdd Zdd Zdd Zdd	 Zd
S )TestChunkedApp)s   thiss   iss   chunkedc                 C   s   d | jS )Nrg   )rk   rE   rM   r   r   r   rD   r     zTestChunkedApp.bodyc                 c   s4    |  |d |ddg | jD ]}|V  qd S )Nr   r   r   )r   rY   rE   )rJ   r  r  chunkr   r   r   r   u  s   
zTestChunkedApp.applicationc                 C   sd   |   }|d t||  d d}W d    n1 sw   Y  |dd | |j| j d S )N6GET / HTTP/1.1
Host: localhost
Connection: close

r9  re   rd   )r   r   r   rD   r\   r   rE   r  r   r   r   test_chunked_response{  s   

z$TestChunkedApp.test_chunked_responsec                 C   s   |   }|d t|}W d    n1 sw   Y  | |j|   | |jdd  |jd}|d urH| |tt|   d S d S )Nz6GET / HTTP/1.0
Host: localhost
Connection: close

re   r   )	r   r   r   r   rD   r-   rY   r_   len)rJ   r+   r   rm   r   r   r   test_no_chunked_http_1_0  s   


z'TestChunkedApp.test_no_chunked_http_1_0N)r   r   r   rE   rD   r   r@  rB  r   r   r   r   r<  n  s    r<  c                   @   s   e Zd Zdd gd ZdS )TestBigChunks   ai    r.  N)r   r   r   rE   r   r   r   r   rC    r   rC  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestNegativeReadc                 C   s@   |  |d |ddg |d dkr|d d}|gS d S )Nr   r   r   r   z/readr-  )r   rY   r6   )rJ   r  r  r:   r   r   r   r     s   zTestNegativeRead.applicationc                 C   H   d}|   }|| t|dd W d    d S 1 sw   Y  d S )Nsl   POST /read HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2
oh
4
 hai
0

oh hair   r   r   r   rJ   r:   r+   r   r   r   test_negative_chunked_read  
   

"z+TestNegativeRead.test_negative_chunked_readc                 C   rG  )NsT   POST /read HTTP/1.1
Host: localhost
Connection: close
Content-Length: 6

oh hairH  r   rI  rJ  r   r   r   test_negative_nonchunked_read  rL  z.TestNegativeRead.test_negative_nonchunked_readN)r   r   r   r   rK  rM  r   r   r   r   rE    s    rE  c                   @   s,   e Zd ZdZedd Zdd Zdd ZdS )TestNegativeReadlineNc                 C   s0   |ddg | d dkr| d  d}|gS d S )Nr   r   r   z	/readliner-  rF  )r&   )r  r  r:   r   r   r   r     s
   z TestNegativeReadline.applicationc                 C   rG  )Nsp   POST /readline HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2
oh
4
 hai
0

rH  r   rI  rJ  r   r   r   test_negative_chunked_readline  rL  z3TestNegativeReadline.test_negative_chunked_readlinec                 C   rG  )NsX   POST /readline HTTP/1.1
Host: localhost
Connection: close
Content-Length: 6

oh hairH  r   rI  rJ  r   r   r   !test_negative_nonchunked_readline  rL  z6TestNegativeReadline.test_negative_nonchunked_readline)r   r   r   r   r   r   rO  rP  r   r   r   r   rN    s    
rN  c                       s   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zeddd Zeddd Zeddd Zeddd Zeddd Zeddd Z  ZS )TestChunkedPostr   c                    s   t    d| _d S )Nr   )r   r   callsrM   r   r   r   r        

zTestChunkedPost.setUpc                    s   |  j d7  _ |  d |ddg  d dkr% d d}|gS  d d	kr8tt fd
dd}|S  d dkrItt fdddS d d dgS )Nr!   r   r   r   r   z/ar-     z/bc                          d  dS )Nr-  rT  r6   r   r  r   r   <lambda>      z-TestChunkedPost.application.<locals>.<lambda>rg   z/cc                      rU  )Nr-  r!   rV  r   rW  r   r   rX    rY  s   We should not get herer]   )rR  r   rY   r6   rj   iterr`   )rJ   r  r  r:   linesr   rW  r   r     s   zTestChunkedPost.applicationc                 C   s   d}|   }|| t|dd W d    n1 sw   Y  |   }||dd t|dd W d    n1 s?w   Y  |   }||dd t|dd W d    d S 1 scw   Y  d S )Nsi   POST /a HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2
oh
4
 hai
0

rH  r   s   /as   /bs   /c)r   r   r   replacerJ  r   r   r   test_014_chunked_post  s   



"z%TestChunkedPost.test_014_chunked_postc                 C   rG  )Nst   POST /a HTTP/1.1
Host: localhost
Connection: close
Content-Length: 12
Transfer-Encoding: chunked

{"hi": "ho"}  r  rI  rJ  r   r   r   #test_229_incorrect_chunk_no_newline  s
   

"z3TestChunkedPost.test_229_incorrect_chunk_no_newlinec                 C   rG  )Nsv   POST /a HTTP/1.1
Host: localhost
Connection: close
Content-Length: 12
Transfer-Encoding: chunked

{"hi": "ho"}
r^  r  rI  rJ  r   r   r    test_229_incorrect_chunk_non_hex  s
   

"z0TestChunkedPost.test_229_incorrect_chunk_non_hexc                 C   rG  )Nsw   POST /a HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2;token="oh hi"
oh
4
 hai
0

rH  r   rI  rJ  r   r   r   !test_229_correct_chunk_quoted_ext
  rL  z1TestChunkedPost.test_229_correct_chunk_quoted_extc                 C   rG  )Nu   POST /a HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2;token=oh_hi
oh
4
 hai
0

rH  r   rI  rJ  r   r   r    test_229_correct_chunk_token_ext  rL  z0TestChunkedPost.test_229_correct_chunk_token_extc                 C   sX   d}| ddd }|  }|| t|dd W d    d S 1 s%w   Y  d S )Nrb  s   oh_his   _oh_hii  r^  r  )r\  r   r   r   rJ  r   r   r   +test_229_incorrect_chunk_token_ext_too_long  s   

"z;TestChunkedPost.test_229_incorrect_chunk_token_ext_too_longzMaybe hangsc                 C   sn   d}d}|   }|| t|dd || t|dd W d    n1 s)w   Y  | | jd d S )Ns   POST /a HTTP/1.1
Host: localhost
Connection: keep-alive
Transfer-Encoding: chunked

2
oh
4
 hai
0
trailer1: value1
trailer2: value2

sg   POST /a HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2
oh
4
 bye
0
rH  r   oh byer3   r   r   r   r   rR  )rJ   data1data2r+   r   r   r   test_trailers_keepalive_ignored&  s   



z/TestChunkedPost.test_trailers_keepalive_ignoredc              	   C   s   d}|   5}|| t|dd | t t| W d    n1 s&w   Y  W d    d S W d    d S 1 s>w   Y  d S )Ns   POST /a HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2
oh
4
 hai
0
GETpath2a:123 HTTP/1.1
Host: a.com
Connection: close

rH  r   )r   r   r   assertRaisesr   rJ  r   r   r   test_trailers_close_ignoredJ  s   


"z+TestChunkedPost.test_trailers_close_ignoredc              	   C   s   d}|dt j 7 }|  7}|| t|dd | t t|dd W d    n1 s/w   Y  W d    d S W d    d S 1 sGw   Y  d S )Ns|   POST /a HTTP/1.1
Host: localhost
Connection: keep-alive
Transfer-Encoding: chunked

2
oh
4
 hai
0
trailer2: value2   trH  r   re  )r   MAX_REQUEST_LINEr   r   r   rj  r   rJ  r   r   r   test_trailers_too_longc  s   

"z&TestChunkedPost.test_trailers_too_longc              	   C   s   d}|   ,}|| t|dd | t t| W d    n1 s&w   Y  W d    n1 s5w   Y  | | jd d S )Ns   POST /a HTTP/1.1
Host: localhost
Connection: keep-alive
Transfer-Encoding: chunked

2
oh
4
 hai
0GET /path2?a=:123 HTTP/1.1
Host: a.com
Connection: close

rH  r   r!   r   r   r   rj  r   r   rR  rJ  r   r   r   =test_trailers_request_smuggling_missing_last_chunk_keep_alive|  s   	


zMTestChunkedPost.test_trailers_request_smuggling_missing_last_chunk_keep_alivec              	   C   s   d}|   .}|| t|dd | t t|dd W d    n1 s(w   Y  W d    n1 s7w   Y  | | jd d S )Ns   POST /a HTTP/1.1
Host: localhost
Connection: keep-alive
Transfer-Encoding: chunked

2
oh
4
 hai
0
Header: value
GET /path2?a=:123 HTTP/1.1
Host: a.com
Connection: close

rH  r   r^  r  r!   ro  rJ  r   r   r   ,test_trailers_request_smuggling_header_first  s   

z<TestChunkedPost.test_trailers_request_smuggling_header_firstc                 C   s`   d}|   }|| t|dd t|dd W d    n1 s"w   Y  | | jd d S )Ns   POST /a HTTP/1.1
Host: localhost
Connection: keep-alive
Transfer-Encoding: chunked

2
oh
4
 hai
0

Header: valueGET /path2?a=:123 HTTP/1.1
Host: a.com
Connection: close

rH  r   r^  r  r!   rf  rJ  r   r   r   >test_trailers_request_smuggling_request_terminates_then_header  s   

zNTestChunkedPost.test_trailers_request_smuggling_request_terminates_then_header)r   r   r   rR  r   r   r]  r_  r`  ra  rc  rd  r   skipOnWindowsri  rk  rn  rp  rq  rr  r   r   r   r   r   rQ    s,    
#


 
rQ  c                   @   sD   e Zd ZdZdZeeee Zdd Zdd Z	dd Z
d	d
 ZdS )TestUseWrites   abcdes   endc                 C   s   |d dkr|ddd| j fg}|| j | jgS |d dkr.|ddg}|| j | jgS |d dkrH|ddg}|| j || j | jgS td)	Nr   z/explicit-content-lengthr   r   r   z/no-content-lengthz/no-content-length-twicezInvalid url)rm   rD   rI   end)rJ   r  r  r   r   r   r   r     s    



zTestUseWrite.applicationc                 C   sd   |   }|d t|| j| j d}W d    n1 sw   Y  |d| j |dd d S )NzMGET /explicit-content-length HTTP/1.1
Host: localhost
Connection: close

r   r   re   F)r   r   r   rD   ru  r\   rm   r  r   r   r   test_explicit_content_length  s   

z)TestUseWrite.test_explicit_content_lengthc                 C   sb   |   }|d t|| j| j d}W d    n1 sw   Y  |dd |dd d S )NzGGET /no-content-length HTTP/1.1
Host: localhost
Connection: close

r   r   Fre   rd   )r   r   r   rD   ru  r\   r  r   r   r   test_no_content_length  s   

z#TestUseWrite.test_no_content_lengthc                 C   s   |   }|d t|| j| j | j d}W d    n1 s!w   Y  |dd |dd | |j| j| j| jg d S )NzMGET /no-content-length-twice HTTP/1.1
Host: localhost
Connection: close

r   r   Fre   rd   )r   r   r   rD   ru  r\   r   rE   r  r   r   r   test_no_content_length_twice  s   

z)TestUseWrite.test_no_content_length_twiceN)r   r   r   rD   ru  r_   rA  rm   r   rv  rw  rx  r   r   r   r   rt    s    rt  c                   @   sR   e Zd ZejejedZejejedZ	dd Z
dddZd	d
 ZdS )HttpsTestCaseztest_server.crtztest_server.keyc                 C   s"   t j| jdf|| j| jd| _d S )Nr   certfilekeyfile)r   r|   r}   r{  r|  r~   r   r   r   r   r     s   zHttpsTestCase.init_serverr   Nc              
   K   s$  dd l }|  }||b}|jddL}|d|  |d ur;|dt|  || |dd u r:||d< n|d |  t|fi |W  d    W  d    W  d    S 1 sdw   Y  W d    n1 ssw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr   r!   r   z %s / HTTP/1.1
Host: localhost
zContent-Length: %s

rD   r   )	sslr   wrap_socketr   r   rA  rY   flushr   )rJ   r   	post_bodyr   r}  raw_sockr   r+   r   r   r   r   
  s,   


"zHttpsTestCase.urlopenc                 C   s4   |d dksJ |d |ddg |d  dgS )Nzwsgi.url_schemehttpsr   r   r-  
   rV  rJ   environr  r   r   r   r     s   zHttpsTestCase.application)r   N)r   r   r   osr   rk   dirname__file__r{  r|  r   r   r   r   r   r   r   ry    s    
ry  create_default_contextc                   @   r  )HttpsSslContextTestCasec                 C   s@   ddl m} | }|j| j| jd tj| jdf||d| _d S )Nr   )_create_unverified_contextrz  )ssl_context)	
gevent.sslr  load_cert_chainr{  r|  r   r|   r}   r~   )rJ   r   r  contextr   r   r   r   &  s   z#HttpsSslContextTestCase.init_serverN)r   r   r   r   r   r   r   r   r  %      r  c                   @   s*   e Zd Zeedrdd Zdd ZdS dS )	TestHttpsr}  c                 C   s    | j ddd}| |jd d S )NPOSTabc)r   r  r   r   rD   rJ   r   r   r   r   test_012_ssl_server8     zTestHttps.test_012_ssl_serverc                 C   s   |   }| |jd d S )Nrf   r  r  r   r   r   test_013_empty_return<  s   zTestHttps.test_013_empty_returnN)r   r   r   rS   r   r  r  r   r   r   r   r  4  s
    
r  c                   @   r   )TestHttpsWithContextNr   r   r   r   r   r  A  r   r  c                   @       e Zd ZdZdd Zdd ZdS )TestInternationalNc                 C   sB   d}t r	|d}| |d | | |d d |ddg g S )Ns   /приветr    r   QUERY_STRINGzC%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81=%D0%BE%D1%82%D0%B2%D0%B5%D1%82
200 PASSEDr   )r   r'   r   )rJ   r  r  
path_bytesr   r   r   r   G  s   
zTestInternational.applicationc              	   C   s   |   7}|ddd | }t|ddddd W d    n1 s&w   Y  W d    d S W d    d S 1 s>w   Y  d S )	Ns   GET /%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82?%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81=%D0%BE%D1%82%D0%B2%D0%B5%D1%82 HTTP/1.1
Host: localhost
Connection: close

   
r4   PASSEDFrf   r   )rF   rE   rD   rm   )r   sendallr\  r   r   )rJ   r   r+   r   r   r   testR  s   

"zTestInternational.testr   r   r   r   r   r  r   r   r   r   r  D  s    r  c                       sH   e Zd ZdZdZdZeZ fddZ fddZ	dd	 Z
d
d Z  ZS )"TestNonLatin1HeaderFromApplicationFNs   Ὂ3c                    s   t t|   g | _d S rL   )r   r  r   errorsrM   r   r   r   r   e  s   
z(TestNonLatin1HeaderFromApplication.setUpc                    s   g | _ tt|   d S rL   )r  r   r  r   rM   r   r   r   r   i  s   z+TestNonLatin1HeaderFromApplication.tearDownc                 C   s<   z|ddd| j fg W g S    | jt d d   )Nr  r   zCustom-Headerr3   )rZ   r  r   sysexc_infor  r   r   r   r   m  s   z.TestNonLatin1HeaderFromApplication.applicationc              	   C   s   |   f}|   |d | @}| jr;t|ddd | jtjd | 	t
| jd | jd \}}| |t nt|dd	d | 	t
| jd W d    n1 sUw   Y  W d    d S W d    d S 1 smw   Y  d S )
Ns   GET / HTTP/1.1

r   r   )rG   rF   )
where_typer!   r   r   r  )r   expect_one_errorr  r   should_errorr   assert_errorr   SecureEnvironr   rA  r  assertIsInstanceUnicodeError)rJ   r   r+   r   vr   r   r   r  x  s"   


"z'TestNonLatin1HeaderFromApplication.test)r   r   r   error_fatalr   rZ   r   r  r   r   r   r  r   r   r   r   r   r  ^  s    r  c                   @   s   e Zd ZdZdZdS ))TestNonLatin1UnicodeHeaderFromApplicationu   Ὂ3TN)r   r   r   rZ   r  r   r   r   r   r    s    r  c                   @   r  )TestInputReadlineNc                 C   sb   |d }g }	 |  }|sntr|dn|}|t|d  q|dg  tr/dd |D S |S )Nr-  Tr]   r>   	200 helloc                 S      g | ]}| d qS r]   r`   r#   lr   r   r   r         z1TestInputReadline.application.<locals>.<listcomp>)r&   r   r'   r   r7   rJ   r  r  inputr[  r.   r   r   r   r     s   
zTestInputReadline.applicationc                 C   s^   |   !}d}|dt||f  |  t|ddd W d    d S 1 s(w   Y  d S )Nzhello

world
123zMPOST / HTTP/1.1
Host: localhost
Connection: close
Content-Length: %s

%shelloz'hello\n' '\n' 'world\n' '123' )rF   rD   )r   r   rA  r  r   )rJ   r+   contentr   r   r   r    s   

"zTestInputReadline.testr  r   r   r   r   r    s    r  c                   @   r  )TestInputIterc                 C   sb   |d }g }|D ]}|s nt r|dn|}|t|d  q|dg  t r/dd |D S |S )Nr-  r]   r>   r  c                 S   r  r  r  r  r   r   r   r     r  z-TestInputIter.application.<locals>.<listcomp>)r   r'   r   r7   r  r   r   r   r     s   
zTestInputIter.applicationNr  r   r   r   r   r        r  c                   @   r  )TestInputReadlinesc                 C   sH   |d }dd |  D }dd |D }|dg  tr"dd |D S |S )Nr-  c                 S   s   g | ]}t r|d n|qS r  )r   r'   r  r   r   r   r     s    z2TestInputReadlines.application.<locals>.<listcomp>c                 S   s   g | ]}t |d  qS )r>   )r7   )r#   r.   r   r   r   r     s    r  c                 S   r  r  r  r  r   r   r   r     r  )	readlinesr   )rJ   r  r  r  r[  r   r   r   r     s
   
zTestInputReadlines.applicationNr  r   r   r   r   r    r  r  c                   @   r  )
TestInputNNc                 C   s   |d  d |dg  g S )Nr-     r   rV  r  r   r   r   r     s   
zTestInputN.applicationc                 C      |    d S rL   r   rM   r   r   r   r    r=  zTestInputN.testr  r   r   r   r   r    s    r  c                   @   s&   e Zd Ze ZdZdd Zdd ZdS )TestErrorInApplicationFc                 C   s   t d| _| j)NzTestError.applicationr   r
   r   r4  r   r   r   r     s   z"TestErrorInApplication.applicationc                 C   s(   |    | jdd | tj| j d S )Nr   r  )r  r   r  r   r
   r   rM   r   r   r   r    s   zTestErrorInApplication.testN)r   r   r   r   r   r  r   r  r   r   r   r   r    s
    r  c                   @   r  )TestError_after_start_responsec                 C   s   t d| _|ddg | j)N*TestError_after_start_response.applicationr   r   r  r4  r   r   r   r     s   r  Nr  r   r   r   r   r    r  r  c                   @   r#  )TestEmptyYieldc                 c   s    |ddg dV  dV  d S Nr   r   rg   r   r&  r   r   r   r        
zTestEmptyYield.applicationc                 C   sb   g }|   }|d t|d|d | }W d    n1 s!w   Y  | |dd|  d S )Nr?  rf   r9  rg   zgot garbage: %rr   r   r   r6   r   rJ   rE   r+   garbager   r   r   test_err  s   


zTestEmptyYield.test_errNr   r   r   r   r   r  r   r   r   r   r        
r  c                   @   r#  )TestFirstEmptyYieldc                 c   s    |ddg dV  dV  d S )Nr   r   rg      hellor   r&  r   r   r   r     r  zTestFirstEmptyYield.applicationc                 C   s^   dg}|   }|d t|d|d | }W d    n1 s"w   Y  | |d d S )Nr  r?  r  r9  rg   r  r  r   r   r   r    s   


zTestFirstEmptyYield.test_errNr  r   r   r   r   r     r  r  c                   @   r#  )TestEmptyYield304c                 c   s    |dg  dV  dV  d S )N304 Not modifiedrg   r   r&  r   r   r   r     s   

zTestEmptyYield304.applicationc                 C   sZ   |   }|d t|dddd | }W d    n1 s w   Y  | |d d S )Nr?  i0  rf   F)rG   rD   rE   rg   r  )rJ   r+   r  r   r   r   r    s   


zTestEmptyYield304.test_errNr  r   r   r   r   r    r  r  c                   @   r  )TestContentLength304Nc              
   C   sL   z|ddg W t d t y% } z|dg  |jW  Y d }~S d }~ww )Nr  )r   100z
200 Raisedz)start_response did not fail but it should)r   rP   )rJ   r  r  r   r   r   r   r   '  s   
z TestContentLength304.applicationc                 C   s`   d}|   }|d t|dd|dd | }W d    n1 s#w   Y  | |d d S )NzGInvalid Content-Length for 304 response: '100' (must be absent or zero)r?  r   RaisedF)rG   rF   rD   rE   rg   r  )rJ   rD   r+   r  r   r   r   r  /  s   


zTestContentLength304.test_errr   r   r   r   r   r  r   r   r   r   r  $  s    r  c                   @   r  )TestBody304Nc                 C   s   |dg  dgS )Nr  s   bodyr   r4  r   r   r   r   <  s   
zTestBody304.applicationc              	   C   s   |   2}|d | t}t| W d    n1 sw   Y  |j}| t|d W d    d S 1 s9w   Y  d S )Nr?  "The 304 response must have no body)r   r   rj  r   r   r   r   r_   rJ   r+   excr   r   r   r   r  @  s   


"zTestBody304.test_errr  r   r   r   r   r  9  s    r  c                   @   s$   e Zd ZdZdZdd Zdd ZdS )TestWrite304Nc              
   C   sH   |dg }d| _ z|d W d S  ty# } zd| _ t|j d }~ww )Nr  FrD   T)error_raisedr   r   rP   )rJ   r  r  r   r   r   r   r   r   M  s   

zTestWrite304.applicationc              	   C   s   |   )}|d | t}t| W d    n1 sw   Y  |j}W d    n1 s0w   Y  | t|d | | j	d d S )Ns6   GET / HTTP/1.1
Host: localhost
Connection: close

r  zwrite() must raise)
r   r   rj  r   r   r   r   r_   r   r  r  r   r   r   r  V  s   


zTestWrite304.test_err)r   r   r   r   r  r   r  r   r   r   r   r  I  s
    	r  c                   @   r  )TestEmptyWritec                 C   s    |ddg}|d |d g S r  r   )r  r  r   r   r   r   r   c  s   zTestEmptyWrite.applicationNr  r   r   r   r   r  a  r  r  c                   @   sR   e Zd ZdZdZejjsJ G dd dejZdd Zdd Z	dd	 Z
d
d ZdS )BadRequestTestsNc                   @   r  )zBadRequestTests.handler_classc                 C      t d)NzShould not print a tracebackr   rM   r   r   r   _print_unexpected_excs     z3BadRequestTests.handler_class._print_unexpected_excN)r   r   r   r  r   r   r   r   rs   r  r  rs   c                 C   s$   |  |d | j |ddg dgS )Nri   r   r   r  )r   rm   r4  r   r   r   r   v  s   zBadRequestTests.applicationc                 C   P   d| _ |  }|d| j   t|dd W d    d S 1 s!w   Y  d S )Nz-1007GET / HTTP/1.1
Host: localhost
Content-Length: %s

r   r^  r  rm   r   r   r   r:  r   r   r   test_negative_content_length{  
   
"z,BadRequestTests.test_negative_content_lengthc                 C   r  )Nr  r  r  r  r  r:  r   r   r   test_illegal_content_length  r  z+BadRequestTests.test_illegal_content_lengthc                 C   rG  )NzGET / HTTP %
r^  r  rI  )rJ   bad_requestr+   r   r   r   "test_bad_request_line_with_percent  s
   

"z2BadRequestTests.test_bad_request_line_with_percent)r   r   r   r   rm   rq   rs   r  r   r  r  r  r   r   r   r   r  k  s    r  c                   @   sp   e Zd ZdZdZdd ZdddZdddZd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )ChunkedInputTestsrf   Nc                 C   s   |d }g }|d }|dkr| d}|g}n"|dkr&|D ]}|| qn|dkr5| d |d ntd	|d
dg |S )Nr-  r   z/short-readr  z/linesz/pingr!   s   pongzbad pathr   r   )r6   r   RuntimeError)rJ   r  r  r  r   pidr$   r   r   r   r     s    

zChunkedInputTests.applicationc                 C   s   |d u r| j }t||dS )Ndirt)r  chunk_encode)rJ   rE   r  r   r   r   r    s   zChunkedInputTests.chunk_encodec                 C   s   | j g d|dS )N)thisz is rd   z
linez 2
line3rf   r  )r  )rJ   r  r   r   r   rD     r8  zChunkedInputTests.bodyc                 C   s   | d t|dd d S )NzGET /ping HTTP/1.1

pongr   )r   r   r:  r   r   r   ping  s   
zChunkedInputTests.pingc                 C   s   |  | d S rL   )r  r:  r   r   r   ping_if_possible     z"ChunkedInputTests.ping_if_possiblec              	   C   s   |   }d| }|  7}|jdd}|| t|dd | | W d    n1 s.w   Y  W d    d S W d    d S 1 sFw   Y  d S )NsN   POST /short-read HTTP/1.1
transfer-encoding: Chunked
Content-Length:1000

r!   r   
this is chr   )rD   r   r   r   r   r  )rJ   rD   reqr   r+   r   r   r   #test_short_read_with_content_length  s   

"z5ChunkedInputTests.test_short_read_with_content_lengthc                 C   ^   |   }d| }|  }|| t|dd | | W d    d S 1 s(w   Y  d S )NsK   POST /short-read HTTP/1.1
transfer-encoding: Chunked
Content-Length:0

r  r   rD   r   r   r   r  rJ   rD   r  r+   r   r   r   (test_short_read_with_zero_content_length  s   

"z:ChunkedInputTests.test_short_read_with_zero_content_lengthc                 C   r  )N9   POST /short-read HTTP/1.1
transfer-encoding: Chunked

r  r   r  r  r   r   r   test_short_read  s   

"z!ChunkedInputTests.test_short_readc                 C   sb   | j dd}d| }|  }|| t|dd | | W d    d S 1 s*w   Y  d S )Nz; here is dirt blar  s3   POST /ping HTTP/1.1
transfer-encoding: Chunked

r  r   r  r  r   r   r   	test_dirt  s   

"zChunkedInputTests.test_dirtc                 C   sj   |   }dt| }|d}||7 }|  }|| t|dd W d    d S 1 s.w   Y  d S )NzHPOST /lines HTTP/1.1
Content-Length: %s
transfer-encoding: Chunked

r    zthis is chunked
line 2
line3r   )rD   rA  r`   r   r   r   r  r   r   r   test_chunked_readline  s   


"z'ChunkedInputTests.test_chunked_readlinec              	   C   s   |    d}d| }|  (}|jddd}|| |  W d    n1 s*w   Y  W d    n1 s9w   Y  |  ~~t j  t	d t
jrpdd l}|  t	d t j  |  t	d | td d S )	Ns   4
thir  r!   wb)r   r   g{Gz?r   z1unexpected end of file while parsing chunked data)r  r   r   r   r   r   get_hubloop
update_nowsleepr   r	   r   r   r  IOError)rJ   rD   r  r   r+   r   r   r   r   test_close_before_finished  s.   






z,ChunkedInputTests.test_close_before_finishedrL   )r   r   r   r  r   r   r  rD   r  r  r   r  r  r  r  r  r   r   r   r   r    s    





r  c                   @   r  )Expect100ContinueTestsNc                 C   sl   t |d }|dkr|dddg dgS |d d}||d |d 7 }|d	d
tt|fdg |gS )Nri   i   z417 Expectation Failed)r   7r   s   failurer-  r!   r   r   )r5   r6   r_   rA  )rJ   r  r  rm   textr   r   r   r     s   z"Expect100ContinueTests.applicationc                 C   sh   |   &}|d t|ddd |d t|dd t|dd	 W d    d S 1 s-w   Y  d S )
NzOPUT / HTTP/1.1
Host: localhost
Content-length: 1025
Expect: 100-continue

i  failure)rG   rD   zSPUT / HTTP/1.1
Host: localhost
Content-length: 7
Expect: 100-continue

testingrc   r  testingr   rI  r:  r   r   r   test_continue+  s   


"z$Expect100ContinueTests.test_continue)r   r   r   r   r   r  r   r   r   r   r    s    r  c                   @   r  )MultipleCookieHeadersTestNc                 C   s.   |  |d d |  |d d |dg  g S )NHTTP_COOKIEzname1="value1"; name2="value2"HTTP_COOKIE2znameA="valueA"; nameB="valueB"r   )r   r  r   r   r   r   9  s   
z%MultipleCookieHeadersTest.applicationc                 C   sH   |   }|ddd t| W d    d S 1 sw   Y  d S )Nz~GET / HTTP/1.1
Host: localhost
Cookie: name1="value1"
Cookie2: nameA="valueA"
Cookie2: nameB="valueB"
Cookie: name2="value2"

r  r   )r   r   r\  r   r:  r   r   r   r  ?  s   

"zMultipleCookieHeadersTest.testr  r   r   r   r   r  5  s    r  c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zdd	 ZdS )
TestLeakInputNc                 C   s   t |  d | _d | _d S rL   )rq   r   _leak_wsgi_input_leak_environrM   r   r   r   r   O  s   

zTestLeakInput.tearDownc                 C   sR   |d }|d | _ || _|dkrtd|d< d}|ddtt|fd	g |gS )
Nr   r-  z/leak-framer   _leaks   foobarr   r   r   )r  r  r  	_getframer_   rA  )rJ   r  r  r  r  r   r   r   r   T  s   
zTestLeakInput.applicationc                 C   sP   |   }|d | }W d    n1 sw   Y  | |d| d S )Ns%   GET / HTTP/1.0
Connection: close

   HTTP/1.1 200 OK)r   r   r6   r   
startswithrJ   r+   r  r   r   r   !test_connection_close_leak_simple_  s
   


z/TestLeakInput.test_connection_close_leak_simplec                 C   s\   |   }|d | }W d    n1 sw   Y  | |d| | jd d S )Ns/   GET /leak-frame HTTP/1.0
Connection: close

r  r  )r   r   r6   r   r  r  r   r   r   r   r    test_connection_close_leak_framee  s   


z.TestLeakInput.test_connection_close_leak_frame)	r   r   r   r  r  r   r   r!  r"  r   r   r   r   r  J  s    r  c                   @   sT   e Zd ZdZdZdZdZdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd ZdS )TestHTTPResponseSplittingNr   r   c                 C   s$   t |  d | _tj| _tj| _d S rL   )rq   r   	start_excr#  r   r-   rM   r   r   r   r   u  s   
zTestHTTPResponseSplitting.setUpc                 C   s   t |  d | _d S rL   )rq   r   r$  rM   r   r   r   r   {  rS  z"TestHTTPResponseSplitting.tearDownc              
   C   sJ   z	|| j | j W n ty } z
|| _W Y d }~dS d }~ww d | _dS )Nr   )r   r-   rI   r$  )rJ   r  r  er   r   r   r     s   z%TestHTTPResponseSplitting.applicationc                 C   s`   |   }|d |  W d    n1 sw   Y  | | jt | | jjd | d S )N#GET / HTTP/1.0
Host: localhost

r   )r   r   r6   r  r$  
ValueErrorr   rP   )rJ   messager+   r   r   r   _assert_failure  s   


z)TestHTTPResponseSplitting._assert_failurec                 C   s   d| _ | d d S )Nz0200 OK
Connection: close
Content-Length: 0

z$carriage return or newline in status)r   r)  rM   r   r   r   test_newline_in_status  s   z0TestHTTPResponseSplitting.test_newline_in_statusc                 C      dg| _ | d d S )N)TestzHi
Connection: closez*carriage return or newline in header valuer-   r)  rM   r   r   r   test_newline_in_header_value     z6TestHTTPResponseSplitting.test_newline_in_header_valuec                 C   r+  )N)zTest
Hiz)carriage return or newline in header namer-  rM   r   r   r   test_newline_in_header_name  r/  z5TestHTTPResponseSplitting.test_newline_in_header_name)r   r   r   r   r   r-   r$  r   r   r   r)  r*  r.  r1  r   r   r   r   r#  l  s    	r#  c                   @   r  )TestInvalidEnvironNc                 C   sJ   |  D ]\}}|dv s|dr|dkrtd||f q|dg  g S )N)ri   r%  HTTP_	HTTP_HOSTz&Unexpected environment variable: %s=%rr   )r   r  r   )rJ   r  r  r/   r0   r   r   r   r     s   
zTestInvalidEnviron.applicationc                 C   sz   |   }|d t| W d    n1 sw   Y  |   }|d t| W d    d S 1 s6w   Y  d S )Nr&  r   rI  r:  r   r   r   r    s   





"zTestInvalidEnviron.testr  r   r   r   r   r2    s    	r2  c                   @   r  )TestInvalidHeadersDroppedNc                 C   s   |  d| |dg  g S )NHTTP_X_AUTH_USERr   )assertNotInr  r   r   r   r     s   
z%TestInvalidHeadersDropped.applicationc                 C   s@   |   }|d t| W d    d S 1 sw   Y  d S )Nz$GET / HTTP/1.0
x-auth_user: bob

rI  r:  r   r   r   r    s   


"zTestInvalidHeadersDropped.testr  r   r   r   r   r5    s    r5  c                   @   s2   e Zd ZdZG dd dejZdd Zdd ZdS )TestHandlerSubclassNc                   @   r  )z!TestHandlerSubclass.handler_classc                 C   sb   | j d}|d dkr*|| j d7 }| dkr"| jd d S | d| d S || j   S )N   r   <      s   <policy-file-request/>   HELLOzInvalid request: %r)rfiler6   r"   r   r  	log_errorr&   )rJ   r:   r   r   r   read_requestline  s   z2TestHandlerSubclass.handler_class.read_requestlineN)r   r   r   r?  r   r   r   r   rs     r  rs   c                 C   s   |dg  g S )Nr   r   r  r   r   r   r     s   
zTestHandlerSubclass.applicationc                 C   s   |   }|d |  | | d W d    n1 s w   Y  |   }|d |  t| W d    n1 sAw   Y  |   }|d |  | | d W d    d S 1 sgw   Y  d S )Ns   <policy-file-request/> r<  r?  z<policy-file-XXXuest/> rg   )r   r   r  r   r6   r   r:  r   r   r   r    s   






"zTestHandlerSubclass.test)r   r   r   r   rq   rs   r   r  r   r   r   r   r8    s
    r8  c                   @   s$   e Zd ZdZedd Zdd ZdS )TestErrorAfterChunkNc                 c   s    |ddg dV  t d)Nr   r   r  r@  )r   r
   r&  r   r   r   r     r  zTestErrorAfterChunk.applicationc              	   C   s|   |   *}|   |d | t t| W d    n1 s"w   Y  W d    n1 s1w   Y  | tj d S )Nz;GET / HTTP/1.1
Host: localhost
Connection: keep-alive

)	r   r  r   rj  r'  r   r  r   r
   r:  r   r   r   r    s   


zTestErrorAfterChunk.test)r   r   r   r   r   r   r  r   r   r   r   r@    s
    
r@  c                 C   s>   |d u rd}d}| D ]}dt |||f }||d7 }q
|S )Nrf   rg   z
%x%s
%s
r]   )rA  r`   )rE   r  bcr$   r   r   r   r    s   r  c                       s   e Zd Zd#ddZerd$ fdd	Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Z  ZS )%TestInputRawNFc                 C   sB   t |trt|}d}nt |trtr|d}tt|||dS )NTr]   )rm   chunked_input)r^   rj   r  r_   r   r`   r   StringIO)rJ   r:   rm   rD  r   r   r   
make_input
  s   

zTestInputRaw.make_inputc                    s,   t |tr
|d}tt| ||| d S r   )r^   r_   r`   r   rC  r   )rJ   firstsecondmsgr   r   r   r     s   

zTestInputRaw.assertEqualc                 C       | j ddd}| t|j d S N1r3   rm   rF  rj  r  r6   rJ   ir   r   r   test_short_post  r  zTestInputRaw.test_short_postc                 C   s"   | j ddd}| t|jd d S rK  rN  rO  r   r   r    test_short_post_read_with_length     z-TestInputRaw.test_short_post_read_with_lengthc                 C   rJ  rK  rF  rj  r  r&   rO  r   r   r   test_short_post_readline   r  z%TestInputRaw.test_short_post_readlinec                 C   s&   | j ddd}| }| |d d S )N12r3   rM  rF  r6   r   rJ   rP  r:   r   r   r   	test_post$     zTestInputRaw.test_postc                 C   s(   | j ddd}|d}| |d d S )NrV  r3   rM  r  rW  rX  r   r   r   test_post_read_with_length)     
z'TestInputRaw.test_post_read_with_lengthc                 C   s&   |  g d}| }| |d d S )NrL  2rf   rV  rW  rX  r   r   r   test_chunked.  rZ  zTestInputRaw.test_chunkedc                 C   s(   |  g d}|d}| |d d S )Nr]  r  rV  rW  rX  r   r   r   test_chunked_read_with_length3  r\  z*TestInputRaw.test_chunked_read_with_lengthc                 C       |  ddg}| t|j d S NrL  r^  rN  rO  r   r   r   test_chunked_missing_chunk8  r  z'TestInputRaw.test_chunked_missing_chunkc                 C   s"   |  ddg}| t|jd d S )NrL  r^  r  rN  rO  r   r   r   +test_chunked_missing_chunk_read_with_length<  rS  z8TestInputRaw.test_chunked_missing_chunk_read_with_lengthc                 C   ra  rb  rT  rO  r   r   r   #test_chunked_missing_chunk_readline@  r  z0TestInputRaw.test_chunked_missing_chunk_readlinec                 C   rJ  N2
1TrD  rN  rO  r   r   r   test_chunked_short_chunkD  r  z%TestInputRaw.test_chunked_short_chunkc                 C   s"   | j ddd}| t|jd d S )Nrg  Trh  r3   rN  rO  r   r   r   )test_chunked_short_chunk_read_with_lengthH  rS  z6TestInputRaw.test_chunked_short_chunk_read_with_lengthc                 C   rJ  rf  rT  rO  r   r   r   !test_chunked_short_chunk_readlineL  r  z.TestInputRaw.test_chunked_short_chunk_readlinec                 C   s  d}dt jd  }|d7 }||7 }d}d}t|dr"| | d ts0ts0| tt	|j
| | j||d	}| |
 d
 | |
|d | |
|| | j|| t|d d	}| |
 d
 | |
|d | |
|| | |
|d d S )Ns
   asdf
ghij
rD  r  r  s   qjk
l    :;$ 
bit_length#   rM  s   asdf
s   ghij
r!      q)r   rm  rS   r   rl  r   r	   rj  OverflowErrorrE  r&   rF  rA  )rJ   r:   	long_datapartial_datanrP  r   r   r   test_32bit_overflowP  s&   
z TestInputRaw.test_32bit_overflow)NFrL   )r   r   r   rF  r   r   rQ  rR  rU  rY  r[  r_  r`  rc  rd  re  ri  rj  rk  rs  r   r   r   r   r   rC  	  s$    
rC  c                   @   r#  )Test414c                 C   r  )Nzshould not get therer  r&  r   r   r   r   r  s   zTest414.applicationc                 C   sV   dd }|   }|d| d t|dd W d    d S 1 s$w   Y  d S )Nr$   i N  z"GET /%s HTTP/1.0
Hello: world

r    i  r  )r   r   r`   r   )rJ   longliner+   r   r   r   r  v  s
   
"zTest414.testN)r   r   r   r   r   r  r   r   r   r   rt  p  s    
rt  c                   @   sH   e Zd ZG dd deZdd Zedd Zdd Zd	d
 Z	dd Z
dS )TestLoggingc                   @   s0   e Zd ZdZdZdZdd Zdd Zdd ZdS )zTestLogging.LoggerNc                 C   s   ||f| _ d S rL   )logged)rJ   levelrI  r   r   r   rz     r  zTestLogging.Logger.logc                 C   s
   || _ d S rL   )accessedrJ   rI  r   r   r   access  s   
zTestLogging.Logger.accessc                 C   s   | j S rL   )thingrM   r   r   r   	get_thing  s   zTestLogging.Logger.get_thing)	r   r   r   ry  rw  r|  rz   r{  r}  r   r   r   r   Logger  s    r~  c                 C   s   |   S rL   )r~  rM   r   r   r   ry     r  zTestLogging.init_loggerc                 C   s   |ddg dgS )Nr   r   r  r   r&  r   r   r   r     s   zTestLogging.applicationc                 C   sZ   |  t| jjtj | jjd | jjd | | jjj	d | | jjj
d d S )Nr{  r   )   r   )r   r^   r~   rz   r   LoggingLogAdapterr{  r   r   ry  rw  rM   r   r   r   test_proxy_methods_on_log  s
   z%TestLogging.test_proxy_methods_on_logc                 C   s>   d| j j_| | j j d | j j`| | j j d  d S )N*   )r~   rz   r|  r   r}  rM   r   r   r   test_set_attributes  s   
zTestLogging.test_set_attributesc                 C   s:   |    | jjjd }| d|v | | d|v| d S )Nr!   z"GET / HTTP/1.1" 200 r  )r   r~   rz   rw  r   rz  r   r   r   test_status_log  s   zTestLogging.test_status_logN)r   r   r   r   r~  ry   r   r   r  r  r  r   r   r   r   rv  }  s    
	rv  c                       sP   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
  ZS )TestEnvironNc                    s   t t| | tj| j_d S rL   )r   r  r   r   r  r~   environ_classr   r   r   r   r     s   zTestEnviron.init_serverc                 C   s   |  |tj |ddg g S r3  )r  r   r  r4  r   r   r   r     s   zTestEnviron.applicationc                 C   r  rL   r  rM   r   r   r   !test_environ_is_secure_by_default  r=  z-TestEnviron.test_environ_is_secure_by_defaultc                 C   s   t  }| dt| | dt| d|d< | dt| | dt| d|_| tddit| | tddit| |`d|_| tddit| | tddit| d|_| tddit| | tddit| |`d S )	Nz'<pywsgi.SecureEnviron dict (keys: 0) atr0   r/   z'<pywsgi.SecureEnviron dict (keys: 1) atF)zmissing valuez<MASKED>r/   )r   r  assertInr7   r_   secure_reprr   whitelist_keys)rJ   r  r   r   r   test_default_secure_repr  s"   z$TestEnviron.test_default_secure_reprc                 C   s   G dd dt j}| }| |j d|_| |j | ti t| | ti t| d|_d|_	d|d< | tddit| | tddit| ~~dd l
}|  d S )	Nc                   @   r   )z>TestEnviron.test_override_class_defaults.<locals>.EnvironClassr   N)r   r   r   	__slots__r   r   r   r   EnvironClass  r   r  FTr  r!   r/   r   )r   r  r   r  default_secure_reprr   r   r_   r7   default_whitelist_keysr   r   )rJ   r  r  r   r   r   r   test_override_class_defaults  s   z(TestEnviron.test_override_class_defaultsc                 C   s(   t jt jfD ]}| |  | qd S rL   )r   Environr  r  r   )rJ   rl   r   r   r   test_copy_still_secure  s   z"TestEnviron.test_copy_still_securec           	      C   s   dd l }dd l}tjtjfD ]L}ddi}||}| || | || | || td|jd D ]}|	||}| 
d| ||}| t|t q2| |	||	| qd S )Nr   r/   r0   r!   s   Environ)picklejsonr   r  r  r  r   r   HIGHEST_PROTOCOLdumpsr7  loadstyper   )	rJ   r  r  rl   bltinr  protocolenv_dumploadedr   r   r   test_pickle_copy_returns_dict  s   
z)TestEnviron.test_pickle_copy_returns_dict)r   r   r   r   r   r   r  r  r  r  r  r   r   r   r   r   r    s    r  __main__rL   )f
__future__r   r   r   	patch_all
contextlibr   urllib.parser   r  r  ior   rE  r   r   wsgiref.validater   gevent.testingr  r   r   r	   gevent.testing.exceptionr
   r   r   gevent.pywsgir   r   r   ri   r   rh   rI   r   r1   r;   r   r<   r6   r   rq   r   r   r	  r  r  r  r  r   r$  r,  r2  r<  rC  rE  rN  rQ  rt  ry  r  r   r}  HAVE_SSLCONTEXTr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r#  r2  r5  r8  r@  r  BaseTestCaserC  rt  rv  r  r   mainr   r   r   r   <module>   s   Z T/   0 *

& "0+
g5
a