o
    er                     @   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   ID:\Projects\ConvertPro\env\Lib\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"   )fdZresponse_lineheaderslinekeyvaluer   r   r   read_headers>   s&   
	
(r0   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_sizeZcrlfdatar   r   r   iread_chunksS   s   



r9   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 r2   /HTTP)1.01.1z	Error: %r)status_liner,   bodychunksr)   reasonr4   codeversionr6   	Exceptionr*   )selfrA   r,   rF   rE   r>   r   r   r   __init__d   s   
zResponse.__init__c                 c   s    | j V  | jV  | jV  d S N)rA   r,   rB   rH   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   rA   r,   rB   rC   )rH   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)hasattrrE   )rH   rE   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)rD   )rH   rD   r   r   r   assertReason      "zResponse.assertReasonc                 C   rS   )Nz'Unexpected version: %r (expected %r)
%s)rF   )rH   rF   r   r   r   assertVersion   rU   zResponse.assertVersionc                 C   s.   | j |d}||ksJ d|||| f d S )NFz)Unexpected header %r: %r (expected %r)
%s)r,   get)rH   headerr/   
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   encoderB   )rH   rB   r   r   r   
assertBody   s   
"zResponse.assertBodyr   defaultr@   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.
        Nr`   d   r   chunkedTransfer-Encoding zhWARNING: server used chunked transfer-encoding despite having Content-Length header (libevent 1.x's bug)    )r0   rR   REASONSrW   rT   rV   rE   r\   r4   r]   rZ   CONTENT_LENGTHr*   listr9   rC   joinrB   r5   r_   )clsr+   rE   rD   rF   rB   rC   content_lengthZ_status_liner,   rH   numr   r   r   r5      s<   








zResponse.read)r   r`   r@   NNN)r   r   r   rI   rL   rO   rR   rT   rV   rZ   r_   classmethodr5   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   ZApplicationErrorr   r   r   r   handler_class   r   ro   c                 C   s"   dd l }|d}||j |S )Nr   zgevent.tests.pywsgi)logging	getLoggersetLevelCRITICAL)rH   rp   loggerr   r   r   init_logger   s   
zTestCase.init_loggerc                 C   s0   |    }| _tj| jdf|||| jd| _d S )Nr   )logZ	error_logro   )ru   rt   r   
WSGIServerlisten_addrro   server)rH   applicationrt   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)rz   r   r{   ry   startZserver_portr*   port	greentestrn   setUprH   rz   r   r   r   r      s   





zTestCase.setUpc                 C   s   | j t| |S rJ   )close_on_teardownappendweakrefref)rH   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 rJ   r   )r#   rr   r   r   
<listcomp>   s    z5TestCase._tearDownCloseOnTearDown.<locals>.<listcomp>)r   superrn   _tearDownCloseOnTearDownrK   rM   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~   rn   tearDownry   geventTimeout	start_newstopr	   gccollect)rH   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^   )r8   Z_rconn_writer   r   write  s   

z1TestCase.connect.<locals>.makefile.<locals>.write)popr   r   )rN   kwargsZrconnr   )conn_makefilerH   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)rH   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)rH   resultr   r   )r   r   r   rH   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   )rH   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)rH   rN   r   r   r+   r   r   r   urlopen6  s   

"zTestCase.urlopenr@   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pathZhttp_verr,   )DEFAULT_EXTRA_CLIENT_HEADERScopyupdateri   itemsdictHTTP_CLIENT_VERSION)rH   r   r   r,   Zdef_headersr   r   r   r   format_request?  s   

zTestCase.format_request)r   r=   )r   r   r   ry   staticmethodr   rz   r~   ZDEFAULT_BIND_ADDRrx   ZDEFAULT_LOCAL_HOST_ADDRr   r   ZWSGIHandlerro   ru   r{   r   ZCPYTHONPY2r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   rn      s,    	
&
rn   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rB   
Connectionr   ,Server closed connection, not expecting that
keep-alive	/notexistr     	Not Found	not foundrE   rD   rB   )r   r   r   r   r,   rW   
assertTrueEXPECT_CLOSEassertFalseEXPECT_KEEPALIVErZ   )rH   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)rH   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,   rW   r   r   r   r   rZ   r&   r   errorrN   CONN_ABORTED_ERRORS)rH   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!   Z414)ranger   ri   r   r   r&   r)   assertEqual)rH   
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_environrW   r   )rH   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   re   s   upgrading tos	   websocket404 Not Found   not    found)r   )rH   envstart_responser   r   r   r   r   rz     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   rC   rZ   )rH   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   rC   r   rZ   rH   r+   r   r   r   r   test_dne  s   
zTestNoChunks.test_dne)T)
r   r   r   r   r   r   rz   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   rE   Fr   r   r   r   r   r   r   rC   r   r   r   r   test_connection_upgrade     
z.TestConnectionUpgrades.test_connection_upgradec                 C   r   )Nr   Z	websocket)r   Z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 )TestNoChunks10r?   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   rW   )rH   r   r   r   r   r   r   rz     s   z%TestExplicitContentLength.applicationNr   r   r   rz   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   rz   
  s   

zTestYield.applicationNr   r   r   r   rz   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   rz     s   
zTestBytearray.application)r   r   r   r   r   rz   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   rz   $  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: 0rd   rd   Nri   r   r   r   rH   r   r+   r   r   r   test_multiline_116*  s
   



"z&TestMultiLineHeader.test_multiline_116N)r   r   r   r   rz   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)r5   r   r'   r   rW   r^   )r   r   rB   r  r   r   r   rz   :  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: 3rd   za=aZ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   rz   r  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   rH   r   r   r   r   r   rz   Y  s   zTestCloseIter.applicationc                 c   s    t dV  dV  d S )N   Hello World   !r  rK   r   r   r   rL   ]  s   

zTestCloseIter.__iter__Fc                 C   s   |  j d7  _ d S )Nr!   )closedrK   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!r#  r$  rB   rC   r!   )r%  r   r   r   r   rH   r+   r   r   r   test_close_is_calledf  s   

z"TestCloseIter.test_close_is_called)	r   r   r   r   rz   rL   r%  r   r)  r   r   r   r   r   T  s    r   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 )Nre   )ri   rC   rK   r   r   r   rB   r     zTestChunkedApp.bodyc                 c   s4    |  |d |ddg | jD ]}|V  qd S )Nr   r   r   )r   rW   rC   )rH   r   r   chunkr   r   r   rz   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

r'  rc   rb   )r   r   r   rB   rZ   r   rC   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

rc   r   )	r   r   r   r   rB   r,   rW   r]   len)rH   r+   r   rk   r   r   r   test_no_chunked_http_1_0  s   


z'TestChunkedApp.test_no_chunked_http_1_0N)r   r   r   rC   rB   rz   r.  r0  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   rC   r   r   r   r   r1    r   r1  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   rW   r5   )rH   r   r   r8   r   r   r   rz     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   rH   r8   r+   r   r   r   test_negative_chunked_read  
   

"z+TestNegativeRead.test_negative_chunked_readc                 C   r5  )NsT   POST /read HTTP/1.1
Host: localhost
Connection: close
Content-Length: 6

oh hair6  r   r7  r8  r   r   r   test_negative_nonchunked_read  r:  z.TestNegativeRead.test_negative_nonchunked_readN)r   r   r   rz   r9  r;  r   r   r   r   r3    s    r3  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  r4  )r&   )r   r   r8   r   r   r   rz     s
   z TestNegativeReadline.applicationc                 C   r5  )Nsp   POST /readline HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2
oh
4
 hai
0

r6  r   r7  r8  r   r   r   test_negative_chunked_readline  r:  z3TestNegativeReadline.test_negative_chunked_readlinec                 C   r5  )NsX   POST /readline HTTP/1.1
Host: localhost
Connection: close
Content-Length: 6

oh hair6  r   r7  r8  r   r   r   !test_negative_nonchunked_readline  r:  z6TestNegativeReadline.test_negative_nonchunked_readline)r   r   r   r   r   rz   r=  r>  r   r   r   r   r<    s    
r<  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   callsrK   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  rB  r5   r   r   r   r   <lambda>      z-TestChunkedPost.application.<locals>.<lambda>re   z/cc                      rC  )Nr  r!   rD  r   rE  r   r   rF    rG  s   We should not get herer[   )r@  r   rW   r5   rh   iterr^   )rH   r   r   r8   linesr   rE  r   rz     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

r6  r   s   /as   /bs   /c)r   r   r   replacer8  r   r   r   test_014_chunked_post  s   



"z%TestChunkedPost.test_014_chunked_postc                 C   r5  )Nst   POST /a HTTP/1.1
Host: localhost
Connection: close
Content-Length: 12
Transfer-Encoding: chunked

{"hi": "ho"}  r   r7  r8  r   r   r   #test_229_incorrect_chunk_no_newline  s
   

"z3TestChunkedPost.test_229_incorrect_chunk_no_newlinec                 C   r5  )Nsv   POST /a HTTP/1.1
Host: localhost
Connection: close
Content-Length: 12
Transfer-Encoding: chunked

{"hi": "ho"}
rL  r   r7  r8  r   r   r    test_229_incorrect_chunk_non_hex  s
   

"z0TestChunkedPost.test_229_incorrect_chunk_non_hexc                 C   r5  )Nsw   POST /a HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2;token="oh hi"
oh
4
 hai
0

r6  r   r7  r8  r   r   r   !test_229_correct_chunk_quoted_ext
  r:  z1TestChunkedPost.test_229_correct_chunk_quoted_extc                 C   r5  )Nu   POST /a HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked

2;token=oh_hi
oh
4
 hai
0

r6  r   r7  r8  r   r   r    test_229_correct_chunk_token_ext  r:  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 )NrP  s   oh_his   _oh_hii  rL  r   )rJ  r   r   r   r8  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
r6  r   oh byer2   r   r   r   r   r@  )rH   Zdata1Z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

r6  r   )r   r   r   assertRaisesr   r8  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   tr6  r   rS  )r   MAX_REQUEST_LINEr   r   r   rV  r   r8  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

r6  r   r!   r   r   r   rV  r   r   r@  r8  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

r6  r   rL  r   r!   r[  r8  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

r6  r   rL  r   r!   rT  r8  r   r   r   >test_trailers_request_smuggling_request_terminates_then_header  s   

zNTestChunkedPost.test_trailers_request_smuggling_request_terminates_then_header)r   r   r   r@  r   rz   rK  rM  rN  rO  rQ  rR  r~   ZskipOnWindowsrU  rW  rZ  r\  r]  r^  r   r   r   r   r   r?    s,    
#


 
r?  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)rk   rB   rG   end)rH   r   r   r   r   r   r   rz     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   rc   F)r   r   r   rB   r`  rZ   rk   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   Frc   rb   )r   r   r   rB   r`  rZ   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   Frc   rb   )r   r   r   rB   r`  rZ   r   rC   r   r   r   r   test_no_content_length_twice  s   

z)TestUseWrite.test_no_content_length_twiceN)r   r   r   rB   r`  r]   r/  rk   rz   ra  rb  rc  r   r   r   r   r_    s    r_  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   rw   rx   rf  rg  ry   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

rB   r   )	sslr   wrap_socketr   r   r/  rW   flushr   )rH   r   	post_bodyr   rh  Z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  
   rD  rH   environr   r   r   r   rz     s   zHttpsTestCase.application)r   N)r   r   r   osr   ri   dirname__file__rf  rg  r{   r   rz   r   r   r   r   rd    s    
rd  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_contextre  )ssl_context)	
gevent.sslru  load_cert_chainrf  rg  r   rw   rx   ry   )rH   rz   ru  contextr   r   r   r{   &  s   z#HttpsSslContextTestCase.init_serverN)r   r   r   r{   r   r   r   r   rt  %      rt  c                   @   s*   e Zd Zeedrdd Zdd ZdS dS )	TestHttpsrh  c                 C   s    | j ddd}| |jd d S )NPOSTabc)r   rk  r   r   rB   rH   r   r   r   r   test_012_ssl_server8     zTestHttps.test_012_ssl_serverc                 C   s   |   }| |jd d S )Nrd   r~  r  r   r   r   test_013_empty_return<  s   zTestHttps.test_013_empty_returnN)r   r   r   rQ   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   Z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   )rH   ro  r   Z
path_bytesr   r   r   rz   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

   
r3   PASSEDFrd   r   )rD   rC   rB   rk   )r   sendallrJ  r   r   )rH   r   r+   r   r   r   testR  s   

"zTestInternational.testr   r   r   r   rz   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 rJ   )r   r  r   errorsrK   r   r   r   r   e  s   
z(TestNonLatin1HeaderFromApplication.setUpc                    s   g | _ tt|   d S rJ   )r  r   r  r   rK   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-Headerr2   )rX   r  r   sysexc_inforn  r   r   r   rz   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   )rE   rD   )Z
where_typer!   r   r   r  )r   expect_one_errorr  r   should_errorr   assert_errorr   SecureEnvironr   r/  r  assertIsInstanceUnicodeError)rH   r   r+   r   vr   r   r   r  x  s"   


"z'TestNonLatin1HeaderFromApplication.test)r   r   r   error_fatalr   rX   r   r  r   r   rz   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   rX   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   r6   rH   ro  r   inputrI  r-   r   r   r   rz     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' )rD   rB   )r   r   r/  rj  r   )rH   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   r6   r  r   r   r   rz     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<   )r6   )r#   r-   r   r   r   r     s    r  c                 S   r  r  r  r  r   r   r   r     r  )	readlinesr   )rH   ro  r   r  rI  r   r   r   rz     s
   
zTestInputReadlines.applicationNr  r   r   r   r   r    r  r  c                   @   r  )
TestInputNNc                 C   s   |d  d |dg  g S )Nr     r   rD  rn  r   r   r   rz     s   
zTestInputN.applicationc                 C      |    d S rJ   r   rK   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   r"  r   r   r   rz     s   z"TestErrorInApplication.applicationc                 C   s(   |    | jdd | tj| j d S )Nr   r   )r  r   r  r~   r
   r   rK   r   r   r   r    s   zTestErrorInApplication.testN)r   r   r   r   r   r  rz   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  r"  r   r   r   rz     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   re   r   r  r   r   r   rz        
zTestEmptyYield.applicationc                 C   sb   g }|   }|d t|d|d | }W d    n1 s!w   Y  | |dd|  d S )Nr-  rd   r'  re   zgot garbage: %rr   r   r   r5   r   rH   rC   r+   garbager   r   r   test_err  s   


zTestEmptyYield.test_errNr   r   r   r   rz   r  r   r   r   r   r        
r  c                   @   r  )TestFirstEmptyYieldc                 c   s    |ddg dV  dV  d S )Nr   r   re      hellor   r  r   r   r   rz     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  r'  re   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 modifiedre   r   r  r   r   r   rz     s   

zTestEmptyYield304.applicationc                 C   sZ   |   }|d t|dddd | }W d    n1 s w   Y  | |d d S )Nr-  i0  rd   F)rE   rB   rC   re   r  )rH   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   rN   )rH   r   r   r   r   r   r   rz   '  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   ZRaisedF)rE   rD   rB   rC   re   r  )rH   rB   r+   r  r   r   r   r  /  s   


zTestContentLength304.test_errr   r   r   r   rz   r  r   r   r   r   r  $  s    r  c                   @   r  )TestBody304Nc                 C   s   |dg  dgS )Nr  s   bodyr   r"  r   r   r   rz   <  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   rV  r   r   r   r   r]   rH   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  FrB   T)error_raisedr   r   rN   )rH   r   r   r   r   r   r   r   rz   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   rV  r   r   r   r   r]   r   r  r  r   r   r   r  V  s   


zTestWrite304.test_err)r   r   r   r   r  rz   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   rz   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   rK   r   r   r   _print_unexpected_excs     z3BadRequestTests.handler_class._print_unexpected_excN)r   r   r   r  r   r   r   r   ro   r  rz  ro   c                 C   s$   |  |d | j |ddg dgS )Nrg   r   r   r  )r   rk   r"  r   r   r   rz   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   rL  r   rk   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   r5  )NzGET / HTTP %
rL  r   r7  )rH   bad_requestr+   r   r   r   "test_bad_request_line_with_percent  s
   

"z2BadRequestTests.test_bad_request_line_with_percent)r   r   r   r   rk   rn   ro   r  rz   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 )ChunkedInputTestsrd   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-readrm  z/linesz/pingr!   s   pongzbad pathr   r   )r5   r   RuntimeError)rH   r   r   r  r   pidr$   r   r   r   rz     s    

zChunkedInputTests.applicationc                 C   s   |d u r| j }t||dS )Ndirt)r  chunk_encode)rH   rC   r  r   r   r   r    s   zChunkedInputTests.chunk_encodec                 C   s   | j g d|dS )N)thisz is rb   z
linez 2
line3rd   r  )r  )rH   r  r   r   r   rB     r&  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 rJ   )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   )rB   r   r   r   r   r  )rH   rB   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   rB   r   r   r   r  rH   rB   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   )rB   r/  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   Zget_hubloopZ
update_nowsleepr~   r	   r   r   r  IOError)rH   rB   r  r   r+   r   r   r   r   test_close_before_finished  s.   






z,ChunkedInputTests.test_close_before_finishedrJ   )r   r   r   r  r   rz   r  rB   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 )Nrg   i   z417 Expectation Failed)r   7r   s   failurer  r!   r   r   )r4   r5   r]   r/  )rH   ro  r   rk   textr   r   r   rz     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  Zfailure)rE   rB   zSPUT / HTTP/1.1
Host: localhost
Content-length: 7
Expect: 100-continue

testingra   r   testingr   r7  r(  r   r   r   test_continue+  s   


"z$Expect100ContinueTests.test_continue)r   r   r   r   rz   r  r   r   r   r   r    s    r  c                   @   r  )MultipleCookieHeadersTestNc                 C   s.   |  |d d |  |d d |dg  g S )NZHTTP_COOKIEzname1="value1"; name2="value2"ZHTTP_COOKIE2znameA="valueA"; nameB="valueB"r   )r   rn  r   r   r   rz   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   rJ  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 rJ   )rn   r   _leak_wsgi_input_leak_environrK   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]   r/  )rH   ro  r   r  r  r   r   r   rz   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   r5   r   
startswithrH   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   r5   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   rz   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 rJ   )rn   r   	start_excr  r   r,   rK   r   r   r   r   u  s   
zTestHTTPResponseSplitting.setUpc                 C   s   t |  d | _d S rJ   )rn   r   r  rK   r   r   r   r   {  rA  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,   rG   r  )rH   ro  r   er   r   r   rz     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   r5   r  r  
ValueErrorr   rN   )rH   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
  rK   r   r   r   test_newline_in_status  s   z0TestHTTPResponseSplitting.test_newline_in_statusc                 C      dg| _ | d d S )N)ZTestzHi
Connection: closez*carriage return or newline in header valuer,   r
  rK   r   r   r   test_newline_in_header_value     z6TestHTTPResponseSplitting.test_newline_in_header_valuec                 C   r  )N)zTest
ZHiz)carriage return or newline in header namer  rK   r   r   r   test_newline_in_header_name  r  z5TestHTTPResponseSplitting.test_newline_in_header_name)r   r   r   r   r   r,   r  r   r   rz   r
  r  r  r  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)rg   r  ZHTTP_Z	HTTP_HOSTz&Unexpected environment variable: %s=%rr   )r   r   r   )rH   ro  r   r.   r/   r   r   r   rz     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   r7  r(  r   r   r   r    s   





"zTestInvalidEnviron.testr  r   r   r   r   r    s    	r  c                   @   r  )TestInvalidHeadersDroppedNc                 C   s   |  d| |dg  g S )NZHTTP_X_AUTH_USERr   )assertNotInrn  r   r   r   rz     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

r7  r(  r   r   r   r    s   


"zTestInvalidHeadersDropped.testr  r   r   r   r   r    s    r  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)rfiler5   r"   r   r  Z	log_errorr&   )rH   r8   r   r   r   read_requestline  s   z2TestHandlerSubclass.handler_class.read_requestlineN)r   r   r   r  r   r   r   r   ro     r  ro   c                 C   s   |dg  g S )Nr   r   rn  r   r   r   rz     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/> re   )r   r   rj  r   r5   r   r(  r   r   r   r    s   






"zTestHandlerSubclass.test)r   r   r   r   rn   ro   rz   r  r   r   r   r   r    s
    r  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   rz     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   rV  r  r   r  r~   r
   r(  r   r   r   r    s   


zTestErrorAfterChunk.test)r   r   r   r   r   rz   r  r   r   r   r   r    s
    
r  c                 C   s>   |d u rd}d}| D ]}dt |||f }||d7 }q
|S )Nrd   re   z
%x%s
%s
r[   )r/  r^   )rC   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[   )rk   chunked_input)r\   rh   r  r]   r   r^   r   StringIO)rH   r8   rk   r  r   r   r   
make_input
  s   

zTestInputRaw.make_inputc                    s,   t |tr
|d}tt| ||| d S r   )r\   r]   r^   r   r  r   )rH   firstsecondmsgr   r   r   r     s   

zTestInputRaw.assertEqualc                 C       | j ddd}| t|j d S N1r2   rk   r!  rV  r  r5   rH   ir   r   r   test_short_post  r  zTestInputRaw.test_short_postc                 C   s"   | j ddd}| t|jd d S r&  r)  r*  r   r   r    test_short_post_read_with_length     z-TestInputRaw.test_short_post_read_with_lengthc                 C   r%  r&  r!  rV  r  r&   r*  r   r   r   test_short_post_readline   r  z%TestInputRaw.test_short_post_readlinec                 C   s&   | j ddd}| }| |d d S )N12r2   r(  r!  r5   r   rH   r+  r8   r   r   r   	test_post$     zTestInputRaw.test_postc                 C   s(   | j ddd}|d}| |d d S )Nr1  r2   r(  rm  r2  r3  r   r   r   test_post_read_with_length)     
z'TestInputRaw.test_post_read_with_lengthc                 C   s&   |  g d}| }| |d d S )Nr'  2rd   r1  r2  r3  r   r   r   test_chunked.  r5  zTestInputRaw.test_chunkedc                 C   s(   |  g d}|d}| |d d S )Nr8  rm  r1  r2  r3  r   r   r   test_chunked_read_with_length3  r7  z*TestInputRaw.test_chunked_read_with_lengthc                 C       |  ddg}| t|j d S Nr'  r9  r)  r*  r   r   r   test_chunked_missing_chunk8  r  z'TestInputRaw.test_chunked_missing_chunkc                 C   s"   |  ddg}| t|jd d S )Nr'  r9  rm  r)  r*  r   r   r   +test_chunked_missing_chunk_read_with_length<  r.  z8TestInputRaw.test_chunked_missing_chunk_read_with_lengthc                 C   r<  r=  r/  r*  r   r   r   #test_chunked_missing_chunk_readline@  r  z0TestInputRaw.test_chunked_missing_chunk_readlinec                 C   r%  N2
1Tr  r)  r*  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 )NrB  TrC  r2   r)  r*  r   r   r   )test_chunked_short_chunk_read_with_lengthH  r.  z6TestInputRaw.test_chunked_short_chunk_read_with_lengthc                 C   r%  rA  r/  r*  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
r2  rm  r  s   qjk
l    :;$ 
bit_length#   r(  s   asdf
s   ghij
r!      q)r   rY  rQ   r   rG  r   r	   rV  OverflowErrorr   r&   r!  r/  )rH   r8   Z	long_dataZpartial_datanr+  r   r   r   test_32bit_overflowP  s&   
z TestInputRaw.test_32bit_overflow)NFrJ   )r   r   r   r!  r   r   r,  r-  r0  r4  r6  r:  r;  r>  r?  r@  rD  rE  rF  rL  r   r   r   r   r   r  	  s$    
r  c                   @   r  )Test414c                 C   r  )Nzshould not get therer  r  r   r   r   rz   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   )rH   Zlongliner+   r   r   r   r  v  s
   
"zTest414.testN)r   r   r   r   rz   r  r   r   r   r   rM  p  s    
rM  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 rJ   )logged)rH   levelr$  r   r   r   rv     r  zTestLogging.Logger.logc                 C   s
   || _ d S rJ   )accessedrH   r$  r   r   r   access  s   
zTestLogging.Logger.accessc                 C   s   | j S rJ   )thingrK   r   r   r   	get_thing  s   zTestLogging.Logger.get_thing)	r   r   r   rQ  rO  rT  rv   rS  rU  r   r   r   r   Logger  s    rV  c                 C   s   |   S rJ   )rV  rK   r   r   r   ru     r  zTestLogging.init_loggerc                 C   s   |ddg dgS )Nr   r   r  r   r  r   r   r   rz     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 )NrS  r   )   r   )r   r\   ry   rv   r   ZLoggingLogAdapterrS  r   r   rQ  rO  rK   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*   )ry   rv   rT  r   rU  rK   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   ry   rv   rO  r   rR  r   r   r   test_status_log  s   zTestLogging.test_status_logN)r   r   r   r   rV  ru   r   rz   rX  rZ  r[  r   r   r   r   rN  }  s    
	rN  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 rJ   )r   r\  r{   r   r  ry   Zenviron_classr   r   r   r   r{     s   zTestEnviron.init_serverc                 C   s   |  |tj |ddg g S r!  )r  r   r  r"  r   r   r   rz     s   zTestEnviron.applicationc                 C   r  rJ   r  rK   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) atr/   r.   z'<pywsgi.SecureEnviron dict (keys: 1) atF)zmissing valuez<MASKED>r.   )r   r  ZassertInr6   r]   secure_reprr   Zwhitelist_keys)rH   ro  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   rb  FTr^  r!   r.   r   )r   r  r   r_  Zdefault_secure_reprr   r   r]   r6   Zdefault_whitelist_keysr   r   )rH   rb  ro  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 rJ   )r   Environr  r  r   )rH   rj   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.   r/   r!   s   Environ)picklejsonr   rd  r  r  r   r   HIGHEST_PROTOCOLdumpsr  loadstyper   )	rH   rf  rg  rj   Zbltinr   protocolZenv_dumpZloadedr   r   r   test_pickle_copy_returns_dict  s   
z)TestEnviron.test_pickle_copy_returns_dict)r   r   r   r   r{   rz   r]  r`  rc  re  rm  r   r   r   r   r   r\    s    r\  __main__rJ   )f
__future__r   r   r   Z	patch_all
contextlibr   urllib.parser   rp  r  ior   r   r   r   Zwsgiref.validater   Zgevent.testingr  r~   r   r	   Zgevent.testing.exceptionr
   r   r   Zgevent.pywsgir   r   r   rg   r   rf   rG   r   r0   r9   r   r:   r5   r   rn   r   r   r   r  r  r  r
  r  r  r  r   r*  r1  r3  r<  r?  r_  rd  rw  r   rh  ZHAVE_SSLCONTEXTrt  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  ZBaseTestCaser  rM  rN  r\  r   mainr   r   r   r   <module>   s   Z T/   0 *

& "0+
g5
a