kWc@smddlZddlmZddlZddlZddlZddlZddlmZmZyddl Z Wne k re Z nXdej fdYZ dZdZdZd Zd fd YZd fd YZdefdYZdejfdYZdfdYZdfdYZdfdYZdfdYZdfdYZdZdZdejfdYZd ej fd!YZ!d"fd#YZ"d$ej fd%YZ#d&Z$d'ej fd(YZ%d)ej fd*YZ&d+ej fd,YZ'e d-Z(e)d.krie(d/e*ndS(0iN(t test_support(tRequesttOpenerDirectort TrivialTestscBs5eZdZdZejeddZRS(cCs|jttjdtjjtjjdd}tj dkrddl }tj |}|j |j dd}ntj dkrd |}n d |}tj|}|j}|jdS( Ns bogus urls\t/triscosis/.s./tnts file:///%ss file://%s(t assertRaisest ValueErrorturllib2turlopentostpathtabspatht__file__treplacetnametstringtexpandt translatet maketranstreadtclose(tselftfnameRtfile_urltftbuf((s'/usr/lib/python2.7/test/test_urllib2.pyt test_trivials!    c Csddddgfdddgfdddd d d d d gfddddgfg}x-|D]%\}}|jtj||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"(t assertEqualR tparse_http_list(RttestsRtlist((s'/usr/lib/python2.7/test/test_urllib2.pyttest_parse_http_list/s sssl module requiredc Cs?tj}|jttjdddd|WdQXdS(Nshttps://localhosttcafiles/nonexistent/pathtcontext(tssltcreate_default_contextRRR R (RR)((s'/usr/lib/python2.7/test/test_urllib2.pyttest_cafile_and_context7s (t__name__t __module__RR'tunittestt skipUnlessR*R,(((s'/usr/lib/python2.7/test/test_urllib2.pyRs  cCsdS(s The Request.headers dictionary is not a documented interface. It should stay that way, because the complete set of headers are only accessible through the .get_header(), .has_header(), .header_items() interface. However, .headers pre-dates those methods, and so real code will be using the dictionary. The introduction in 2.4 of those methods was a mistake for the same reason: code that previously saw all (urllib2 user)-provided headers in .headers now sees only a subset (and the function interface is ugly and incomplete). A better change would have been to replace .headers dict with a dict subclass (or UserDict.DictMixin instance?) that preserved the .headers interface and also provided access to the "unredirected" headers. It's probably too late to fix that, though. Check .capitalize() case normalization: >>> url = "http://example.com" >>> Request(url, headers={"Spam-eggs": "blah"}).headers["Spam-eggs"] 'blah' >>> Request(url, headers={"spam-EggS": "blah"}).headers["Spam-eggs"] 'blah' Currently, Request(url, "Spam-eggs").headers["Spam-Eggs"] raises KeyError, but that could be changed in future. N((((s'/usr/lib/python2.7/test/test_urllib2.pyttest_request_headers_dict@scCsdS(s Note the case normalization of header names here, to .capitalize()-case. This should be preserved for backwards-compatibility. (In the HTTP case, normalization to .title()-case is done by urllib2 before sending headers to httplib). >>> url = "http://example.com" >>> r = Request(url, headers={"Spam-eggs": "blah"}) >>> r.has_header("Spam-eggs") True >>> r.header_items() [('Spam-eggs', 'blah')] >>> r.add_header("Foo-Bar", "baz") >>> items = r.header_items() >>> items.sort() >>> items [('Foo-bar', 'baz'), ('Spam-eggs', 'blah')] Note that e.g. r.has_header("spam-EggS") is currently False, and r.get_header("spam-EggS") returns None, but that could be changed in future. >>> r.has_header("Not-there") False >>> print r.get_header("Not-there") None >>> r.get_header("Not-there", "default") 'default' N((((s'/usr/lib/python2.7/test/test_urllib2.pyttest_request_headers_methods^scCsdS(s >>> mgr = urllib2.HTTPPasswordMgr() >>> add = mgr.add_password >>> add("Some Realm", "http://example.com/", "joe", "password") >>> add("Some Realm", "http://example.com/ni", "ni", "ni") >>> add("c", "http://example.com/foo", "foo", "ni") >>> add("c", "http://example.com/bar", "bar", "nini") >>> add("b", "http://example.com/", "first", "blah") >>> add("b", "http://example.com/", "second", "spam") >>> add("a", "http://example.com", "1", "a") >>> add("Some Realm", "http://c.example.com:3128", "3", "c") >>> add("Some Realm", "d.example.com", "4", "d") >>> add("Some Realm", "e.example.com:3128", "5", "e") >>> mgr.find_user_password("Some Realm", "example.com") ('joe', 'password') >>> mgr.find_user_password("Some Realm", "http://example.com") ('joe', 'password') >>> mgr.find_user_password("Some Realm", "http://example.com/") ('joe', 'password') >>> mgr.find_user_password("Some Realm", "http://example.com/spam") ('joe', 'password') >>> mgr.find_user_password("Some Realm", "http://example.com/spam/spam") ('joe', 'password') >>> mgr.find_user_password("c", "http://example.com/foo") ('foo', 'ni') >>> mgr.find_user_password("c", "http://example.com/bar") ('bar', 'nini') Actually, this is really undefined ATM ## Currently, we use the highest-level path where more than one match: ## >>> mgr.find_user_password("Some Realm", "http://example.com/ni") ## ('joe', 'password') Use latest add_password() in case of conflict: >>> mgr.find_user_password("b", "http://example.com/") ('second', 'spam') No special relationship between a.example.com and example.com: >>> mgr.find_user_password("a", "http://example.com/") ('1', 'a') >>> mgr.find_user_password("a", "http://a.example.com/") (None, None) Ports: >>> mgr.find_user_password("Some Realm", "c.example.com") (None, None) >>> mgr.find_user_password("Some Realm", "c.example.com:3128") ('3', 'c') >>> mgr.find_user_password("Some Realm", "http://c.example.com:3128") ('3', 'c') >>> mgr.find_user_password("Some Realm", "d.example.com") ('4', 'd') >>> mgr.find_user_password("Some Realm", "e.example.com:3128") ('5', 'e') N((R((s'/usr/lib/python2.7/test/test_urllib2.pyttest_password_managers>cCsdS(sV >>> mgr = urllib2.HTTPPasswordMgr() >>> add = mgr.add_password The point to note here is that we can't guess the default port if there's no scheme. This applies to both add_password and find_user_password. >>> add("f", "http://g.example.com:80", "10", "j") >>> add("g", "http://h.example.com", "11", "k") >>> add("h", "i.example.com:80", "12", "l") >>> add("i", "j.example.com", "13", "m") >>> mgr.find_user_password("f", "g.example.com:100") (None, None) >>> mgr.find_user_password("f", "g.example.com:80") ('10', 'j') >>> mgr.find_user_password("f", "g.example.com") (None, None) >>> mgr.find_user_password("f", "http://g.example.com:100") (None, None) >>> mgr.find_user_password("f", "http://g.example.com:80") ('10', 'j') >>> mgr.find_user_password("f", "http://g.example.com") ('10', 'j') >>> mgr.find_user_password("g", "h.example.com") ('11', 'k') >>> mgr.find_user_password("g", "h.example.com:80") ('11', 'k') >>> mgr.find_user_password("g", "http://h.example.com:80") ('11', 'k') >>> mgr.find_user_password("h", "i.example.com") (None, None) >>> mgr.find_user_password("h", "i.example.com:80") ('12', 'l') >>> mgr.find_user_password("h", "http://i.example.com:80") ('12', 'l') >>> mgr.find_user_password("i", "j.example.com") ('13', 'm') >>> mgr.find_user_password("i", "j.example.com:80") (None, None) >>> mgr.find_user_password("i", "http://j.example.com") ('13', 'm') >>> mgr.find_user_password("i", "http://j.example.com:80") (None, None) N((R((s'/usr/lib/python2.7/test/test_urllib2.pyt"test_password_manager_default_portst MockOpenercBs)eZgZdejdZdZRS(cCs!||||_|_|_dS(N(treqtdatattimeout(RR6R7R8((s'/usr/lib/python2.7/test/test_urllib2.pytopenscGs|||_|_dS(N(tprototargs(RR:R;((s'/usr/lib/python2.7/test/test_urllib2.pyterrorsN(R-R.t addheaderstNonetsockett_GLOBAL_DEFAULT_TIMEOUTR9R<(((s'/usr/lib/python2.7/test/test_urllib2.pyR5stMockFilecBs)eZddZddZdZRS(cCsdS(N((Rtcount((s'/usr/lib/python2.7/test/test_urllib2.pyRscCsdS(N((RRB((s'/usr/lib/python2.7/test/test_urllib2.pytreadlinescCsdS(N((R((s'/usr/lib/python2.7/test/test_urllib2.pyRsN(R-R.R>RRCR(((s'/usr/lib/python2.7/test/test_urllib2.pyRAs  t MockHeaderscBseZdZRS(cCs |jS(N(tvalues(RR((s'/usr/lib/python2.7/test/test_urllib2.pyt getheaderss(R-R.RF(((s'/usr/lib/python2.7/test/test_urllib2.pyRDst MockResponsecBs&eZddZdZdZRS(cCsAtjj||||||f\|_|_|_|_dS(N(tStringIOt__init__tcodetmsgtheadersturl(RRJRKRLR7RM((s'/usr/lib/python2.7/test/test_urllib2.pyRIscCs|jS(N(RL(R((s'/usr/lib/python2.7/test/test_urllib2.pytinfoscCs|jS(N(RM(R((s'/usr/lib/python2.7/test/test_urllib2.pytgeturlsN(R-R.R>RIRNRO(((s'/usr/lib/python2.7/test/test_urllib2.pyRGs  t MockCookieJarcBseZdZdZRS(cCs ||_dS(N(tach_req(Rtrequest((s'/usr/lib/python2.7/test/test_urllib2.pytadd_cookie_header scCs|||_|_dS(N(tec_reqtec_r(RtresponseRR((s'/usr/lib/python2.7/test/test_urllib2.pytextract_cookies s(R-R.RSRW(((s'/usr/lib/python2.7/test/test_urllib2.pyRPs t FakeMethodcBseZdZdZRS(cCs||_||_||_dS(N(t meth_namethandletaction(RRYR[RZ((s'/usr/lib/python2.7/test/test_urllib2.pyRIs  cGs|j|j|j|S(N(RZRYR[(RR;((s'/usr/lib/python2.7/test/test_urllib2.pyt__call__s(R-R.RIR\(((s'/usr/lib/python2.7/test/test_urllib2.pyRXs tMockHTTPResponsecBseZdZdZRS(cCs(||_||_||_||_dS(N(tfpRKtstatustreason(RR^RKR_R`((s'/usr/lib/python2.7/test/test_urllib2.pyRIs   cCsdS(Nt((R((s'/usr/lib/python2.7/test/test_urllib2.pyRs(R-R.RIR(((s'/usr/lib/python2.7/test/test_urllib2.pyR]s t MockHTTPClasscBsYeZdZejdZdZdddZdddZ dZ dZ RS(cCs(g|_d|_t|_i|_dS(N(t req_headersR>R7tFalsetraise_on_endheaderst_tunnel_headers(R((s'/usr/lib/python2.7/test/test_urllib2.pyRI s   cCs||_||_|S(N(thostR8(RRgR8((s'/usr/lib/python2.7/test/test_urllib2.pyR\&s  cCs ||_dS(N(tlevel(RRh((s'/usr/lib/python2.7/test/test_urllib2.pytset_debuglevel+scCs5||_||_|r$||_n |jjdS(N(t _tunnel_hostt _tunnel_portRftclear(RRgtportRL((s'/usr/lib/python2.7/test/test_urllib2.pyt set_tunnel.s    cCs}||_||_|dk r6|j|j7_n|jj|rU||_n|jryddl}|j ndS(Ni( tmethodtselectorR>RctitemstsortR7ReR?R<(RRoRMtbodyRLR?((s'/usr/lib/python2.7/test/test_urllib2.pyRR6s       cCsttiddS(NitOK(R]RA(R((s'/usr/lib/python2.7/test/test_urllib2.pyt getresponseBscCsdS(N((R((s'/usr/lib/python2.7/test/test_urllib2.pyREsN( R-R.RIR?R@R\RiR>RnRRRuR(((s'/usr/lib/python2.7/test/test_urllib2.pyRbs   t MockHandlercBsDeZdZdZdZdZdZdZdZRS(icCs|j|dS(N(t_define_methods(Rtmethods((s'/usr/lib/python2.7/test/test_urllib2.pyRILscCskxd|D]\}t|dkr.|\}}n |d}}t|||j}t|j||qWdS(Ni(tlenR>RXRZtsetattrt __class__(RRxtspecRR[tmeth((s'/usr/lib/python2.7/test/test_urllib2.pyRwNs   cOs*|jjj||||f|dkr/dS|dkr?|S|dkrdtddid}|S|dkrztdS|jdr||jd d }yt|}Wnt k rnXtddid}|jj d |d ||diS|d krt j dnt s&tdS(Ns return selfsreturn responseiRtRasreturn requests http://blah/R<t ithttpitraisetblah(tparenttcallstappendR>RGRt startswithtrfindtintRR<R tURLErrorRdtAssertionError(Rtfn_nameR[R;tkwdstresRJ((s'/usr/lib/python2.7/test/test_urllib2.pyRZTs*      # cCsdS(N((R((s'/usr/lib/python2.7/test/test_urllib2.pyRjscCs||_g|j_dS(N(RR(RR((s'/usr/lib/python2.7/test/test_urllib2.pyt add_parentks cCs#t|dstS|j|jkS(Nt handler_order(thasattrtTrueR(Rtother((s'/usr/lib/python2.7/test/test_urllib2.pyt__lt__ns( R-R.RRIRwRZRRR(((s'/usr/lib/python2.7/test/test_urllib2.pyRvHs     cCsg}d}xp|D]h}dtfdY}||}|j|7_|j||d}|j||j|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. itMockHandlerSubclasscBseZRS((R-R.(((s'/usr/lib/python2.7/test/test_urllib2.pyRsi(RvRRRt add_handler(topenert meth_specthandlersRBtmethsRR"((s'/usr/lib/python2.7/test/test_urllib2.pytadd_ordered_mock_handlersts     cGs+t}x|D]}|j|qW|S(N(RR(thandler_instancesRR"((s'/usr/lib/python2.7/test/test_urllib2.pytbuild_test_openers  tMockHTTPHandlercBs#eZdZdZdZRS(cCs ||_||_|jdS(N(RJRLtreset(RRJRL((s'/usr/lib/python2.7/test/test_urllib2.pyRIs  cCsd|_g|_dS(Ni(t_counttrequests(R((s'/usr/lib/python2.7/test/test_urllib2.pyRs cCsddl}ddl}ddl}ddlm}|jj|j||jdkr|jd|_|j|j }|j ||j }|j j d|t|j ||S||_|j |d}tdd|d |jSdS( Ni(RHiiRs iRtRa(t mimetoolsthttplibtcopyRHRRtdeepcopyRt responsesRJtMessageRLRR<RAR6RGt get_full_url(RR6RRRRHRRK((s'/usr/lib/python2.7/test/test_urllib2.pyt http_opens$  (R-R.RIRR(((s'/usr/lib/python2.7/test/test_urllib2.pyRs  tMockHTTPSHandlercBseZdZdZRS(cCs tjj|t|_dS(N(R tAbstractHTTPHandlerRIRbthttpconn(R((s'/usr/lib/python2.7/test/test_urllib2.pyRIscCs|j|j|S(N(tdo_openR(RR6((s'/usr/lib/python2.7/test/test_urllib2.pyt https_opens(R-R.RIR(((s'/usr/lib/python2.7/test/test_urllib2.pyRs tMockPasswordManagercBseZdZdZRS(cCs(||_||_||_||_dS(N(trealmRMtusertpassword(RRturiRR((s'/usr/lib/python2.7/test/test_urllib2.pyt add_passwords   cCs"||_||_|j|jfS(N(t target_realmt target_urlRR(RRtauthuri((s'/usr/lib/python2.7/test/test_urllib2.pytfind_user_passwords  (R-R.RR(((s'/usr/lib/python2.7/test/test_urllib2.pyRs tOpenerDirectorTestscBsGeZdZdZdZdZdZdZdZRS(cCs6dtfdY}|jttj|dS(Nt NonHandlercBseZRS((R-R.(((s'/usr/lib/python2.7/test/test_urllib2.pyRs(tobjectRt TypeErrorRR(RR((s'/usr/lib/python2.7/test/test_urllib2.pyttest_add_non_handlers cCsddlm}t}d d gd gg}t||}|jtjx(dD] }|j||j|d qWWdS(Ni(RRs return selft proxy_opentredirect_requesttdotproxytredirects://example.com/(sdo_opens return self(Rs return self(Rs return self(Rsproxysredirect(R RRRRtUnknownHandlerRR9(RRtoRRtscheme((s'/usr/lib/python2.7/test/test_urllib2.pyttest_badly_named_methodss     c Cst}dddgdgdgd gg}t||}td}|j|}|j||d|ddf|ddfg}x[t||jD]G\}}|\} } } } |j| | f||j| |fqWdS( NRtftp_openthttp_error_302s return selfshttp://example.com/ii(s http_opens return self(s http_opens return self(RRRR9R#tzipR( RRRRR6trRtexpectedtgotthandlerRR;R((s'/usr/lib/python2.7/test/test_urllib2.pyt test_handleds     cCst}g}xqd gdfdgdfgD]Q\}}dtfdY}||}||_|j||j|q.W|jd}|j|jdd|d|j|jdd|ddS( NRs return selfiiRcBseZRS((R-R.(((s'/usr/lib/python2.7/test/test_urllib2.pyRsshttp://example.com/i(s http_opens return self(RRvRRRR9R#R(RRRRRRR"R((s'/usr/lib/python2.7/test/test_urllib2.pyttest_handler_orders     cCs|t}dgdgg}t||}td}|jtj|j||j|j|dd|fifgdS(NRRs return selfshttp://example.com/i(s http_opensraise(s http_opens return self( RRRRR RR9R#R(RRRRR6((s'/usr/lib/python2.7/test/test_urllib2.pyt test_raises   c Cs't}dgddgdddgdgg}t||}d dd Y}td }|j|}t|jd kst|d d|ff|d d||ddiffg}x]t||jD]I\}} |\} } } |j| | f| d |j| | d qWdS(NRs error 302thttp_error_400RRsreturn responsethttp_error_303t http_errortUnknowncBseZdZRS(cSstS(N(R(RR((s'/usr/lib/python2.7/test/test_urllib2.pyt__eq__2s(R-R.R(((s'/usr/lib/python2.7/test/test_urllib2.pyR1sshttp://example.com/iii.Ra(s http_opens error 302(Rsraise(shttp_error_302sreturn response(( RRRR9RyRRRR#( RRRRRR6RRRRRt method_nameR;((s'/usr/lib/python2.7/test/test_urllib2.pyttest_http_error$s$     c Csst}d d gd d gg}t||}td}|j|}|ddf|ddf|ddf|ddfg}xt|jD]\}\}} } } |dkr|j|| f|||jt| d|j| dtq|j|| f|||jt| d|j| dt| ddk r|j| dt qqWdS( Nt http_requestsreturn requestt http_responsesreturn responseshttp://example.com/iii(s http_requestsreturn request(s http_responsesreturn response(s http_requestsreturn request(s http_responsesreturn response( RRRR9t enumerateRR#RytassertIsInstanceR>RG( RRRRR6RRtiRRR;R((s'/usr/lib/python2.7/test/test_urllib2.pyttest_processors?s(    ( ( R-R.RRRRRRR(((s'/usr/lib/python2.7/test/test_urllib2.pyRs      cCsJddl}|j|}tjdkrF|jdrF|d}n|S(NiRs///i(turllibt pathname2urlR RR(R Rturlpath((s'/usr/lib/python2.7/test/test_urllib2.pytsanepathname2urlcs   t HandlerTestscBseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZddZdZdZdZdZdZRS(csndddYdtjffdY}ddl}d}||}t}|_xdd|jd d d d d gd df dd|jdd d d d gd df dd|jdd d d d gd df dd|jdd d d d gd df dddd d dd d gd df dd|jd d dgddf gD]\ }}}} } } } } }t|}d|_|j |}|j |j | |j |j | |j |j tj||j |j||j |j| |j |jj| |j |jj| |j}|j |jd||j t|dt|qHWdS(NtMockFTPWrappercBs#eZdZdZdZRS(cSs ||_dS(N(R7(RR7((s'/usr/lib/python2.7/test/test_urllib2.pyRIoscSs2|||_|_tj|jt|jfS(N(tfilenametfiletypeRHR7Ry(RRR((s'/usr/lib/python2.7/test/test_urllib2.pytretrfilepscSsdS(N((R((s'/usr/lib/python2.7/test/test_urllib2.pyRss(R-R.RIRR(((s'/usr/lib/python2.7/test/test_urllib2.pyRns  tNullFTPHandlercs&eZdZejfdZRS(cSs ||_dS(N(R7(RR7((s'/usr/lib/python2.7/test/test_urllib2.pyRIvscsH|||_|_|||_|_||_|j|_|jS(N(RtpasswdRgRmtdirsR7t ftpwrapper(RRRRgRmRR8(R(s'/usr/lib/python2.7/test/test_urllib2.pyt connect_ftpws  (R-R.RIR?R@R((R(s'/usr/lib/python2.7/test/test_urllib2.pyRus isrheum rhaponicums ftp://localhost/foo/bar/baz.htmlt localhostRatItfootbarsbaz.htmls text/htmls'ftp://parrot@localhost/foo/bar/baz.htmltparrots*ftp://%25parrot@localhost/foo/bar/baz.htmls%parrots,ftp://%2542parrot@localhost/foo/bar/baz.htmls %42parrotsftp://localhost:80/foo/bar/iPtDsftp://localhost/baz.gif;type=atAsbaz.gifs Content-typesContent-length((R t FTPHandlertftplibR5RtFTP_PORTR>RR8RR#RRRgR?t gethostbynameRmRRRRRNtgetRRy(RRRR7R"RRMRgRmRRttype_RRtmimetypeR6RRL((Rs'/usr/lib/python2.7/test/test_urllib2.pyttest_ftpmsL  1   cCsddl}ddl}tj}t}|_tj}tt j j |}d}d|d|d|j d|fg}y|j |j } Wn|jk rd} nX| r|jd| |fnx|D]} t|d} zz| j|Wd| jX|jt| } z(| j} | j}| j}Wd| jXt j|}|j|j}Wdt j|X|j| ||j|d d |j|d d |j|d ||j|| qWxd|dd|j dt j|fdt j|fgD]h} zQt|d} z| j|Wd| jX|jtj|jt| Wdt j|Xq:Wtj}t}|_xdt fdt!fdt!fdt fdt!fgD]\} }t| }y|j|Wn(tjt"fk rI|j#| n*X|j#|j$|k|j|j%d|j|j%dk|qWdS(Nis hello, world sfile://localhost%ss file://%ss file://%s%sRRatwbs Content-types text/plainsContent-lengtht13s Last-modifiedsfile://localhost:80%ssfile:///file_does_not_exist.txtsfile://%s:80%s/%ss,file://somerandomhost.ontheinternet.com%s/%ssfile://ftp.example.com//foo.txts file://ftp.example.com///foo.txtsfile://ftp.example.com/foo.txts"file://somehost//foo/something.txts#file://localhost//foo/something.txttftp(&trfc822R?R t FileHandlerR5RRtTESTFNRR R R Rt gethostnametgaierrorRR9twriteRt file_openRRRNROtstatt formatdatetst_mtimetremoveR#tgetcwdRRRRdtOSErrort assertTrueR6ttype(RRR?R"RRRttowriteturlst localaddrRMRRR7RLtrespurltstatstmodifiedRR6((s'/usr/lib/python2.7/test/test_urllib2.pyt test_files                c Cs!tj}t}|_d}xGddgD]9\}}t||idd6}d|_|jddt}|j ||}|j |j |j |j |j|jd kd f|j } | j| j|j|j ||j|jd |j|jd |j|j||j|jd |j|jdddg|j|j|q/Wt|_|jtj|j ||dg|_x}dD]u}td|}td d id}|j|} |dkr|j d|j!|j d|j!n.|j|j!dd|j|j!dd|j|j!dd |j|j!dd|jdd|jdd|jdd|jdd|j|} |j|j!dd|j|j!dd|j|j!dd|j|j!ddqWdS(Nshttp://example.com/tGETtPOSTRRtFootSpamteggsiRts example.comiRt ConnectionRRasContent-lengths Content-typet0s!application/x-www-form-urlencodedtHostRtbaz(RN(Rsblah(Rsclose(Rsbar(RR(RR(RaN("R RR5RR>RR8tadd_unredirected_headerRbRRRCRNRORJRKRthas_keyR#RgRhRoRpRcR7RReRRR=RGt do_request_t assertNotIntunredirected_hdrs( RR"RRMRoR7R6RRthdrstnewreq((s'/usr/lib/python2.7/test/test_urllib2.pyt test_httpsd          c Cstj}t}|_d}ddddg}xy|D]q}t||}|j|}|j|jdd|jdd|j|}|j|jddq;WdS( NRas#http://example.com/foo/bar/baz.htmls$http://example.com//foo/bar/baz.htmls$http://example.com/foo//bar/baz.htmls$http://example.com/foo/bar//baz.htmlRs example.comssomeproxy:3128( R RR5RRRR#Rt set_proxyR>( RR"RR7tds_urlstds_urltds_reqt np_ds_reqtp_ds_req((s'/usr/lib/python2.7/test/test_urllib2.pyttest_http_doubleslashBs   cCstj}t}|_d}t|}|j|}|j|jd|j|jdd}t|}|j|}|j|jd|j|jddS(Nshttp://www.python.org?getspamswww.python.orgs /?getspamshttp://www.python.orgRa( R RR5RRRR#tget_hostt get_selector(RR"Rt weird_urlR6R!turl_without_path((s'/usr/lib/python2.7/test/test_urllib2.pyttest_fixpath_in_weirdurls\s   cCstj}t}|_d}t|}tddid|}|j||}|j||k|jt|d tddid|}|j||}|j||k|jt|d tdd id|}|j||}|j||k|jt|d td d id|}|j|j||dk|j |j d |j |j ||d d ifdS( Nshttp://example.com/iRtRaR:itAcceptedisPartial contentis Bad gatewayR( R tHTTPErrorProcessorR5RRRGRR RR>R#R:R;(RR"RRMR6Rtnewr((s'/usr/lib/python2.7/test/test_urllib2.pyt test_errorsos(  cCst}tj|}t}|_td}tddid}|j|}|j|j |kox|kn|j |j d|j|j |j ||}|j|j|k|j|j|ko|kndS(Nshttp://example.com/iRtRas example.com(RPR tHTTPCookieProcessorR5RRRGRR RQR#tget_origin_req_hosttis_unverifiableRRTRU(RtcjR"RR6RR!R1((s'/usr/lib/python2.7/test/test_urllib2.pyt test_cookiess  &c Cs#d}d}tj}t}|_xdD]}xdD]}t|d|}t||}|jd d tj |_ |dk r|jd t t |n|j d d y*||t|dti|d6Wn1tjk r|j|d|j|nX|j|jj|y|j|jjdWn(tk ry|j|jj nXg|jjD]} | j^q} |jd| |jd| |j|jjd d |jd |jj|jd |jjq<Wq/Wt|}tj |_ |d} t|dd}d} tj |_ y$x| ||d| d} q[Wn*tjk r|j| tjjnXt|dd}d} tj |_ y(x!| ||d| | d} qWn*tjk r|j| tjjnXdS(Nshttp://example.com/a.htmlshttp://example.com/b.htmli-i.i/i3s blah blah s http_error_%stNonsensesviking=withholdsContent-LengthRtspamtBlahtlocationRscontent-lengths content-typec Ss-|j|tddti|d6dS(Ni.R:R;(RRARD(R"R6RM((s'/usr/lib/python2.7/test/test_urllib2.pyRstorigin_req_hosts example.comishttp://example.com/ishttp://example.com/%d(i-i.i/i3(Ns blah blah (R tHTTPRedirectHandlerR5RR>tgetattrRt add_headerR?R@R8tstrRyRRARDt HTTPErrorR#tassertIsNotNoneR6Rt get_methodtAttributeErrorR thas_dataRLtlowerRRt max_repeatstmax_redirections( Rtfrom_urltto_urlR"RRJR7RoR6txRLRRB((s'/usr/lib/python2.7/test/test_urllib2.pyt test_redirectsj      %      c Csd}dddg}dddg}d}tj}t}|_t|}tj|_xQ|D]I}|d |} |jtj |j |t d d t i| d 6qeWx^|D]V}|d |} |j |t d d t i| d 6|j |jj| qWdS(Nshttp://example.com/a.htmlRthttpsRtfiletimaptldapsexample.com/b.htmls://i.sSecurity LoopholeR;s That's fine(R R=R5RRR?R@R8RRARRARDR#R6R( RRIt valid_schemestinvalid_schemestschemeless_urlR"RR6Rt invalid_urlt valid_url((s'/usr/lib/python2.7/test/test_urllib2.pyttest_invalid_redirects$     c Csddlm}ddlm}|}||ddtdd}tj}tj}tj|}t ||||}|j d|j |j j d dS( Ni(t CookieJar(tinteract_netscapeshttp://www.example.com/s spam=eggsi.s%Location: http://www.cracker.com/ tCookie(t cookielibRWttest.test_cookielibRXRR tHTTPDefaultErrorHandlerR=R3RR9R R6t has_header( RRWRXR6thhthdehthrhtcpR((s'/usr/lib/python2.7/test/test_urllib2.pyttest_cookie_redirects    cCsrd}tdd|}tj}tj}t|||}|jd}|j|j|jdS(Ns(http://www.example.com/index.html#OK i.s Location: shttp://www.example.com( RR R\R=RR9R#ROtstrip(Rtredirected_urlR^R_R`RR^((s'/usr/lib/python2.7/test/test_urllib2.pyttest_redirect_fragments  cCst}tjtdd}|j|d gg}t||}td}|j|jd|j |}|j|jd|j|ddfgg|j D]}|dd!^qdS( NRsproxy.example.com:3128Rsreturn responseshttp://acme.example.com/sacme.example.comii(s http_opensreturn response( RR t ProxyHandlertdictRRRR#R*R9R(RRtphRRR6Rttup((s'/usr/lib/python2.7/test/test_urllib2.pyt test_proxys    cCsdtjdR(RRR|t auth_headerRR}R{t request_urlt protected_urlRRRRtuserpasstauth_hdr_value((s'/usr/lib/python2.7/test/test_urllib2.pyRys.   (R-R.RRR"R)R.R2R7RLRVRbReRjRmRnRuR~RRRRRy(((s'/usr/lib/python2.7/test/test_urllib2.pyRks* : ] >     I           /t MiscTestscBseZdZdZRS(cCsdtjfdY}dtjfdY}dtjfdY}tj}|||}|j|||j|||||}|j|||j||||}|j|||}|j|tj|tj}|j|tj|tj}|j|tjdtjfdY}|||}|j|||j||dS( Nt MyHTTPHandlercBseZRS((R-R.(((s'/usr/lib/python2.7/test/test_urllib2.pyRst FooHandlercBseZdZRS(cSsdS(N((R((s'/usr/lib/python2.7/test/test_urllib2.pytfoo_opens(R-R.R(((s'/usr/lib/python2.7/test/test_urllib2.pyRst BarHandlercBseZdZRS(cSsdS(N((R((s'/usr/lib/python2.7/test/test_urllib2.pytbar_opens(R-R.R(((s'/usr/lib/python2.7/test/test_urllib2.pyRstMyOtherHTTPHandlercBseZRS((R-R.(((s'/usr/lib/python2.7/test/test_urllib2.pyRs(R t HTTPHandlert BaseHandlert build_openertopener_has_handler(RRRRRRR((s'/usr/lib/python2.7/test/test_urllib2.pyttest_build_openers,   cCs8x1|jD]}|j|kr Pq q W|jtdS(N(RR{R Rd(RRt handler_classR"((s'/usr/lib/python2.7/test/test_urllib2.pyRs(R-R.RR(((s'/usr/lib/python2.7/test/test_urllib2.pyRs %t RequestTestscBs}eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d ZRS( cCs8tjd|_tjdddidd6|_dS(Nshttp://www.python.org/~jeremy/R7RLttestsX-Test(R RRtpost(R((s'/usr/lib/python2.7/test/test_urllib2.pytsetUps cCs6|jd|jj|jd|jjdS(NRR(R#RRCR(R((s'/usr/lib/python2.7/test/test_urllib2.pyt test_methodscCss|j|jj |jd|jj|jjd|j|jj|jd|jjdS(NRR9R(R RRER#RCtadd_data(R((s'/usr/lib/python2.7/test/test_urllib2.pyt test_add_datas cCs|jd|jjdS(Nshttp://www.python.org/~jeremy/(R#RR(R((s'/usr/lib/python2.7/test/test_urllib2.pyttest_get_full_url s cCsB|jd|jjtjd}|jd|jdS(Ns /~jeremy/shttp://www.python.org/R(R#RR+R R(RR6((s'/usr/lib/python2.7/test/test_urllib2.pyt test_selector$scCs|jd|jjdS(NR(R#Rtget_type(R((s'/usr/lib/python2.7/test/test_urllib2.pyt test_get_type)scCs|jd|jjdS(Nswww.python.org(R#RR*(R((s'/usr/lib/python2.7/test/test_urllib2.pyt test_get_host,scCs)tjd}|jd|jdS(Nshttp://www.%70ython.org/swww.python.org(R RR#R*(RR6((s'/usr/lib/python2.7/test/test_urllib2.pyttest_get_host_unquote/scCsv|j|jj |jjdd|j|jj|jd|jj|jd|jjdS(Ns www.perl.orgRswww.python.org(R Rt has_proxyR#R#R4R*(R((s'/usr/lib/python2.7/test/test_urllib2.pyRj3s cCs&td}|jd|jdS(Nsswww.python.org(RR#R*(RR6((s'/usr/lib/python2.7/test/test_urllib2.pyttest_wrapped_url:s cCsptd}|jd|jtd}|jd|jd}t|}|j|j|dS(Ns-http://www.python.org/?qs=query#fragment=trues /?qs=queryshttp://www.python.org/#fun=trueRs.http://docs.python.org/library/urllib2.html#OK(RR#R+R(RR6RM((s'/usr/lib/python2.7/test/test_urllib2.pyttest_url_fragment>s   cCsdS(sc Issue 13211 reveals that HTTPError didn't implement the URLError interface even though HTTPError is a subclass of URLError. >>> err = urllib2.HTTPError(msg='something bad happened', url=None, code=None, hdrs=None, fp=None) >>> assert hasattr(err, 'reason') >>> err.reason 'something bad happened' N((R((s'/usr/lib/python2.7/test/test_urllib2.pyttest_HTTPError_interfaceIsc Cstjdddd dd dddd }|jt|dt|dsUtt|d sjtt|jsty|jWntk r|j d nX|j |jdd S( s] Issue 15701= - HTTPError interface has info method available from URLError. RKssomething bad happenedRMRJR sContent-Length:42R^R`RNserr.info() failedN( R RAR>R RRtcallableRNRDtfailR#(Rterr((s'/usr/lib/python2.7/test/test_urllib2.pyttest_HTTPError_interface_callTs (R-R.RRRRRRRRRjRRRR(((s'/usr/lib/python2.7/test/test_urllib2.pyR s           cCsVddlm}tj||tjt|ttttt f}tj |dS(Ni(t test_urllib2( RRRt run_doctestR RRRRRt run_unittest(tverboseRR%((s'/usr/lib/python2.7/test/test_urllib2.pyt test_mainds t__main__R(+R/RRR R?RHR RRR*t ImportErrorR>tTestCaseRR1R2R3R4R5RARgRDRGRPRXR]RbRvRRRRRRRRRRRRRR-R(((s'/usr/lib/python2.7/test/test_urllib2.pytsN       +  ! A /  ),     v.W