{G_Tc@s?ddlZddlZddlZddlZddlZddlmZejddeZ ejdZ dej fdYZ de j fd YZd dd YZd ejfd YZdejfdYZdefdYZdZdefdYZdZedkr;endS(iN(t test_supportt mimetoolst deprecatedt threadingtLoopbackHttpServercBs eZdZdZdZRS(s^HTTP server w/ a few modifications that make it useful for loopback testing purposes. cCs*tjj||||jjddS(Ng?(tBaseHTTPServert HTTPServert__init__tsockett settimeout(tselftserver_addresstRequestHandlerClass((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRs cCs,|jj\}}|jd||fS(s"BaseHTTPServer method, overridden.g$@(RtacceptR (R trequesttclient_address((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt get_requests (t__name__t __module__t__doc__RR(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRs tLoopbackHttpServerThreadcBs)eZdZdZdZdZRS(s2Stoppable thread that runs a loopback http server.cCsVtjj|t|_tj|_d|_td||_ |j j |_ dS(NsHTTP/1.0s 127.0.0.1i(s 127.0.0.1i( RtThreadRtFalset_stoptEventtreadytprotocol_versionRthttpdt server_porttport(R trequest_handler((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyR+s   cCst|_|jdS(s.Stops the webserver if it's currently running.N(tTrueRtjoin(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytstop6s cCs.|jjx|js)|jjqWdS(N(RtsetRRthandle_request(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytrun>s  (RRRRR!R$(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyR(s tDigestAuthHandlercBs_eZdZdZdZdZdZdZdZdZ dZ d Z RS( s-Handler for performing digest authentication.cCs1d|_g|_i|_d|_d|_dS(Nis Test Realmtauth(t _request_numt_noncest_userst _realm_namet_qop(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRHs     cCs ||_dS(N(R+(R tqop((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytset_qopOscCs ||_dS(N(R)(R tusers((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt set_usersRscCs ||_dS(N(R*(R trealm((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt set_realmVscCsA|jd7_tjt|jj}|jj||S(Ni(R'thashlibtmd5tstrt hexdigestR(tappend(R tnonce((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt_generate_nonceYscCs|jd}||d}|jd}i}xu|D]m}|jd\}}|j}|ddkr|ddkr|dd!}n |j}||||j |j gD]*}|j |||j |rt}qqW|s|j|StSdS(sPerforms digest authentication on the given HTTP request handler. Returns True if authentication was successful, False otherwise. If no users have been set, then digest auth is effectively disabled and this method will always return True. isProxy-AuthorizationtusernameR7N(tlenR)RtheadersRYRGtgetR(tremoveRtpatht short_pathRStcommand(R RRCRHtauth_validatedR_((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyR#s,        ( RRRRR-R/R1R8RGRSRYR#(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyR%Es        tFakeProxyHandlercBs)eZdZdZdZdZRS(sThis is a 'fake proxy' that makes it look like the entire internet has gone down due to a sudden zombie invasion. It main utility is in providing us with authentication support for testing. cOs#||_tjj|||dS(N(tdigest_auth_handlerRtBaseHTTPRequestHandlerR(R Rdtargstkwargs((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRs cGsdS(N((R tformatRf((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt log_messagescCstj|jd\}}}}}}||_|jj|r|jdd|jdd|j|jj d|j|jj dndS(NthttpitOKs Content-Types text/htmlsYou've reached %s!
sFOur apologies, but our server is down due to a sudden zombie invasion.( turlparseR_R`RdR#RTRURVRWRX(R tscmtnetlocR_tparamstquerytfragment((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytdo_GETs!  (RRRRRiRr(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRcs  t BaseTestCasecBseZdZdZRS(cCstj|_dS(N(Rtthreading_setupt_threads(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytsetUpscCstj|jdS(N(Rtthreading_cleanupRu(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttearDowns(RRRvRx(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRss tProxyAuthTestscBsVeZdZdZdZdZdZdZdZdZ dZ d Z RS( shttp://localhostttesterttest123t TestRealmcsttjt_jjijj6jjj fd}t |_ j j j j jdj j}tji|d6}tj_tj|j_dS(Ncstj||S(N(RcRd(RfRg(R (s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytcreate_fake_proxy_handlersshttp://127.0.0.1:%dRj(tsuperRyRvR%RdR/tPASSWDtUSERR1tREALMRtservertstartRtwaitRturllib2t ProxyHandlertProxyDigestAuthHandlertproxy_digest_handlert build_openertopener(R R}t proxy_urlthandler((R s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRvs  cCs$|jjtt|jdS(N(RR!R~RyRx(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRxs cCs\|jj|j|j|j|jd|jjd|jt j |j j |jdS(NtbadR&( Rt add_passwordRtURLRRRdR-t assertRaisesRt HTTPErrorRtopen(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt-test_proxy_with_bad_password_raises_httperrors   cCs3|jjd|jtj|jj|jdS(NR&(RdR-RRRRRR(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt,test_proxy_with_no_password_raises_httperror s  cCsk|jj|j|j|j|j|jjd|jj |j}x|j r\qMW|j dS(NR&( RRRRRRRdR-RRtreadtclose(R tresult((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_proxy_qop_auth_worksscCs|jj|j|j|j|j|jjdy|jj |j}Wnt j k rjd}nX|rx|j rqtW|jndS(Nsauth-int(RRRRRRRdR-RRRtURLErrortNoneRR(R R((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt0test_proxy_qop_auth_int_works_or_throws_urlerrors ( RRRRRRRvRxRRRR(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRys     cs&dtjffdYS(NtFakeHTTPRequestHandlercsMeZdZgZgZdZdZdZfdZdZ RS(s TestHTTP/iPcSs)|j}|r%|jj|ndS(N(t send_headRWRX(R tbody((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRr3s cSsC|jd}|jjt|}|j|jj|dS(NsContent-Length(R\trfileRtintRrtrequestsR6(R tcontent_lengtht post_data((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytdo_POST8s  cs|j_|jj|jjd\}}}|j|x+|D]#\}}|j|||jqKW|r|jdd|j |S|j dS(Nis Content-types text/plain( R\theaders_receivedRR6R_tpopRTRURRV(R t response_codeR\RtheaderRF(Rt responses(s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyR>s   cWsdS(N((R Rf((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRiMs( RRtserver_versionRRRRrRRRi((RR(s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyR,s  (RRe(R((RRs6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytGetRequestHandler*s"%t TestUrlopencBseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZRS(sGTests urllib2.urlopen using the network. These tests are not exhaustive. Assuming that testing using files does a good job overall of some of the basic interface features. There are no tests exercising the optional 'data' and 'proxies' arguments. No tests for transparent redirection have been written. cCsBtji}tj|}tj|tt|jdS(N(RRRtinstall_openerR~RRv(R t proxy_handlerR((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRv]s cCsQt|}t||_|jj|jjj|jj}||_|S(N(RRRRRRR(R RRR((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt start_servercs    cCsd}dd gdfdg|fg}|j|}zYtjd|j}|j}|j|j|||j|jdd gWd|jj XdS( NsWe got here...i.tLocations"http://localhost:%s/somewhere_elsetishttp://localhost:%s/t/s/somewhere_else(Rs"http://localhost:%s/somewhere_else( RRturlopenRRRt assertEqualRRR!(R texpected_responseRRtftdata((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_redirectionns  cCsd}|jdg|fg}z~ytjd|jWntjk rT}nX|jd|j}|j|j|||j|j dgWd|j j XdS(NsBad bad bad...ishttp://localhost:%s/weebles404 should raise URLErrors/weeble( RRRRRtfailRRRRRR!(R RRRR((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_404s   cCsd}|jdg|fg}zVtjd|j}|j}|j|j|||j|jdgWd|jj XdS(Ns pycon 2008...ishttp://localhost:%s/bizarres/bizarre( RRRRRRRRRR!(R RRRR((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_200s  cCsd}|jdg|fg}z\tjd|jd}|j}|j|j|||j|jddgWd|jj XdS(Ns pycon 2008...ishttp://localhost:%s/bizarresget=with_feelings/bizarre( RRRRRRRRRR!(R RRRR((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_200_with_parameterss  cCs{|jdgdfg}zKtjd|jdidd6}tj||j|jddWd|jjXdS(Nis we don't careshttp://localhost:%s/R\s bytes=20-39tRange( RRtRequestRRRRRR!(R Rtreq((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_sending_headerss cCs|jdgdfg}zptjd|j}x+d D]#}|jt||d|q;Wz|j|jd Wd|jXWd|jj XdS( Nis we don't careshttp://localhost:%sRRtinfotgeturls3object returned from urlopen lacks the %s attributescalling 'read' failed(sreadsclosesinfosgeturl( RRRRt assertTruethasattrRRRR!(R Rtopen_urltattr((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt test_basics  cCs|jdgdfg}zRtjd|j}|j}|j|tjd|j|j dWd|j j XdS(Nis we don't careshttp://localhost:%ssAobject returned by 'info' is not an instance of mimetools.Messagetplain( RRRRRtassertIsInstanceRtMessageRt getsubtypeRR!(R RRtinfo_obj((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt test_infos cCsm|jdgdfg}z=tjd|j}|j}|j|d|jWd|jjXdS(Nis we don't careshttp://localhost:%s(RRRRRRRR!(R RRturl((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt test_geturls  cCs'tjd|jttjddS(Ntnetworkshttp://sadflkjsasf.i.nvali.d./(RtrequiresRtIOErrorRR(R ((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_bad_addresss  cCsqd}|jdg|fg}z;tjd|j}x|D]}|j||qAWWd|jjXdS(Ns pycon 2008...ishttp://localhost:%s(RRRRRRR!(R RRRtline((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyttest_iterations c Csddddddg}dj|}|jdg|fg}zktjd |j}xNt|D]@\}}|j|||d |t||t|fqjWWd|jj X|j|d t|dS( NsWe sgot shere s verylong i s Rishttp://localhost:%ssQFetched line number %s doesn't match expected: Expected length was %s, got %si( R RRRRt enumerateRR[RR!(R tlinesRRRtindexR((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pytztest_line_iteration s)(RRRRvRRRRRRRRRRRR(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyRTs        cCstjttdS(N(Rt run_unittestRyR(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyt test_mainst__main__((RlRRtunittestR2ttestRt import_moduleRRRRRRRR%ReRctTestCaseRsRyRRRR(((s6/usr/local/lib/python2.7/test/test_urllib2_localnet.pyts$     z C *