m &UIc@sdkZdklZdkZdkZdkZdkZdklZlZdei fdYZ dfdYZ dfdYZ d e fd YZd eifd YZd fdYZdfdYZdfdYZdZdei fdYZdZdei fdYZdei fdYZdei fdYZedZedjoedendS( N(s test_support(sRequestsOpenerDirectort TrivialTestscBstZdZdZRS(NcCs|ittidtiitii dd}|dd!djo|d}nti djod|i dd}nHti djo7dk }ti |}|i|id d }nd |}ti|}|i}|idS( Ns bogus urls\t/iit:tmactriscoss/.s./s file://%s(tselft assertRaisest ValueErrorturllib2turlopentostpathtabspatht__file__treplacetfnametnametstringtexpandt translatet maketranstfile_urltftreadtbuftclose(RRRRRR((t./data/zmath/lib/python2.4/test/test_urllib2.pyt test_trivials!   c Csddddgfdddgfdddd d d d d gfddddgfg}x-|D]%\}}|iti||q^WdS(Nsa,b,ctatbtcspath"o,l"og"i"cal, examplespath"o,l"og"i"caltexamplesa, b, "c", "d", "e,f", g, hs"c"s"d"s"e,f"tgthsa="b\"c", d="e\,f", g="h\\i"sa="b"c"sd="e,f"sg="h\i"(ttestsRtlistRt assertEqualsRtparse_http_list(RR#R"R((Rttest_parse_http_list,sW (t__name__t __module__RR&(((RRs t MockOpenercBs#tZgZddZdZRS(NcCs|||_|_dS(N(treqtdataR(RR*R+((Rtopen7scGs|||_|_dS(N(tprototargsR(RR-R.((Rterror9s(R'R(t addheaderstNoneR,R/(((RR)5s tMockFilecBs)tZddZddZdZRS(NcCsdS(N((Rtcount((RR=scCsdS(N((RR3((Rtreadline>scCsdS(N((R((RR?s(R'R(R1RR4R(((RR2<s  t MockHeaderscBstZdZRS(NcCs |iS(N(Rtvalues(RR((Rt getheadersBs(R'R(R7(((RR5Ast MockResponsecBs&tZddZdZdZRS(NcCsAtii||||||f\|_|_|_|_dS(N(tStringIOt__init__RR+tcodetmsgtheadersturl(RR;R<R=R+R>((RR:FscCs|iS(N(RR=(R((RtinfoIscCs|iS(N(RR>(R((RtgeturlKs(R'R(R1R:R?R@(((RR8Es  t MockCookieJarcBstZdZdZRS(NcCs ||_dS(N(trequestRtach_req(RRB((Rtadd_cookie_headerOscCs|||_|_dS(N(RBtresponseRtec_reqtec_r(RRERB((Rtextract_cookiesQs(R'R(RDRH(((RRANs t FakeMethodcBstZdZdZRS(NcCs||_||_||_dS(N(t meth_nameRthandletaction(RRJRLRK((RR:Us  cGs|i|i|i|S(N(RRKRJRLR.(RR.((Rt__call__Ys(R'R(R:RM(((RRITs t MockHandlercBs>tZdZdZdZdZdZdZRS(NcCs|i|dS(N(Rt_define_methodstmethods(RRP((RR:]scCsmxf|D]^}t|djo|\}}n|d}}t|||i}t |i ||qWdS(Ni( RPtspectlenRRLR1RIRRKtmethtsetattrt __class__(RRPRRLRSRQ((RRO_s cOs;|iii||||f|djodSn|djo|Sn|djot ddhd}|Sn|djot dSn|i doy||i d d }yt|}Wntj onXt ddhd}|iid |d ||dhSn!|d jotidndS(Ns return selfsreturn responseitOKtsreturn requests http://blah/R/t ithttpitraisetblah(Rtparenttcallstappendtfn_nameR.tkwdsRLR1R8trestRequestt startswithtrfindR;tintRR/RtURLError(RR_RLR.R`R;Ra((RRKes(    ' cCsdS(N((R((RR{scCs||_g|i_dS(N(R\RR](RR\((Rt add_parent|s cCs(t|dptSn|i|ijS(Nt handler_order(thasattrtothertTrueRRh(RRj((Rt__lt__s(R'R(R:RORKRRgRl(((RRN\s      cCs}g}d}xj|D]b}dtfdY}||}||_|i||d}|i ||i |qW|S(sCreate MockHandlers and add them to an OpenerDirector. meth_spec: list of lists of tuples and strings defining methods to define on handlers. eg: [["http_error", "ftp_open"], ["http_open"]] defines methods .http_error() and .ftp_open() on one handler, and .http_open() on another. These methods just record their arguments and return None. Using a tuple instead of a string causes the method to perform some action (see MockHandler.handle()), eg: [["http_error"], [("http_open", "return request")]] defines .http_error() on one handler (which simply returns None), and .http_open() on another handler, which returns a Request object. itMockHandlerSubclasscBstZRS(N(R'R((((RRmsiN( thandlersR3t meth_spectmethsRNRmR!RhRgtopenerR^t add_handler(RqRoR3RnR!RpRm((Rtadd_ordered_mock_handlerss     tOpenerDirectorTestscBs5tZdZdZdZdZdZRS(Nc Cst}dddgdgdgd gg}t||}td}|i|} |i | |d|ddf|ddfg}x[t ||i D]G\} } | \}} }}|i || f| |i ||fqWdS( Nt http_opentftp_openthttp_error_302s return selfshttp://example.com/ii(s http_opens return self(s http_opens return self(tOpenerDirectortoRoRsRnRbR*R,trRt assertEqualR]tziptexpectedtgotthandlerRR.R`( RR.R]RnRR*RyR`RoRzR}R~R((Rt test_handleds $   cCst}g}xqd gdfdgdfgD]Q\}}dtfdY}||}||_|i||i |q.W|i d}|i |idd|d|i |idd|ddS( NRus return selfiiRmcBstZRS(N(R'R((((RRmsshttp://example.com/i(s http_opens return self(RxRyRnRpRhRNRmR!R^RrR,RzRR{R](RRhRnR!RpRyRmRz((Rttest_handler_orders     cCs|t}dgdgg}t||}td}|it i |i ||i |i |dd|fhfgdS(NRuRZs return selfshttp://example.com/i(s http_opensraise(s http_opens return self(RxRyRoRsRnRbR*RRRRfR,R{R](RRnR*RyRo((Rt test_raises   c Cs t}dgddgdddgdgg} t|| }d fd Y}td }|i|} |d d|ff|d d||ddhffg}x]t ||i D]I\} } | \}}}|i||f| d |i|| d qWdS(NRus error 302thttp_error_400RZRwsreturn responsethttp_error_303t http_errortUnknowncBstZdZRS(NcCstS(N(Rk(RRj((Rt__eq__s(R'R(R(((RRsshttp://example.com/iii.RW(s http_opens error 302(Rsraise(shttp_error_302sreturn response(RxRyRoRsRnRRbR*R,RzR]R|R}R~Rt method_nameR.RR{( RR.R]RnRRR*RyRRoRzR}R~((Rttest_http_errors ' ; c Cst}d d gd d gg} t|| }td}|i|} |ddf|ddf|ddf|ddfg}xt |i D]\}\}}}} |djoN|i||f|||it|d|it|dtq|i||f|||it|d|it|dt|i|ddjpt|dtqWdS( Nt http_requestsreturn requestt http_responsesreturn responseshttp://example.com/iii(Rsreturn request(Rsreturn response(Rsreturn request(Rsreturn response(RxRyRoRsRnRbR*R,RzR]t enumeratetiRRR.R`RR{RRtassert_t isinstanceR1R8( RR.RR]RnRRR*RyRoRzR`((Rttest_processorss   : (R'R(RRRRR(((RRts     cCsJdk}|i|}tidjo|ido|d}n|S(Ntnts///i(turllibt pathname2urlR turlpathR RRc(R RR((Rtsanepathname2url"s   t HandlerTestscBsGtZdZdZdZdZdZdZdZRS(Nc sdfdYdtifdY}dk}dk} d}||} t} | _ xodd|i dd d gd d fd dddd d gddfdd|i dgddfgD] \} }}}}}} | it| }|i| i| ijo djn|i| i| i||i| i||i| i||i| ii||i| ii||i}|i|i!d| |it"|dt#|qWdS(NtMockFTPWrappercBstZdZdZRS(NcCs ||_dS(N(R+R(RR+((RR:.scCs2|||_|_ti|it|ifS(N(tfilenametfiletypeRR9R+RR(RRR((Rtretrfile/s(R'R(R:R(((RR-s tNullFTPHandlercstZdZdZRS(NcCs ||_dS(N(R+R(RR+((RR:4scsH|||_|_|||_|_||_|i|_|iS(N( tusertpasswdRthosttporttdirsRR+t ftpwrapper(RRRRRR(R(Rt connect_ftp5s  (R'R(R:R((R(RR3s srheum rhaponicums ftp://localhost/foo/bar/baz.htmlt localhosttItfootbarsbaz.htmls text/htmlsftp://localhost:80/foo/bar/iPtDRWsftp://localhost/baz.gif;type=atAsbaz.gifs Content-typesContent-length($RRt FTPHandlerRtftplibtsocketR+R!R)RyR\tFTP_PORTR1R>RRttype_RRtmimetypeRvRbRzRRRRR{t gethostbynameRRR?R=tgetReRR(RRRRRRRRR+RRR>R!RyR=RRz((RRttest_ftp,s$  a * cCsdk}dk}dk} ti} t}| _t i } t t i i| }d}xId|d|d| id|fd| i| i|fgD]} t| d} zz| i|Wd| iX| it| }z(|i}|i}|i}Wd|iXt i!| }|i#|i$}Wdt i&| X|i(|||i(|dd|i(|d d |i(|d |qWxwd |gD]h} zQt| d} z| i|Wd| iX|i)ti*| it| Wdt i&| XqWti} t}| _xd t+fdt,fdt,fgD]\} }t| }y| i|Wn*ti*t/fj o|i0| qbX|i0|i.|j|i(|i1dqbWdS(Ns hello, world sfile://localhost%ss file://%ss file://%s%sRtwbs Content-types text/plainsContent-lengtht13s Last-modifiedsfile://localhost:80%ssfile://ftp.example.com//foo.txts file://ftp.example.com///foo.txtsfile://ftp.example.com/foo.txttftp(2ttimetrfc822RRt FileHandlerR!R)RyR\t test_supporttTESTFNRR R R RttowriteRt gethostnameR>R,RtwriteRt file_openRbRzRR+R?R=R@tnewurltstattstatst formatdatetst_mtimetmodifiedtremoveRR{RRfRktFalseRR*tOSErrorRttype(RRRRRR*RRR+RRRR>R!RRyR=RzR((Rt test_fileXsh  G       "  c sKdfdYdfdY} ti}t}|_d}x@ddgD]2\} } t || hd d <}|id d | }|i||} | i| i| i| i| i| id jdf| i}|i|i|i| i||i|id|i|id|i|i | |i|id|i|i ddd g|i|i | qXWt!|_"|i#ti$|i||d!g|_%xd"D]}} t d| }t&d dhd} |i'|}| djo0|i)d|i*j|i)d|i*jn/|i|i*dd|i|i*dd|i|i*dd|i|i*d d |idd|idd |idd|id d|i'|}|i|i*dd|i|i*dd |i|i*dd|i|i*d dqWdS(#NtMockHTTPResponsecBstZdZdZRS(NcCs(||_||_||_||_dS(N(tfpRR<tstatustreason(RRR<RR((RR:s   cCsdS(NRW((R((RRs(R'R(R:R(((RRs t MockHTTPClasscs>tZdZdZdZdhdZdZRS(NcCsg|_d|_t|_dS(N(Rt req_headersR1R+Rtraise_on_endheaders(R((RR:s  cCs ||_|S(N(RR(RR((RRMs cCs ||_dS(N(tlevelR(RR((Rtset_debuglevelscCso||_||_|i|i7_|ii|o ||_ n|i odk }|i ndS(N( tmethodRR>tselectorRR=titemstsorttbodyR+RRR/(RRR>RR=R((RRBs      csthddS(NiRV(RR2(R(R(Rt getresponses(R'R(R:RMRR1RBR((R(RRs     shttp://example.com/tGETtPOSTR[tFooRtSpamteggsiRVs example.comiRt ConnectionRRWsContent-lengths Content-typet0s!application/x-www-form-urlencodedtHostRtbaz(RN(Rsblah(Rsclose(Rsbar(RR(RR(RWN(+RRRtAbstractHTTPHandlerR!R)RyR\R>R1RR+RbR*tadd_unredirected_headerRYtdo_openRzRR4R?R@R;R<thdrsRthas_keyRR{RRRRRkRRRfR0R8t do_request_tnewreqRtunredirected_hdrs( RRRYRR>R!R*RyRRzR+RR((RRt test_httpsb        cCsti}t}|_d}t|}t ddhd|}|i ||}|i||j|it|d t ddhd|}|i|i ||dj|i|id|i|i||ddhfdS( Nshttp://example.com/iRVRWR-itCreatedRY(RtHTTPErrorProcessorR!R)RyR\R>RbR*R8RzRtnewrRRRiR1R{R-R.(RR>R!R*RyRzR((Rt test_errorss  cCst}ti|}t}|_td}t ddhd}|i |}|i|i|jo |jn|i|id|i|i |i||}|i|i|j|i|i|jo |jndS(Nshttp://example.com/iRVRWs example.com(RAtcjRtHTTPCookieProcessorR!R)RyR\RbR*R8RzRRRRRCR$tget_origin_req_hosttis_unverifiableRRRFRG(RRRR!R*RyRzR((Rt test_cookiess  'c Csd}d} ti}t}|_xudD]m}xddD]\}t |d|} t ||}|id d |id d y,| |t|d thd| <Wn6tij o'|i|djo |dj nX|i|ii| y|i|iidWn*tj o|i|ii nX|i|iid d |id |iij|id |iijq<Wq/Wt |}| d}t |dd}d}y&x|||d|d}qWWn,tij o|i|tiinXt |dd}d}y*x#|||d||d}qCWWn,tij o|i|tii nXdS(Nshttp://example.com/a.htmlshttp://example.com/b.htmli-i.i/i3s blah blah s http_error_%stNonsensesviking=withholdRtspamtBlahtlocationRc Cs/|i|tddthd|<dS(Ni.RR(R!RwR*R2R5R>(R!R*R>((RtredirectEstorigin_req_hosts example.comiishttp://example.com/shttp://example.com/%d(i-i.i/i3(Ns blah blah (!tfrom_urltto_urlRtHTTPRedirectHandlerR!R)RyR\R;R1R+tgetattrRRbR*t add_headerRR2R5t HTTPErrorRRR{t get_full_urlt get_methodtAttributeErrorthas_dataR=RRR3t max_repeatstmax_redirections( RRR;R3R!R*RyRR+RR((Rt test_redirect&s^ %!   c CsdtifdY} dkl}dkl}l} l} l}dkl }|}||dd| }||}|||}|id|i|iid dS( NtMockHTTPHandlercBstZdZdZRS(NcCs d|_dS(Ni(Rt_count(R((RR:cscCsdk}dkl}|idjoK|id|_|i|d}|iid|t dd|Sn;||_|i|d}t d d |d |i SdS( N(sStringIOiis%Location: http://www.cracker.com/ RYi.tFounds iRVRW( t mimetoolsR9RRtMessageR<R\R/R*R2R8R(RR*R R9R<((RRuds  & (R'R(R:Ru(((RRbs (s CookieJar(s build_openers HTTPHandlers HTTPErrorsHTTPCookieProcessor(sinteract_netscapeshttp://www.example.com/s spam=eggstCookie(Rt HTTPHandlerRt cookielibt CookieJart build_openerRRttest_cookielibtinteract_netscapeRthhtcpRyR,RRR*t has_header( RRRRRRyRRRRRR ((Rttest_cookie_redirectas      ( R'R(RRRRRRR(((RR*s , S [   ;t MiscTestscBstZdZdZRS(NcCs8dtifdY}dtifdY}dtifdY}ti}|||}|i |||i |||||}|i |||i ||||}|i |||}|i |ti|ti}|i |ti|ti}|i |tidS(Nt MyHTTPHandlercBstZRS(N(R'R((((RRst FooHandlercBstZdZRS(NcCsdS(N((R((Rtfoo_opens(R'R(R(((RRst BarHandlercBstZdZRS(NcCsdS(N((R((Rtbar_opens(R'R(R(((RRs( RR Rt BaseHandlerRRRRyRtopener_has_handler(RRRRyRR((Rttest_build_openers$   cCs:x3|iD]}|i|joPq q W|itdS(N(RqRnR!RUt handler_classRRR(RRqRR!((RRs   (R'R(RR(((RRs t NetworkTestscBsPtZdZdZdZdZdZdZdZdZ RS( NcCsdS(N((R((RtsetUpscCsStiddhdd<}ti|}|i}|it |ddS(Nshttp://www.python.orgR=tRanges bytes=20-39i( RRbR*R tresultRR+RR{RR(RR*R#R+((Rt test_ranges   cCs)dddg}|i||idS(Ns-ftp://www.python.org/pub/python/misc/sousa.aus!ftp://www.python.org/pub/tmp/blats[ftp://gatekeeper.research.compaq.com/pub/DEC/SRC/research-reports/00README-Legal-Rules-Regs(turlsRt _test_urlst_extra_handlers(RR%((RRscCs&ddg}|i||idS(Ns3gopher://gopher.lib.ncsu.edu/11/library/stacks/Alexs"gopher://gopher.vt.edu:10010/10/33(R%RR&R'(RR%((Rt test_gophers cCsti}t|d}ze|id|idttii |ddt t i ffg}|i||iWdti|XdS(Ntws hi there sfile:sfile://nonsensename/etc/passwd(RRR,RRRRR R R R1RRR/R%RR&R'R(RRR%R((RRs   4cCs,ddddg}|i||idS(Nshttp://www.espn.com/s)http://www.python.org/Spanish/Inquistion/s%http://www.python.org/cgi-bin/faqw.pys:query=pythonistas&querytype=simple&casefold=yes&req=searchshttp://www.python.org/(s%http://www.python.org/cgi-bin/faqw.pys:query=pythonistas&querytype=simple&casefold=yes&req=searchN(R1R%RR&R'(RR%((RRsc Cs5dk}dk} dk}|idi} titi|x|D]}t |t o|\}}}n d}}| |yti||}WnLt|itfj o4} | | |o|it | |qn,X|i} |i| dt| | d| idqJWdS(Nt test_urllib2s read %d bytess******** next url coming up...f0.10000000000000001(RRtloggingt getLoggertdebugRtinstall_openerRRnR%R>RttupleR*t expected_errR1R RtIOErrorR/RterrRRRRRRRtsleep( RR%RnR0R*R+RRR>R2RR-R((RR&s,         cCs@g}|ititi}|id|i||S(Ni(RnR^Rt GopherHandlertCacheFTPHandlertcfht setTimeout(RR6Rn((RR's    ( R'R(R!R$RR(RRR&R'(((RR s    & cCsDttttf}tido|tf7}nti|dS(Ntnetwork( RRtRRR"Rtis_resource_enabledR t run_unittest(tverboseR"((Rt test_main3st__main__R;(tunittestttestRR RR9RRbRxtTestCaseRR)R2tdictR5R8RARIRNRsRtRRRR R1R<R'Rk(RR R>R5RtR8RIRARR<RNRbRR2RxRRsRRR9RR)R ((Rt?s.     " ) ~ X(