
    dafKu                        d dl mZ d dlmZ d dlmZ d dlmZ d dl mZ d dl	m
Z
 d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZ d dlmZ d dlmZmZmZ d dlm Z  d dl!m"Z" d dl#Z#d dl$Z$d dl%Z%d dl&Z&d dl'Z'd dl Z  G d de      Z( G d de      Z)d Z*d Z+e*dfdZ,d Z- G d de      Z. G d de      Z/ G d d e      Z0 G d! d"e      Z1 G d# d$e1      Z2 G d% d&e      Z3e4d'k(  r e jj                          yy)(    )mock)support)socket_helper)NoLogRequestHandler)TestCase)setup_testing_defaults)Headers)BaseHandlerBaseCGIHandlerSimpleHandler)util)	validator)
WSGIServerWSGIRequestHandler)make_server)HTTPConnection)StringIOBytesIOBufferedReader)
BaseServer)python_implementationNc                       e Zd ZdZd Zd Zy)
MockServerzNon-socket HTTP serverc                 R    t        j                  | ||       | j                          y N)r   __init__server_bind)selfserver_addressRequestHandlerClasss      ,/root/Python-3.12.4/Lib/test/test_wsgiref.pyr   zMockServer.__init__   s!    D.2EF    c                 ^    | j                   \  }}|| _        || _        | j                          y r   )r   server_nameserver_portsetup_environ)r   hostports      r!   r   zMockServer.server_bind!   s-    ((
dr"   N)__name__
__module____qualname____doc__r   r    r"   r!   r   r      s     r"   r   c                       e Zd ZdZd Zd Zy)MockHandlerzNon-socket HTTP handlerc                 X    | j                   | _        | j                  \  | _        | _        y r   )request
connectionrfilewfiler   s    r!   setupzMockHandler.setup*   s    ,,!%
DJr"   c                      y r   r-   r5   s    r!   finishzMockHandler.finish.   s    r"   N)r)   r*   r+   r,   r6   r8   r-   r"   r!   r/   r/   (   s    !1r"   r/   c                      |dddg       dgS )N200 OKContent-Typez
text/plainDatezMon, 05 Jun 2006 18:49:54 GMT   Hello, world!r-   environstart_responses     r!   	hello_apprC   2   s"    8%0  r"   c                 r     |dddg       dj                  | d   | d   | d   g      j                  d      gS )	Nr:   r;   r=   ;HTTP_X_TEST_HEADERQUERY_STRING	PATH_INFO
iso-8859-1)joinencoder@   s     r!   
header_apprL   :   sX    8&1  HH$%w~'>  vl r"   s   GET / HTTP/1.0

c                 V   t        dd| t        t              }t        t	        |            }t	               }t
        j                  }t               x}t
        _        	 |j                  ||fd       |t
        _        |j                         |j                         fS # |t
        _        w xY w)N P   )	127.0.0.1i"  )
r   r   r/   r   r   sysstderrr   finish_requestgetvalue)appdataserverinpoutolderrerrs          r!   	run_amockr\   E   s    Rj+>F

'C
)CZZFz!C#*sCj*<=
<<>3<<>)) 
s   B B(c                      |        }t        |      |urt        |D ]  }t        |      |k(  rt         	 t        |       t        d|      # t        $ r Y yw xY w)zUtility to compare a generic iterator with an iterable

    This tests the iterator using iter()/next().
    'make_it' must be a function returning a fresh
    iterator to be tested (since this may test the iterator twice).zToo many items from .__next__()N)iterAssertionErrornextStopIteration)make_itmatchititems       r!   compare_generic_iterrf   T   sn     
B8r> !Bx4  !DR >CC  s   A 	AAc                   P    e Zd ZddZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)IntegrationTestsc                     t               dz   t        j                  j                         d   z   }| j	                  |d|z   dz   |xr dxs dz   dz   j                  d             y )	N/r   z(HTTP/1.0 200 OK
Server: WSGIServer/0.2 zA
Content-Type: text/plain
Date: Mon, 05 Jun 2006 18:49:54 GMT
zContent-Length: 13
rN   z
Hello, world!rI   )r   rQ   versionsplitassertEqualrK   )r   rY   
has_lengthpyvers       r!   check_hellozIntegrationTests.check_hellok   s{    &(3.!!#A&'&(-.066 55;	=
 $VL1	
r"   c                 @    t               \  }}| j                  |       y r   )r\   rp   r   rY   r[   s      r!   test_plain_helloz!IntegrationTests.test_plain_hellox   s    ;Sr"   c                 t    d}t        t        |      \  }}| j                  |j                         d   d       y )Nsm   GET /p%61th/?query=test HTTP/1.0
X-Test-Header: Python test 
X-Test-Header: Python test 2
Content-Length: 0

s+   Python test,Python test 2;query=test;/path/)r\   rL   rm   
splitlines)r   r1   rY   r[   s       r!   test_environzIntegrationTests.test_environ|   s>    % 	 Z1SNNR :	
r"   c                 z    t        dddz  z   dz         \  }}| j                  |j                         d   d       y )Ns   GET    xi  s    HTTP/1.0

rV   r   s!   HTTP/1.0 414 Request-URI Too Long)r\   rm   rv   rr   s      r!   test_request_lengthz$IntegrationTests.test_request_length   s>    'TE\":=M"MNS)!,=	?r"   c                 `    t        t        t                    \  }}| j                  |d       y )NF)rn   )r\   r   rC   rp   rr   s      r!   test_validated_helloz%IntegrationTests.test_validated_hello   s(    Yy12S/r"   c                     d }t        t        |            \  }}| j                  |j                  d             | j	                  |j                         d   d       y )Nc                      |dd       dgS )Nr:   r;   zHello, world!r-   r@   s     r!   bad_appz>IntegrationTests.test_simple_validation_error.<locals>.bad_app   s    8%BC#$$r"   ;   A server error occurred.  Please contact the administrator.z^AssertionError: Headers (('Content-Type', 'text/plain')) must be of type list: <class 'tuple'>r\   r   
assertTrueendswithrm   rv   r   r   rY   r[   s       r!   test_simple_validation_errorz-IntegrationTests.test_simple_validation_error   sZ    	% Yw/0SJ
 	 	NNR 0	
r"   c                 "   d }g d}|D ]w  \  }}| j                  |      5  t         ||            \  }}| j                  |j                  d             | j	                  |j                         d   |       d d d        y y # 1 sw Y   xY w)Nc                       fd}|S )Nc                      |dg       dgS )Nr<   ztext/plain; charset=utf-8r?   r-   )rA   rB   statuss     r!   r   zWIntegrationTests.test_status_validation_errors.<locals>.create_bad_app.<locals>.bad_app   s    v(U'VW())r"   r-   )r   r   s   ` r!   create_bad_appzFIntegrationTests.test_status_validation_errors.<locals>.create_bad_app   s    * Nr"   ))200z4AssertionError: Status must be at least 4 characters)z20X OKz8AssertionError: Status message must begin w/3-digit code)200OKz;AssertionError: Status message must have a space after code)r   r   r   )subTestr\   r   r   rm   rv   )r   r   testsr   exc_messagerY   r[   s          r!   test_status_validation_errorsz.IntegrationTests.test_status_validation_errors   s    	
 $) 	DFKV, D$^F%;<SR!    !1"!5{CD D	DD Ds   ABB	c                     d }t        t        |            \  }}| j                  |j                  d             | j	                  |j                         d   d       y )Nc                 B    | d   j                           |ddg       dgS )N
wsgi.inputr:   r      data)readess     r!   r   z1IntegrationTests.test_wsgi_input.<locals>.bad_app   s(    lO  "hFGH9r"   r   r   r_   r   r   s       r!   test_wsgi_inputz IntegrationTests.test_wsgi_input   sX    	 Yw/0SJ
 	 	NNR "2	
r"   c                 J   d }t        t        |            \  }}| j                  |j                  d             t        j
                  j                         d   j                  d      }t               j                  d      }|dz   |z   }| j                  d|z   dz   |       y )Nc                      |dddg       dgS )Nr:   r   r>   zWed, 24 Dec 2008 13:29:32 GMTr   r-   r   s     r!   rU   z3IntegrationTests.test_bytes_validation.<locals>.app   s!    h=9  9r"   z"GET / HTTP/1.0" 200 4
r   ascii   /s(   HTTP/1.0 200 OK
Server: WSGIServer/0.2 sV   
Content-Type: text/plain; charset=utf-8
Date: Wed, 24 Dec 2008 13:29:32 GMT

data)
r\   r   r   r   rQ   rk   rl   rK   r   rm   )r   rU   rY   r[   verpyro   s          r!   test_bytes_validationz&IntegrationTests.test_bytes_validation   s    	 Ys^,S%?@Akk!!$++G4#%,,W5T	C+,125 	r"   c                     d }t        t        |      d      \  }}| j                  dt        j                  ddddg|j                                y )	Nc                 B     |dddg       | d   j                  d      gS )Nr:   r;   r   rH   latin1rK   r   s     r!   rU   z-IntegrationTests.test_cp1252_url.<locals>.app   s1    h.9  kN))(344r"   s   GET /%80 HTTP/1.0rz   s   HTTP/1.0 200 OKs   Content-Type: text/plains#   Date: Wed, 24 Dec 2008 13:29:32 GMTr"   s   /)r\   r   rm   r   ANYrv   )r   rU   rY   r[   s       r!   test_cp1252_urlz IntegrationTests.test_cp1252_url   sV    	5 cN!9;S"+6 NN		r"   c                   	
 t        j                  t        d      	d } G d dt        t              }t        t        j                  d||      | j                  j                         t        j                         fd}t        j                  t        j                  |      }| j                  t        j                  t        j                  |       d 
t        j                         	
fd}t        j                  |	      }|j                          j!                          |j#                          | j%                  
t         j&                  d
z
         y )Npthread_killc                 <     |dg        dt         j                  z  gS )Nr:       )r   SOCK_MAX_SIZEr@   s     r!   rU   z4IntegrationTests.test_interrupted_write.<locals>.app   s     8R(G11122r"   c                       e Zd Zy)<IntegrationTests.test_interrupted_write.<locals>.WsgiHandlerNr)   r*   r+   r-   r"   r!   WsgiHandlerr          r"   r   r   )handler_classc                 &    j                          y r   )set)signumframeinterrupteds     r!   signal_handlerz?IntegrationTests.test_interrupted_write.<locals>.signal_handler   s    OOr"   c                  v   t        j                   } | j                  dd       | j                         5 }|j	                  d       	  t
        j                         j                  t        d            rn4t        |j	                               d d d        | j                          y # 1 sw Y   xY w)NGETrj   d      )timeout)r   r   r1   getresponser   signalSIGUSR1waitfloatlenclose)httpresponser   main_threadr   receivedrW   s     r!   
run_clientz;IntegrationTests.test_interrupted_write.<locals>.run_client  s    !6#8#89DLL$!!# 0xc"  fnn="''a'9 
 x}}/0 JJL0 0s   A B//B8)targetr   )r   get_attributer   r   r   r   r   HOST
addCleanupserver_close	threadingEventr   	get_identThreadstarthandle_requestrJ   rm   r   )r   rU   r   r   originalr   
backgroundr   r   r   r   rW   s          @@@@@r!   test_interrupted_writez'IntegrationTests.test_interrupted_write   s    ,,V^D	3	-/A 	 ]//C{S++,oo'	 ==@v~~x@))+	 	$ %%Z8
7#8#83#>?r"   N)T)r)   r*   r+   rp   rs   rw   r{   r}   r   r   r   r   r   r   r-   r"   r!   rh   rh   i   s;    

?
0

D*
*./@r"   rh   c                   j    e Zd Zd ZddZd Zd ZddZd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zy)UtilityTestsc                     ||d}t        j                  |       | j                  t        j                  |      |       | j                  |d   |       | j                  |d   |       |S )NSCRIPT_NAMErH   rH   r   )r   r   rm   shift_path_info)r   sn_inpi_inpartsn_outpi_outenvs          r!   
checkShiftzUtilityTests.checkShift#  sd    "u5##C(--c248[)&1]+F3
r"   Nc                 `   i }t        j                  |       t        |t              r| j	                  ||   t               n?t        |t
              r| j	                  ||   t
               n| j                  ||   |       ||i}t        j                  |       | j                  ||   |       y r   )r   r   
isinstancer   assertIsInstancer   rm   assertIs)r   keyvaluealtr   s        r!   checkDefaultzUtilityTests.checkDefault+  s    ##C(eX&!!#c(H5g&!!#c(73SXu- 3i##C(c#h$r"   c                 X    t        j                  |       | j                  ||   |       y r   )r   r   rm   )r   r   r   kws       r!   checkCrossDefaultzUtilityTests.checkCrossDefault;  s$    ##B'C'r"   c                 x    t        j                  |       | j                  t        j                  |      |       y r   )r   r   rm   application_uri)r   urir   s      r!   checkAppURIzUtilityTests.checkAppURI?  s+    ##B'--b1#6r"   c                 z    t        j                  |       | j                  t        j                  ||      |       y r   )r   r   rm   request_uri)r   r   queryr   s       r!   checkReqURIzUtilityTests.checkReqURIC  s-    ##B'))"U3C8r"   c                 B   ||fd}t        ||        |       }| j                  |j                  j                         |D ]  } | j                  |j                  j                         |j	                          | j                  |j                  j                         y )Nc                 @    t        j                  t        |       |      S r   )r   FileWrapperr   )textsizes     r!   rb   z%UtilityTests.checkFW.<locals>.make_itI  s    ##HTN488r"   )rf   assertFalsefilelikeclosedr   r   )r   r   r   rc   rb   rd   re   s          r!   checkFWzUtilityTests.checkFWG  s    4 	9 	WU+Y++, 	D	 	++,

**+r"   c                     | j                  ddddd       | j                  ddddd       | j                  ddd dd       | j                  ddddd       | j                  dd	ddd       y )
NrN   rj   z/xx/az/x/yz/a/x/yz/x/r   r5   s    r!   testSimpleShiftszUtilityTests.testSimpleShiftsY  sf    3C,4dB/Bc2.VS&$7US&#6r"   c                    | j                  ddddd       | j                  ddddd       | j                  dddd	d       | j                  dd
dd	d       | j                  dddd	d       | j                  dddd	d       | j                  ddddd       | j                  ddddd       | j                  ddddd       | j                  ddddd       | j                  ddd dd       y )Nz/a/bz/../yz..r  r  rN   z//yyz/a/b/yz//y/rj   z/./yz/./y/z///./..//y/.//z/y/z///z/a/b/z/.//z/x//r  z/a/b/xz/.r  r5   s    r!   testNormalizedShiftsz!UtilityTests.testNormalizedShifts`  s    tT:GT2t4sHb9Xs;Xr:h< 0$eDr7B7GR8Xs;dFB7r"   c                     ddddddddd	d
ddt               fdt               fdfD ]  \  }}| j                  ||        y )N)SERVER_NAMErP   )SERVER_PORT80)SERVER_PROTOCOLHTTP/1.0)	HTTP_HOSTrP   )REQUEST_METHODr   )r   rN   )rH   rj   )wsgi.versionr   r   )wsgi.run_oncer   )wsgi.multithreadr   )wsgi.multiprocessr   r   wsgi.errors)wsgi.url_schemer   )r   r   r   )r   r   r   s      r!   testDefaultszUtilityTests.testDefaultsm  sa    '!*%$# #$79%HJ'&
 	)JC  c%(!	)r"   c                    | j                  ddd       | j                  ddd       | j                  ddd       | j                  ddd	       | j                  dd
d       | j                  ddd       | j                  ddd       y )Nr  zfoo.barr  r  httpsonHTTPS1yesr   foor  r  443)r   r5   s    r!   testCrossDefaultszUtilityTests.testCrossDefaults  s    {9K0tD0sC0uE0eD}T>}U>r"   c                    | j                  t        j                  i       d       | j                  t        j                  ddi      d       | j                  t        j                  ddi      d       | j                  t        j                  ddi      d       | j                  t        j                  ddi      d       y )Nr   r"  r%  r   r  r$  r#  )rm   r   guess_schemer5   s    r!   testGuessSchemezUtilityTests.testGuessScheme  s    **2.7**GE?;VD**GD>:GD**GE?;WE**GC=97Cr"   c                 6   | j                  d       | j                  dd       | j                  dd       | j                  ddd	
       | j                  dd       | j                  ddd       | j                  dd       | j                  ddd        y )Nhttp://127.0.0.1/http://127.0.0.1/spam/spamr   http://127.0.0.1/sp%E4m   /spämzhttp://spam.example.com:2071/zspam.example.com:20712071)r  r  zhttp://spam.example.com/zspam.example.comr  rP   )r  r  zhttps://127.0.0.1/r   r!  zhttp://127.0.0.1:8000/8000)r  r  )r   r5   s    r!   testAppURIszUtilityTests.testAppURIs  s    ,-0gF2
K8-6 	 	C3* 	 	,,!/A 	 	C-T:1v 	 	r"   c                    | j                  d       | j                  dd       | j                  dd       | j                  ddd	       | j                  d
dd	       | j                  ddd	       | j                  ddd	       | j                  dddd       | j                  dddd       | j                  ddddd       y )Nr,  r-  r.  r/  r0  r1  zhttp://127.0.0.1/spammity/spamz	/spammityr   z http://127.0.0.1/spammity/sp%E4mz"http://127.0.0.1/spammity/spam;hamz	/spam;hamz/http://127.0.0.1/spammity/spam;cookie=1234,5678z/spam;cookie=1234,5678z%http://127.0.0.1/spammity/spam?say=nizsay=ni)r   rH   rG   z'http://127.0.0.1/spammity/spam?s%E4y=nizs%E4y=nir   )r   r5   s    r!   testReqURIszUtilityTests.testReqURIs  s   ,-0gF2
K9#w 	 	8;#z 	 	;=#{ 	 	<J#/G 	 	I@#wH 	 	NB#wJ 	 	P91#wH 	 	Nr"   c                 .    | j                  ddddg       y )Nږxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzx   xxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz)r  r5   s    r!   testFileWrapperzUtilityTests.testFileWrapper  s    XsXh$78r"   c                    dj                         D ][  }||j                         |j                         |j                         fD ]&  }| j	                  t        j                  |             ( ] dj                         D ][  }||j                         |j                         |j                         fD ]&  }| j                  t        j                  |             ( ] y )NzbConnection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgradez4Accept Cache-Control Date Pragma Trailer Via Warning)rl   titleupperlowerr   r   is_hop_by_hopr   )r   hopr   s      r!   testHopByHopzUtilityTests.testHopByHop  s    4
%'	9C CIIKciikA 9 2 23 789		9 C
%'	:C CIIKciikA :  !3!3C!89:	:r"   r   )r   )r)   r*   r+   r   r   r   r   r   r  r	  r  r  r'  r*  r4  r6  r<  rC  r-   r"   r!   r   r   !  sO    % (79,$78)&?DN&9:r"   r   c                       e Zd Zd Zd Zd Zy)HeaderTestsc           	         dg}| j                  t        t                     d       | j                  t        t        g             d       | j                  t        t        |d d              d       | j                  t        |d d        j                         dg       | j                  t        |d d        j	                         dg       | j                  t        |d d        j                         |       | j                  t        |      j                         |       t               }|d= d|d<   |j                  |j                  |j                  |j                  fD ]^  }| j                   |d             | j                   |d             | j                   |d	             | j                   |d             ` | j                  |d   d       d
|d<   | j                  |d	   d
       | j                  |j                  d      d
g       | j                  |j                  dd      d
       | j                  |j                  dd      d       | j                  |j                  dd      d
       | j                  |j                  dd      d       | j                  |d   d
       | j                  |d   d       y )N)r  r  r   r   r  r  r%  barFooFOObazwheezoo)rm   r   r	   keysvaluesitemsassertIsNot__contains__getget_all__getitem__r   r   
setdefault)r   testhms       r!   testMappingInterfacez HeaderTests.testMappingInterface  s@   {WY+WR[)!,WT!W-.q1a)..03%8a)002SE:a)//148,,.5IeH%		1==@ 	'AOOAeH%OOAeH%OOAeH%QuX&		' 	5%(%5%(5)5'2uV,e4uV,f5eF3U;eF3V<5%(5&)r"   c                 :    | j                  t        t        d       y )Nr%  )assertRaises	TypeErrorr	   r5   s    r!   testRequireListzHeaderTests.testRequireList  s    )We4r"   c                 z   t               }| j                  t        |      d       |j                  ddd       | j                  |d   d       | j                  t        |      d       |j                  ddd 	       | j                  |j	                  d      dd
g       | j                  t        |      d       y )Nz
r%  rG  spam)rJ  zbar; baz="spam"zfoo: bar; baz="spam"

rH  )cheesezbar; cheesez*foo: bar; baz="spam"
Foo: bar; cheese

)r	   rm   str
add_headerrS  r   rW  s     r!   
testExtraszHeaderTests.testExtras  s    IQ'	U5V,5#45Q >?	U5-5).	0 	Q	
r"   N)r)   r*   r+   rY  r]  rd  r-   r"   r!   rE  rE    s    *@5
r"   rE  c                   V    e Zd ZdZ eej                  j                               Zd Z	y)ErrorHandlerz/Simple handler subclass for testing BaseHandlerc           	          t        |       t        j                  | t               t               t	               |dd       y )NT)multithreadmultiprocess)r   r   r   r   r   )r   r   s     r!   r   zErrorHandler.__init__  s.    r"')WY
B4	
r"   N)
r)   r*   r+   r,   dictosrA   rO  
os_environr   r-   r"   r!   rf  rf    s#    9
 bjj&&()J
r"   rf  c                       e Zd ZdZd Zy)TestHandlerzASimple handler subclass for testing BaseHandler, w/error passthruc                       r   r-   r5   s    r!   handle_errorzTestHandler.handle_error  s    r"   N)r)   r*   r+   r,   rp  r-   r"   r!   rn  rn  	  s
    Kr"   rn  c                   ^    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zy)HandlerTestsi  c                 @   dddddd}t        j                  t        d|      5  t        dd	
      }|j                          d d d        dD ]1  }| j	                  t        d|z         |j                  d|z             3 i }t        |       |j                         D ]  \  }}||vs|||<    |j                  dd	ddj                         |j                         dddddt        j                  d       | j                  |j                  |       y # 1 sw Y   xY w)Nz/my/homez/my/pathzfr_FR.UTF-8test_script_nametest_server_name)HOMEPATHLANGr   r  rl  Yz/override/home)Xrv  )rk   rh  ri  run_oncefile_wrapperwsgi_zwsgi.rN   rP   r  Fr   T)rz  rv  r   r  r   r  r  r  r  r  r  zwsgi.file_wrapper)r   	swap_attrrn  r&   rm   getattrrA   r   rO  update	get_stdin
get_stderrr   r   assertDictEqual)r   rl  handlerattrexpectedr   r   s          r!   testEnvironzHandlerTests.testEnviron  sB    ! .-	

 {L*E 	$!C.>?G!!#	$% 	>DWWgn=$__Wt^<>	> x( %**, 	&JC(" %	& 	$ & "++-"--/""% $!%!%!1!1%
 	( 	W__h7Q	$ 	$s   DDc                     t        d d d i       }|j                          dD ]  }| j                  ||j                           y )N)r  r   r  )r   r&   assertInrA   )r   rW  r   s      r!   testCGIEnvironzHandlerTests.testCGIEnvironJ  s=    4T"-	A 	*CMM#qyy)	*r"   c                     t        d      }|j                          | j                  |j                  d   d       t               }|j                          | j                  |j                  d   d       y )Nr   r!  r  r  r   )rn  r&   rm   rA   rc  s     r!   
testSchemezHandlerTests.testSchemeP  sY    
D
!1??#4#45g>
-*#45f=r"   c                     t               }dD ]"  }| j                  t        t        ||             $ | j                  t        |j                  d       y )N)_flushr  r  add_cgi_varsrV  )r
   r[  NotImplementedErrorr  _write)r   rW  names      r!   testAbstractMethodsz HandlerTests.testAbstractMethodsV  sN    M
 	DD 171T?C	D 	-qxx@r"   c                    d }d }d }d }t               }|j                  |       | j                  |j                  j	                         dj                  d             t               }|j                  |       | j                  |j                  j	                         dj                  d             t               }|j                  |       | j                  |j                  j	                         d       t               }|j                  |       | j                  |j                  j	                         d	       y )
Nc                 >     |dg        | d   j                  d      gS Nr:   r  rI   r   r   s     r!   trivial_app1z4HandlerTests.testContentLength.<locals>.trivial_app1a  s%    hrN'(//=>>r"   c                 H      |dg       | d   j                  d             g S r  r   r   s     r!   trivial_app2z4HandlerTests.testContentLength.<locals>.trivial_app2e  s(    AhrN1./66|DEIr"   c                 8     |dg        dj                  d      gS )Nr:   u   тестzutf-8r   r   s     r!   trivial_app3z4HandlerTests.testContentLength.<locals>.trivial_app3i  s    hrN.55g>??r"   c                      |ddg       g S )Nr:   )zContent-Length12345r-   r   s     r!   trivial_app4z4HandlerTests.testContentLength.<locals>.trivial_app4m  s    h345Ir"   z)Status: 200 OK
Content-Length: 4

httprI   zStatus: 200 OK

https-   Status: 200 OK
Content-Length: 8

тестs)   Status: 200 OK
Content-Length: 12345

)rn  runrm   stdoutrT   rK   )r   r  r  r  r  rW  s         r!   testContentLengthzHandlerTests.testContentLength^  s	   	?		@	
 M	l**, F<(		* M	l**,F<(	*
 M	l**,0	1 M	l**,	r"   c                 f   d }d }t               }|j                  |       | j                  |j                  j	                         dj                  d             | j                  |j                  j	                         d       t               }|j                  |       | j                  |j                  j	                         d|j                  t        |j                        fz  j                  d      |j                  z          | j                  d|j                  j	                                y )Nc                      |dg        g S Nr:   r-   r   s     r!   non_error_appz8HandlerTests.testBasicErrorOutput.<locals>.non_error_app      hrNIr"   c                     t        d      )N This should be caught by handlerr_   r   s     r!   	error_appz4HandlerTests.testBasicErrorOutput.<locals>.error_app  s     !CDDr"   %Status: 200 OK
Content-Length: 0

rI   rN   z<Status: %s
Content-Type: text/plain
Content-Length: %d

r_   )rf  r  rm   r  rT   rK   rR   error_statusr   
error_bodyr  )r   r  r  rW  s       r!   testBasicErrorOutputz!HandlerTests.testBasicErrorOutput  s    		E N	m**,F<(	* 	**,R0N	i**, nnS%678 :@9Mll		 	&(9(9(;<r"   c                    dfd}t               }|j                  |       | j                  |j                  j	                         dj                  d      z          | j                  d|j                  j	                                y )N   Some output has been sentc                 8      |dg              t        d      )Nr:   r  r  )r   r   MSGs     r!   r  z4HandlerTests.testErrorAfterOutput.<locals>.error_app  s    AhrN3 !CDDr"   zStatus: 200 OK

rI   r_   )rf  r  rm   r  rT   rK   r  rR   )r   r  rW  r  s      @r!   testErrorAfterOutputz!HandlerTests.testErrorAfterOutput  st    *	E N	i**,6,',	. 	&(9(9(;<r"   c           
         d }d}dj                  d      }dD ]`  }|xr d|z  xs d}dD ]J  }d	D ]A  }t        |
      }d|_        ||_        ||_        |j                  |       | j                  ||j                  j                                t        |
      }d|_        ||_        ||_        |j                  |       |dk(  r+| j                  |j                  j                         d       | j                  t        j                  |||fz  j                  d      |j                  j                               |||fz  j                  d      |j                  j                         f       D M c y )Nc                      |dg        g S r  r-   r   s     r!   r  z5HandlerTests.testHeaderFormats.<locals>.non_error_app  r  r"   zfHTTP/%s 200 OK\r\nDate: \w{3}, [ 0123]\d \w{3} \d{4} \d\d:\d\d:\d\d GMT\r\n%sContent-Length: 0\r\n\r\nr  rI   )z
FooBar/1.0NzServer: %s
rN   )z1.0z1.1)HTTP/0.9r  zHTTP/1.1)r  FTr  r"   )rK   rn  origin_serverhttp_versionserver_softwarer  rm   r  rT   r   rerc   )	r   r  stdpatshortpatsswswrk   protorW  s	            r!   testHeaderFormatszHandlerTests.testHeaderFormats  sn   	
3 	 @
&
 	 & 	C/)C/52B' ? E#E:A&+AO%,AN(+A%EE-($$Xahh.?.?.AB#E:A&*AO%,AN(+A%EE-(j((():):)<SAHHfgb\&9%A%A,%O ! 1 1 35$gb\199,G ! 1 1 35#	r"   c                     d }t               }|j                  |       | j                  d|j                  j	                                y )Nc                      |ddg       dgS )Nr:   r   r   r-   r   s     r!   rU   z'HandlerTests.testBytesData.<locals>.app  s    h=  9r"   sR   Status: 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 4

data)rn  r  rm   r  rT   )r   rU   rW  s      r!   testBytesDatazHandlerTests.testBytesData  s@    	 M	c
 
 HH	!r"   c                     ddidfd}t               }|j                  |       | j                  d   d       y )Nclose_calledFr  c                 R      |dg               G fddt               } |       S )Nr:   c                       e Zd Zd Z fdZy)HHandlerTests.testCloseOnError.<locals>.error_app.<locals>.CrashyIterablec              3   *   K   	 d t        d      w)Ns   blahr  r  r5   s    r!   __iter__zQHandlerTests.testCloseOnError.<locals>.error_app.<locals>.CrashyIterable.__iter__  s     %,-OPPs   c                     dd<   y )NTr  r-   )r   side_effectss    r!   r   zNHandlerTests.testCloseOnError.<locals>.error_app.<locals>.CrashyIterable.close  s    37L0r"   N)r)   r*   r+   r  r   )r  s   r!   CrashyIterabler    s    Q8r"   r  )object)r   r   r  r  r  s      r!   r  z0HandlerTests.testCloseOnError.<locals>.error_app  s*    AhrN38 8 "##r"   T)rf  r  rm   )r   r  rW  r  r  s      @@r!   testCloseOnErrorzHandlerTests.testCloseOnError  sA    &.*		$ N	in5t<r"   c                 4   t                G fdd      }ddi}t        t                |       t        j                  |      }d}| j                  t        |      5  |j                  t               d d d        | j                  d       y # 1 sw Y   xY w)Nc                       e Zd Z fdZd Zy)4HandlerTests.testPartialWrite.<locals>.PartialWriterc                 F    |d d }j                  |       t        |      S )N   )extendr   )r   bpartialwrittens      r!   writez:HandlerTests.testPartialWrite.<locals>.PartialWriter.write
  s$    BQ%w'7|#r"   c                      y r   r-   r5   s    r!   flushz:HandlerTests.testPartialWrite.<locals>.PartialWriter.flush  s    r"   N)r)   r*   r+   r  r  )r  s   r!   PartialWriterr  	  s    $
r"   r  r  r  zshould not do partial writesss   HTTP/1.0 200 OK
Content-Type: text/plain
Date: Mon, 05 Jun 2006 18:49:54 GMT
Content-Length: 13

Hello, world!)
	bytearrayr   r   rQ   rR   assertWarnsRegexDeprecationWarningr  rC   rm   )r   r  rA   rW  msgr  s        @r!   testPartialWritezHandlerTests.testPartialWrite  s    +	 	 %j1')]_cjj'J,""#5s; 	EE)	  		 	s   BBc                 R   ddi}t         t        t        fD ]  | j                        5   G fdd      }t	               }t        t                |       ||      }|j                  t               | j                  |j                                d d d         y # 1 sw Y   xY w)Nr  r  	exceptionc                       e Zd Z fdZy)EHandlerTests.testClientConnectionTerminations.<locals>.AbortingWriterc                     r   r-   )r   r  r  s     r!   r  zKHandlerTests.testClientConnectionTerminations.<locals>.AbortingWriter.write(  s    'r"   Nr)   r*   r+   r  r  s   r!   AbortingWriterr  '  s    (r"   r  )ConnectionAbortedErrorBrokenPipeErrorConnectionResetErrorr   r   r   r   r  rC   r   rT   )r   rA   r  rR   rW  r  s        @r!    testClientConnectionTerminationsz-HandlerTests.testClientConnectionTerminations  s    $j1" 
 	4I
 	2 	4( ( "!')^-=vwOi   !23	4 	4	4
	4 	4s   A&BB&	c                     G d dt                G fdd      }t               }ddi}t        t                |       ||      }|j	                  t
               | j                  d|j                                | j                  |j                         | j                  |j                         | j                  |j                         | j                  |j                         y )Nc                       e Zd Zy)KHandlerTests.testDontResetInternalStateOnException.<locals>.CustomExceptionNr   r-   r"   r!   CustomExceptionr  2  r   r"   r  c                       e Zd Z fdZy)JHandlerTests.testDontResetInternalStateOnException.<locals>.AbortingWriterc                     r   r-   )r   r  r  s     r!   r  zPHandlerTests.testDontResetInternalStateOnException.<locals>.AbortingWriter.write:  s	    %%r"   Nr  )r  s   r!   r  r  9  s    &r"   r  r  r  )
ValueErrorr   r   r   r  rC   r  rT   assertIsNotNoneresultheadersr   rA   )r   r  rR   rA   rW  r  s        @r!   %testDontResetInternalStateOnExceptionz2HandlerTests.testDontResetInternalStateOnException1  s    	j 		& 	& $j1')^%5vwG	i'):; 	QXX&QYY'QXX&QYY'r"   N)r)   r*   r+   maxDiffr  r  r  r  r  r  r  r  r  r  r  r  r  r-   r"   r!   rr  rr    sL    G48l*>A0d=8=)V! =$24$(r"   rr  __main__)6unittestr   rV  r   test.supportr   test.test_httpserversr   r   wsgiref.utilr   wsgiref.headersr	   wsgiref.handlersr
   r   r   wsgirefr   wsgiref.validater   wsgiref.simple_serverr   r   r   http.clientr   ior   r   r   socketserverr   platformr   rk  r  r   rQ   r   r   r/   rC   rL   r\   rf   rh   r   rE  rf  rn  rr  r)   mainr-   r"   r!   <module>r
     s      & 5  / # G G  & @ - & 0 0 # * 	 	  
   $  "7 *D*u@x u@p`:8 `:D5
( 5
n
> 
, x(8 x(v	 zHMMO r"   