{G_Tc@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z ddlmZddl mZddlmZddlmZejdZd d"d YZd efd YZd ejfdYZde jfdYZde jfdYZdefdYZdefdYZdZdZ e j!e"edoej#dkddefdYZ$de jfdYZ%dd Z'e(d!kr e'ndS(#sUnittests for the various HTTPServer modules. Written by Cody A.W. Somerville , Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest. iN(tBaseHTTPRequestHandlert HTTPServer(tSimpleHTTPRequestHandler(tCGIHTTPRequestHandler(tStringIO(t test_supportt threadingtNoLogRequestHandlercBseZdZRS(cGsdS(N((tselftargs((s1/usr/local/lib/python2.7/test/test_httpservers.pyt log_messages(t__name__t __module__R (((s1/usr/local/lib/python2.7/test/test_httpservers.pyRstSocketlessRequestHandlercBs#eZdZdZdZRS(cCst|_d|_dS(NsHTTP/1.1(tFalset get_calledtprotocol_version(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyt__init__#s cCsDt|_|jd|jdd|j|jjddS(Nis Content-Types text/htmls Data (tTrueRt send_responset send_headert end_headerstwfiletwrite(R((s1/usr/local/lib/python2.7/test/test_httpservers.pytdo_GET's    cGsdS(N((RtfmtR ((s1/usr/local/lib/python2.7/test/test_httpservers.pyR .s(R R RRR (((s1/usr/local/lib/python2.7/test/test_httpservers.pyR "s  tTestServerThreadcBs#eZdZdZdZRS(cCs&tjj|||_||_dS(N(RtThreadRtrequest_handlert test_object(RRR((s1/usr/local/lib/python2.7/test/test_httpservers.pyR3s cCsstd|j|_|jjjd|j_|jjjd|_z|jj dWd|jj XdS(Ntiig?(Ri( RRtservertsockett getsocknameRtPORTtserver_startedtsettNonet serve_forevert server_close(R((s1/usr/local/lib/python2.7/test/test_httpservers.pytrun8s cCs|jjdS(N(Rtshutdown(R((s1/usr/local/lib/python2.7/test/test_httpservers.pytstopBs(R R RR(R*(((s1/usr/local/lib/python2.7/test/test_httpservers.pyR2s  t BaseTestCasecBs,eZdZdZddidZRS(cCs`tj|_tjt_tj|_t ||j |_ |j j |jj dS(N(Rtthreading_setupt_threadstEnvironmentVarGuardtostenvironRtEventR#RRtthreadtstarttwait(R((s1/usr/local/lib/python2.7/test/test_httpservers.pytsetUpGs  cCs.|jjtjjtj|jdS(N(R2R*R/R0t__exit__Rtthreading_cleanupR-(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyttearDownOs  tGETcCs>tjd|j|_|jj|||||jjS(Nt localhost(thttplibtHTTPConnectionR"t connectiontrequestt getresponse(Rturitmethodtbodytheaders((s1/usr/local/lib/python2.7/test/test_httpservers.pyR>TsN(R R R5R8R%R>(((s1/usr/local/lib/python2.7/test/test_httpservers.pyR+Fs  tBaseHTTPRequestHandlerTestCasecBsweZdZejdZdZdZdZdZ dZ dZ dZ d Z d Zd ZRS( sZTest the functionality of the BaseHTTPServer focussing on BaseHTTPRequestHandler. sHTTP/1.[0-9]+ 200 OKcCst|_dS(N(R thandler(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyR5`scCsQt|}t}||j_||j_|jj|jd|jS(Ni(RREtrfileRthandle_one_requesttseekt readlines(Rtmessaget input_msgtoutput((s1/usr/local/lib/python2.7/test/test_httpservers.pytsend_typical_requestcs      cCs|j|jjdS(N(t assertTrueRER(R((s1/usr/local/lib/python2.7/test/test_httpservers.pytverify_get_calledlscs;x4dD],|jtfd|DdqWdS(NsServer: sDate: sContent-Type: c3s|]}|jVqdS(N(t startswith(t.0th(t fieldName(s1/usr/local/lib/python2.7/test/test_httpservers.pys qsi(sServer: sDate: sContent-Type: (t assertEqualtsum(RRC((RSs1/usr/local/lib/python2.7/test/test_httpservers.pytverify_expected_headersos cCs#|jj|}|j|dS(N(tHTTPResponseMatchtsearchtassertIsNotNone(Rtresponsetmatch((s1/usr/local/lib/python2.7/test/test_httpservers.pytverify_http_server_responsesscCsV|jd}|j|d|j|dd!|j|j|dddS(NsGET / HTTP/1.1 iiis Data (RMR\RVRORT(Rtresult((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_http_1_1ws  cCsV|jd}|j|d|j|dd!|j|j|dddS(NsGET / HTTP/1.0 iiis Data (RMR\RVRORT(RR]((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_http_1_0~s  cCsG|jd}|jt|d|j|dd|jdS(NsGET / HTTP/0.9 iis Data (RMRTtlenRO(RR]((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_http_0_9scCsV|jd}|j|d|j|dd!|j|j|dddS(Ns(GET / HTTP/1.0 Expect: 100-continue iiis Data (RMR\RVRORT(RR]((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_with_continue_1_0s  cCsB|jddd}|j|dd|j|jjdS(NsGET txiis#HTTP/1.1 414 Request-URI Too Long (RMRTt assertFalseRER(RR]((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_request_lengths(R R t__doc__tretcompileRWR5RMRORVR\R^R_RaRbRe(((s1/usr/local/lib/python2.7/test/test_httpservers.pyRDYs        tBaseHTTPServerTestCasecBseZdeefdYZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZRS(RcBs8eZdZdZdZdZdZdZRS(sHTTP/1.1cCs;|jd|jdd|jdd|jdS(Nis Content-Types text/htmlt Connectiontclose(RRR(R((s1/usr/local/lib/python2.7/test/test_httpservers.pytdo_TESTs cCs;|jd|jdd|jdd|jdS(Nis Content-Types text/htmlRjs keep-alive(RRR(R((s1/usr/local/lib/python2.7/test/test_httpservers.pytdo_KEEPs cCs|jddS(Ni(t send_error(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyt do_KEYERRORscCs;|jd|jdd|jdd|jdS(Nis Content-Types text/htmlRjRk(RRR(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyt do_CUSTOMs (R R Rtdefault_request_versionRlRmRoRp(((s1/usr/local/lib/python2.7/test/test_httpservers.pyRs    cCs6tj|tjd|j|_|jjdS(NR:(R+R5R;R<R"tcontconnect(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyR5s cCs9|jjdd|jj}|j|jddS(NR9t/i(RrR>R?RTtstatus(Rtres((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_commandscCsRd|j_|jjdd|jj|jj}|j|jddS(Ns HTTP/1.1 tXYZBOGUSRti(Rrt _http_vsn_strt putrequestt endheadersR?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_request_line_trimmings   cCsRd|j_|jjdd|jj|jj}|j|jddS(NtFUBARR9Rti(RrRyRzR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_version_boguss   cCsRd|j_|jjdd|jj|jj}|j|jddS(Ns HTTP/9.9.9R9Rti(RrRyRzR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_version_digitss   cCsRd|j_|jjdd|jj|jj}|j|jddS(NRR9Rti(RrRyRzR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_version_none_gets   cCsRd|j_|jjdd|jj|jj}|j|jddS(NRtCUSTOMRti(RrRyRzR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_version_nones   cCs^d|j_d|j_|jjdd|jj|jj}|j|jddS(NicsHTTP/9.9R9Rti(Rrt _http_vsnRyRzR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_version_invalids    cCsRd|j_|jjdd|jj|jj}|j|jddS(NRi(RrRyRzR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_send_blanks   cCsY|jjdd|jjdd|jj|jj}|j|jddS(NR9RtRjRki(RrRzt putheaderR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_header_closes  cCsed|j_|jjdd|jjdd|jj|jj}|j|jddS(NsHTTP/1.1R9RtRjs keep-alivei(RrRyRzRR{R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_head_keep_alives   cCs9|jjdd|jj}|j|jddS(NtTESTRti(RrR>R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_handlerscCse|jjdd|jj}|j|jdd|jjdd|j|jjdS(NtKEEPRtRjs keep-aliveR(RrR>R?RTt getheadert addCleanupRk(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_return_header_keep_alives cCs9|jjdd|jj}|j|jddS(NtKEYERRORRti(RrR>R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_internal_key_error scCs9|jjdd|jj}|j|jddS(NRRti(RrR>R?RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_return_custom_statuss(R R RRRR5RwR|R~RRRRRRRRRRR(((s1/usr/local/lib/python2.7/test/test_httpservers.pyRis               tSimpleHTTPServerTestCasecBsZeZdeefdYZdZdZddZdZ dZ dZ RS( RcBseZRS((R R (((s1/usr/local/lib/python2.7/test/test_httpservers.pyRscCstj|tj|_tj}tj|d|_tj d||_ tj j |j |_ ttj j|j dd}|j|j|jdS(NsWe are the knights who say Ni!tdirttesttwb(R+R5R/tgetcwdtcwdttempfilet gettempdirtchdirtdatatmkdtempttempdirtpathtbasenamet tempdir_nametopentjoinRRk(Rt basetempdirttemp((s1/usr/local/lib/python2.7/test/test_httpservers.pyR5s    !cCsQz<tj|jytj|jWntk r:nXWdtj|XdS(N( R/RRtshutiltrmtreeRtOSErrorR+R8(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyR8's cCsY|j}|j||j|j||j|j|rU|j||ndS(N(treadRNRTRuRYtreason(RRZRuRRB((s1/usr/local/lib/python2.7/test/test_httpservers.pytcheck_status_and_reason1s   cCs|j|jd}|j|dd|j|j|jd}|j|d|j|jd}|j|d|j|j}|j|d|jd}|j|d|jd}|j|dttjj|jd d }|jd|jd}|j|dtjd krtj d krtj |j d |j|jd}|j|dtj |j dnWdQXdS(Ns/testiRs/test/iRti-s/ThisDoesNotExisttThisDoesNotExists index.htmltwtposixiis/ThisDoesNotExists/ThisDoesNotExist/( R>RRRRR/RRtnametgeteuidtchmodR(RRZtfp((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_get9s($!cCsq|j|jddd}|j|d|j|jdtt|j|j|jdddS(Ns/testRAtHEADiscontent-lengths content-typesapplication/octet-stream(R>RRRTRtstrR`R(RRZ((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_headSscCss|jddd}|j|d|jddd}|j|d|jddd}|j|ddS(NRtRAtFOOitgettGETs(R>R(RRZ((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_invalid_requests\s N( R R RRRR5R8R%RRRR(((s1/usr/local/lib/python2.7/test/test_httpservers.pyRs   s@#!%s print "Content-type: text/html" print print "Hello World" s#!%s import cgi print "Content-type: text/html" print form = cgi.FieldStorage() print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), form.getfirst("bacon")) Ris7This test can't be run reliably as root (issue #13308).tCGIHTTPServerTestCasecBseZdeefdYZdZdZdZdZdZ dZ dZ d Z d Z d Zd ZRS( RcBseZRS((R R (((s1/usr/local/lib/python2.7/test/test_httpservers.pyR~sc Cstj|tj|_tjj|jd|_tj |jt tdrtjj|jd|_ tj t j|j n t j|_ tjj|jd|_t|jd}|jt|j WdQXtj|jdtjj|jd|_t|jd}|jt|j WdQXtj|jdtjj|jd|_t|jd}|jt|j WdQXtj|jdtj|_tj|jdS( Nscgi-bintsymlinktpythonsnocgi.pyRisfile1.pysfile2.py(R+R5RRt parent_dirR/RRtcgi_dirtmkdirthasattrt pythonexeRtsyst executablet nocgi_pathRRt cgi_file1Rt file1_patht file2_patht cgi_file2RRR(RRtfile1tfile2((s1/usr/local/lib/python2.7/test/test_httpservers.pyR5s,  cCsztj|j|jtjkr8tj|jntj|jtj|jtj|j tj |j tj |j Wdt j|XdS(N(R/RRRRRtremoveRRRtrmdirRRR+R8(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyR8sc CsTidd6td6td6dd6dd6dd6dd6d d 6d d 6d d 6d d 6d d 6d d6dd6dd6dd6dd6dd6dd6dd6dd6dd6td6dd6dd 6td!6dd"6dd#6}x|jD]u\}}t|trt|tr|j|tj|qtj|}|j ||d$d%|||fqWdS(&Ns//Rs..s/.//..Rts//\s/\s/.//s/cgi-bin/file1.pyscgi-bin/file1.pys//atas/as./as/C:/s./C:/s/a/bs/a/b/s/a/b/.s /a/b/c/..s/a/b/ds /a/b/c/../ds/a/b/d/fs/a/b/c/../d/e/../fs/a/b/fs/a/b/c/../d/e/../../fs/a/b/c/../d/e/.././././..//fs../a/b/c/../d/e/.././././..//fs/a/fs/a/b/c/../d/e/../../../fs//fs/a/b/c/../d/e/../../../../fs/a/b/c/../d/e/../../../../../fs/a/b/c/../d/e/../../../../f/..s /a/b/c/../d/e/../../../../f/../.tmsgspath = %r Got: %r Wanted: %r( t IndexErrort iteritemst isinstancettypet issubclasst Exceptiont assertRaisest CGIHTTPServert_url_collapse_pathRT(Rt test_vectorsRtexpectedtactual((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_url_collapse_pathsJ  cCs>|jd}|jd|j|jd|jfdS(Ns/cgi-bin/file1.pys Hello World s text/htmlis Content-type(s Hello World s text/htmli(R>RTRRRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_headers_and_contents cCs&|jd}|j|jddS(Ns)///////////nocgi.py/../cgi-bin/nothere.shi(R>RTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_issue19435scCsctjidd6dd6dd6}idd6}|jd d ||}|j|jd dS( NitspamRteggsi@tbacons!application/x-www-form-urlencodeds Content-types/cgi-bin/file2.pytPOSTs1, python, 123456 (turllibt urlencodeR>RTR(RtparamsRCRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_posts$ cCs0|jd}|j|j|jddS(Ns/cgi-bin/invalidi(R>RRTRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_invaliduris cCsaidtjdd6}|jddd|}|jd |j|jd |jfdS( NsBasic %ss username:passt Authorizations/cgi-bin/file1.pyR9RCs Hello World s text/htmlis Content-type(s Hello World s text/htmli(tbase64t b64encodeR>RTRRRu(RRCRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_authorizations  cCs>|jd}|jd|j|jd|jfdS(Nscgi-bin/file1.pys Hello World s text/htmlis Content-type(s Hello World s text/htmli(R>RTRRRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_no_leading_slashs cCshd}|tjd<|jd}|jd|j|jd|jf|jtjd|dS( NsTest CGI ServertSERVER_SOFTWAREs/cgi-bin/file1.pys Hello World s text/htmlis Content-type(s Hello World s text/htmli(R/R0R>RTRRRu(Rt signatureRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_os_environ_is_not_altereds   "cCs>|jd}|jd|j|jd|jfdS(Ns/cgi-bin%2ffile1.pys Hello World s text/htmlis Content-type(s Hello World s text/htmli(R>RTRRRu(RRv((s1/usr/local/lib/python2.7/test/test_httpservers.pyt#test_urlquote_decoding_in_cgi_checks (R R RRRR5R8RRRRRRRRR(((s1/usr/local/lib/python2.7/test/test_httpservers.pyR{s *       t SimpleHTTPRequestHandlerTestCasecBs)eZdZdZdZdZRS(s Test url parsing cCs:tj|_tjj|jd|_t|_dS(Ntfilename(R/Rt translatedRRR RE(R((s1/usr/local/lib/python2.7/test/test_httpservers.pyR5 scCss|jjd}|j||j|jjd}|j||j|jjd}|j||jdS(Ns /filenames/filename?foo=bars/filename?a=b&spam=eggs#zot(REttranslate_pathRTR(RR((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_query_argumentss cCsN|jjd}|j||j|jjd}|j||jdS(Ns //filenames//filename?foo=bar(RERRTR(RR((s1/usr/local/lib/python2.7/test/test_httpservers.pyttest_start_with_double_slashs(R R RfR5RR(((s1/usr/local/lib/python2.7/test/test_httpservers.pyRs  cCs>z)tj}tjtttttWdtj |XdS(N( R/RRt run_unittestRDRRiRRR(tverboseR((s1/usr/local/lib/python2.7/test/test_httpservers.pyt test_mains   t__main__(()RfR/RRgRRRR;RtunittestRtBaseHTTPServerRRtSimpleHTTPServerRRRRRt import_moduleRRR RRtTestCaseR+RDRiRRRtskipIfRRRRR%RR (((s1/usr/local/lib/python2.7/test/test_httpservers.pyts@          A}U $