kWc@sddlmZddlmZddlmZddlmZddlm Z m Z ddl m Z ddl mZddlmZmZmZdd lmZdd lmZdd lmZdd lZdd lZdd lZdd lmZdefdYZdefdYZdZeddZdZ defdYZ!defdYZ"defdYZ#de fdYZ$de$fdYZ%d efd!YZ&d"Z'e(d#kre'nd S($i(t nested_scopes(tTestCase(tsetup_testing_defaults(tHeaders(t BaseHandlertBaseCGIHandler(tutil(t validator(t WSGIServertWSGIRequestHandlertdemo_app(t make_server(tStringIO(t BaseServerN(t test_supportt MockServercBs eZdZdZdZRS(sNon-socket HTTP servercCs!tj||||jdS(N(R t__init__t server_bind(tselftserver_addresstRequestHandlerClass((s'/usr/lib/python2.7/test/test_wsgiref.pyRscCs/|j\}}||_||_|jdS(N(Rt server_namet server_portt setup_environ(Rthosttport((s'/usr/lib/python2.7/test/test_wsgiref.pyRs  (t__name__t __module__t__doc__RR(((s'/usr/lib/python2.7/test/test_wsgiref.pyRs t MockHandlercBs eZdZdZdZRS(sNon-socket HTTP handlercCs%|j|_|j\|_|_dS(N(trequestt connectiontrfiletwfile(R((s'/usr/lib/python2.7/test/test_wsgiref.pytsetup"s cCsdS(N((R((s'/usr/lib/python2.7/test/test_wsgiref.pytfinish&s(RRRR"R#(((s'/usr/lib/python2.7/test/test_wsgiref.pyR s cCs|dddgdgS( Ns200 OKs Content-Types text/plaintDatesMon, 05 Jun 2006 18:49:54 GMTs Hello, world!(s Content-Types text/plain(R$sMon, 05 Jun 2006 18:49:54 GMT((tenvirontstart_response((s'/usr/lib/python2.7/test/test_wsgiref.pyt hello_app*s sGET / HTTP/1.0 cCstdd|tt}t|tttjf\}}}}|t_z|j||fdWd|t_X|j|jfS(NtiPs 127.0.0.1i"(s 127.0.0.1i"(R RRR tsyststderrtfinish_requesttgetvalue(tapptdatatservertinptoutterrtolderr((s'/usr/lib/python2.7/test/test_wsgiref.pyt run_amock1s-  cCs|}d}x1|D])}|||ks5tn|d7}qWy ||Wntk rbnXtd|yttfWntk rnX|}t||k rtnx)|D]!}|j|kstqqWy|jWntk rnXtd|dS(sUtility to compare a generic 2.1/2.2+ iterator with an iterable If running under Python 2.2+, this tests the iterator using iter()/next(), as well as __getitem__. 'make_it' must be a function returning a fresh iterator to be tested (since this may test the iterator twice).iisToo many items from __getitem__sToo many items from .next()N(tAssertionErrort IndexErrortitert StopIterationt NameErrortnext(tmake_ittmatchtittntitem((s'/usr/lib/python2.7/test/test_wsgiref.pytcompare_generic_iter>s4           tIntegrationTestscBs8eZedZdZdZdZdZRS(cCs=|j|dtjjdd|r-dp0dddS(Ns/HTTP/1.0 200 OK Server: WSGIServer/0.1 Python/isA Content-Type: text/plain Date: Mon, 05 Jun 2006 18:49:54 GMT sContent-Length: 13 R(s Hello, world!(t assertEqualR)tversiontsplit(RR1t has_length((s'/usr/lib/python2.7/test/test_wsgiref.pyt check_helloes (cCs t\}}|j|dS(N(R4RF(RR1R2((s'/usr/lib/python2.7/test/test_wsgiref.pyttest_plain_hellopscCs?tddddd\}}|j|jdddS(NR.sGET txis HTTP/1.0 is!HTTP/1.0 414 Request-URI Too Long(R4RBt splitlines(RR1R2((s'/usr/lib/python2.7/test/test_wsgiref.pyttest_request_lengthts!cCs/ttt\}}|j|dtdS(NRE(R4RR'RFtFalse(RR1R2((s'/usr/lib/python2.7/test/test_wsgiref.pyttest_validated_helloyscCsUd}tt|\}}|j|jd|j|jdddS(NcSs|dddgS(Ns200 OKs Content-Types text/plains Hello, world!(s Content-Types text/plain((R%R&((s'/usr/lib/python2.7/test/test_wsgiref.pytbad_apps s;A server error occurred. Please contact the administrator.is]AssertionError: Headers (('Content-Type', 'text/plain')) must be of type list: (R4Rt assertTruetendswithRBRI(RRMR1R2((s'/usr/lib/python2.7/test/test_wsgiref.pyttest_simple_validation_error~s    (RRtTrueRFRGRJRLRP(((s'/usr/lib/python2.7/test/test_wsgiref.pyRAcs    t UtilityTestscBseZdZddZdZdZddZdZdZ dZ d Z d Z d Z d Zd ZdZdZRS(cCsfi|d6|d6}tj||jtj|||j|d||j|d||S(Nt SCRIPT_NAMEt PATH_INFO(RRRBtshift_path_info(Rtsn_intpi_intparttsn_outtpi_outtenv((s'/usr/lib/python2.7/test/test_wsgiref.pyt checkShifts  cCsi}tj|t|tr9|j||tn|j|||i||6}tj||j|||dS(N(RRt isinstanceR tassertIsInstanceRBtassertIs(RtkeytvaluetaltR[((s'/usr/lib/python2.7/test/test_wsgiref.pyt checkDefaults   cKs%tj||j|||dS(N(RRRB(RR`Ratkw((s'/usr/lib/python2.7/test/test_wsgiref.pytcheckCrossDefaults cKs*tj||jtj||dS(N(RRRBtapplication_uri(RturiRd((s'/usr/lib/python2.7/test/test_wsgiref.pyt checkAppURIs icKs-tj||jtj|||dS(N(RRRBt request_uri(RRgtqueryRd((s'/usr/lib/python2.7/test/test_wsgiref.pyt checkReqURIs cCs}||d}t|||}|j|jjx|D]}q?W|j|jj|j|j|jjdS(NcSstjt||S(N(Rt FileWrapperR (ttexttsize((s'/usr/lib/python2.7/test/test_wsgiref.pyR;s(R@t assertFalsetfileliketclosedtcloseRN(RRmRnR<R;R=R?((s'/usr/lib/python2.7/test/test_wsgiref.pytcheckFWs    cCs|jddddd|jddddd|jddddd|jddddd|jdd ddddS( NR(t/s/xRHs/as/x/ys/a/xs/ys/x/(R\tNone(R((s'/usr/lib/python2.7/test/test_wsgiref.pyttestSimpleShiftss cCs|jddddd|jddddd|jdddd d|jdd dd d |jdd dd d|jdd dd d |jddddd|jddddd|jddddd|jddddd |jddddddS(Ns/a/bs/../ys..s/as/yR(s//ytys/a/b/ys//y/Rts/./ys/./y/s///./..//y/.//s/y/s///s/a/b/s/.//s/x//RHs/a/b/xs/.(R\Ru(R((s'/usr/lib/python2.7/test/test_wsgiref.pyttestNormalizedShiftsscCspxidddddddddfd d!d"dtd fdtfd#gD]\}}|j||qLWdS($Nt SERVER_NAMEs 127.0.0.1t SERVER_PORTt80tSERVER_PROTOCOLsHTTP/1.0t HTTP_HOSTtREQUEST_METHODtGETRSR(RTRts wsgi.versioniis wsgi.run_onceswsgi.multithreadswsgi.multiprocesss wsgi.inputs wsgi.errorsswsgi.url_schemethttp(s SERVER_NAMEs 127.0.0.1(s SERVER_PORTR{(sSERVER_PROTOCOLsHTTP/1.0(s HTTP_HOSTs 127.0.0.1(R~R(s SCRIPT_NAMER((s PATH_INFORt(ii(s wsgi.run_oncei(swsgi.multithreadi(swsgi.multiprocessi(swsgi.url_schemeR(R Rc(RR`Ra((s'/usr/lib/python2.7/test/test_wsgiref.pyt testDefaultss   cCs|jdddd|jdddd|jdddd|jdddd |jdd dd |jd d dd |jd ddddS(NR}sfoo.barRyswsgi.url_schemethttpstHTTPStont1tyesRtfooRzR{t443(Re(R((s'/usr/lib/python2.7/test/test_wsgiref.pyttestCrossDefaultsscCs|jtjid|jtjidd6d|jtjidd6d|jtjidd6d|jtjidd6ddS(NRRRRRRR(RBRt guess_scheme(R((s'/usr/lib/python2.7/test/test_wsgiref.pyttestGuessSchemes    cCs|jd|jddd|jddd|jddd d d |jd d d|jdddd d|jddd|jdd ddddS(Nshttp://127.0.0.1/shttp://127.0.0.1/spamRSs/spamshttp://127.0.0.1/sp%E4ms/spmshttp://spam.example.com:2071/R}sspam.example.com:2071Rzt2071shttp://spam.example.com/Rysspam.example.coms 127.0.0.1shttps://127.0.0.1/RRshttp://127.0.0.1:8000/t8000(RhRu(R((s'/usr/lib/python2.7/test/test_wsgiref.pyt testAppURIss      c Cs|jd|jddd|jddd|jdddd d|jd ddd d|jd ddd d |jd ddd d|jdddd ddd|jdddd ddd|jddddd ddddS(Nshttp://127.0.0.1/shttp://127.0.0.1/spamRSs/spamshttp://127.0.0.1/sp%E4ms/spmshttp://127.0.0.1/spammity/spams /spammityRTs http://127.0.0.1/spammity/sp%E4ms"http://127.0.0.1/spammity/spam;hams /spam;hams/http://127.0.0.1/spammity/spam;cookie=1234,5678s/spam;cookie=1234,5678s%http://127.0.0.1/spammity/spam?say=nit QUERY_STRINGssay=nis'http://127.0.0.1/spammity/spam?s%E4y=niss%E4y=nii(Rk(R((s'/usr/lib/python2.7/test/test_wsgiref.pyt testReqURIss"           cCs)|jdddddddgdS(Ntxyzi2ixi(i (Rs(R((s'/usr/lib/python2.7/test/test_wsgiref.pyttestFileWrapperscCsxYdjD]K}xB||j|j|jfD]}|jtj|q8Wq WxYdjD]K}xB||j|j|jfD]}|jtj|qWqiWdS(NsbConnection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrades4Accept Cache-Control Date Pragma Trailer Via Warning(RDttitletuppertlowerRNRt is_hop_by_hopRo(RthopRb((s'/usr/lib/python2.7/test/test_wsgiref.pyt testHopByHops++N(RRR\RuRcReRhRkRsRvRxRRRRRRR(((s'/usr/lib/python2.7/test/test_wsgiref.pyRRs            t HeaderTestscBs#eZdZdZdZRS(cCs=d g}|jttgd|jtt|d|jt|jdg|jt|jdg|jt|j||jt|j|tg}|d=d|dStatus: %s Content-Type: text/plain Content-Length: %d %sR5i( RRRBRR,R*t error_statusRt error_bodytassertNotEqualtfind(RRRR((s'/usr/lib/python2.7/test/test_wsgiref.pyttestBasicErrorOutputs       csndfd}t}|j||j|jjd|j|jjjdddS(NsSome output has been sentcs#|dgtddS(Ns200 OKs This should be caught by handler(R5(RR(tMSG(s'/usr/lib/python2.7/test/test_wsgiref.pyRssStatus: 200 OK R5i(RRRBRR,RR*R(RRR((Rs'/usr/lib/python2.7/test/test_wsgiref.pyttestErrorAfterOutputs  c CsYd}d}d}x=d D]5}|r2d|p5d}xdD]}xdD]}td |}t|_||_||_|j||j||jj td |}t |_||_||_|j||d kr|j|jj dqL|j t j |||f|jj |||f|jj fqLWq?WqWdS(NcSs|dggS(Ns200 OK((RR((s'/usr/lib/python2.7/test/test_wsgiref.pyRs sfHTTP/%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\ns%Status: 200 OK Content-Length: 0 s FooBar/1.0s Server: %s R(s1.0s1.1sHTTP/0.9sHTTP/1.0sHTTP/1.1R|(s FooBar/1.0N(s1.0s1.1(sHTTP/0.9sHTTP/1.0sHTTP/1.1(RuRRKt origin_servert http_versiontserver_softwareRRBRR,RQRNtreR<( RRtstdpattshortpattsswtswRCtprotoR((s'/usr/lib/python2.7/test/test_wsgiref.pyttestHeaderFormatss.             "csSitd6dfd}t}|j||jdtdS(Nt close_calledsSome output has been sentcs6|dgdtffdY}|S(Ns200 OKtCrashyIterablecs eZdZfdZRS(css"xtrdVtdqWdS(Ntblahs This should be caught by handler(RQR5(R((s'/usr/lib/python2.7/test/test_wsgiref.pyt__iter__)s cstds8     %*8