{G_Tc@sCddlZddlmZddlZddlZddlZddlZddlmZmZdej fdYZ dZ dZ dZ d Zd fd YZd fd YZdefdYZdejfdYZdfdYZdfdYZdfdYZdfdYZdfdYZdZdZdejfdYZd ejfd!YZd"fd#YZd$ej fd%YZ d&Z!d'ej fd(YZ"d)ej fd*YZ#d+ej fd,YZ$e%d-Z&e'd.kr?e&d/e(ndS(0iN(t test_support(tRequesttOpenerDirectort TrivialTestscBseZdZdZRS(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/local/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/local/lib/python2.7/test/test_urllib2.pyttest_parse_http_list*s (t__name__t __module__RR'(((s-/usr/local/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/local/lib/python2.7/test/test_urllib2.pyttest_request_headers_dict3scCsdS(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/local/lib/python2.7/test/test_urllib2.pyttest_request_headers_methodsQscCsdS(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/local/lib/python2.7/test/test_urllib2.pyttest_password_managerrs>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/local/lib/python2.7/test/test_urllib2.pyt"test_password_manager_default_portst MockOpenercBs)eZgZdejdZdZRS(cCs!||||_|_|_dS(N(treqtdatattimeout(RR/R0R1((s-/usr/local/lib/python2.7/test/test_urllib2.pytopenscGs|||_|_dS(N(tprototargs(RR3R4((s-/usr/local/lib/python2.7/test/test_urllib2.pyterrorsN(R(R)t addheaderstNonetsockett_GLOBAL_DEFAULT_TIMEOUTR2R5(((s-/usr/local/lib/python2.7/test/test_urllib2.pyR.stMockFilecBs)eZddZddZdZRS(cCsdS(N((Rtcount((s-/usr/local/lib/python2.7/test/test_urllib2.pyRscCsdS(N((RR;((s-/usr/local/lib/python2.7/test/test_urllib2.pytreadlinescCsdS(N((R((s-/usr/local/lib/python2.7/test/test_urllib2.pyRsN(R(R)R7RR<R(((s-/usr/local/lib/python2.7/test/test_urllib2.pyR:s  t MockHeaderscBseZdZRS(cCs |jS(N(tvalues(RR((s-/usr/local/lib/python2.7/test/test_urllib2.pyt getheaderss(R(R)R?(((s-/usr/local/lib/python2.7/test/test_urllib2.pyR=st MockResponsecBs&eZddZdZdZRS(cCsAtjj||||||f\|_|_|_|_dS(N(tStringIOt__init__tcodetmsgtheadersturl(RRCRDRER0RF((s-/usr/local/lib/python2.7/test/test_urllib2.pyRBscCs|jS(N(RE(R((s-/usr/local/lib/python2.7/test/test_urllib2.pytinfoscCs|jS(N(RF(R((s-/usr/local/lib/python2.7/test/test_urllib2.pytgeturlsN(R(R)R7RBRGRH(((s-/usr/local/lib/python2.7/test/test_urllib2.pyR@s  t MockCookieJarcBseZdZdZRS(cCs ||_dS(N(tach_req(Rtrequest((s-/usr/local/lib/python2.7/test/test_urllib2.pytadd_cookie_headerscCs|||_|_dS(N(tec_reqtec_r(RtresponseRK((s-/usr/local/lib/python2.7/test/test_urllib2.pytextract_cookiess(R(R)RLRP(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRIs t FakeMethodcBseZdZdZRS(cCs||_||_||_dS(N(t meth_namethandletaction(RRRRTRS((s-/usr/local/lib/python2.7/test/test_urllib2.pyRBs  cGs|j|j|j|S(N(RSRRRT(RR4((s-/usr/local/lib/python2.7/test/test_urllib2.pyt__call__s(R(R)RBRU(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRQs tMockHTTPResponsecBseZdZdZRS(cCs(||_||_||_||_dS(N(tfpRDtstatustreason(RRWRDRXRY((s-/usr/local/lib/python2.7/test/test_urllib2.pyRB s   cCsdS(Nt((R((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs(R(R)RBR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRV s t MockHTTPClasscBsYeZdZejdZdZdddZdddZ dZ dZ RS(cCs(g|_d|_t|_i|_dS(N(t req_headersR7R0tFalsetraise_on_endheaderst_tunnel_headers(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyRBs   cCs||_||_|S(N(thostR1(RR`R1((s-/usr/local/lib/python2.7/test/test_urllib2.pyRUs  cCs ||_dS(N(tlevel(RRa((s-/usr/local/lib/python2.7/test/test_urllib2.pytset_debuglevelscCs5||_||_|r$||_n |jjdS(N(t _tunnel_hostt _tunnel_portR_tclear(RR`tportRE((s-/usr/local/lib/python2.7/test/test_urllib2.pyt set_tunnel!s    cCs}||_||_|dk r6|j|j7_n|jj|rU||_n|jryddl}|j ndS(Ni( tmethodtselectorR7R\titemstsortR0R^R8R5(RRhRFtbodyRER8((s-/usr/local/lib/python2.7/test/test_urllib2.pyRK)s       cCsttiddS(NitOK(RVR:(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyt getresponse5scCsdS(N((R((s-/usr/local/lib/python2.7/test/test_urllib2.pyR8sN( R(R)RBR8R9RURbR7RgRKRnR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyR[s   t MockHandlercBsDeZdZdZdZdZdZdZdZRS(icCs|j|dS(N(t_define_methods(Rtmethods((s-/usr/local/lib/python2.7/test/test_urllib2.pyRB?scCskxd|D]\}t|dkr.|\}}n |d}}t|||j}t|j||qWdS(Ni(tlenR7RQRStsetattrt __class__(RRqtspecRRTtmeth((s-/usr/local/lib/python2.7/test/test_urllib2.pyRpAs   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 responseiRmRZsreturn requests http://blah/R5t ithttpitraisetblah(tparenttcallstappendR7R@Rt startswithtrfindtintRR5R tURLErrorR]tAssertionError(Rtfn_nameRTR4tkwdstresRC((s-/usr/local/lib/python2.7/test/test_urllib2.pyRSGs*      # cCsdS(N((R((s-/usr/local/lib/python2.7/test/test_urllib2.pyR]scCs||_g|j_dS(N(R{R|(RR{((s-/usr/local/lib/python2.7/test/test_urllib2.pyt add_parent^s cCs#t|dstS|j|jkS(Nt handler_order(thasattrtTrueR(Rtother((s-/usr/local/lib/python2.7/test/test_urllib2.pyt__lt__as( R(R)RRBRpRSRRR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRo;s     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/local/lib/python2.7/test/test_urllib2.pyR}si(RoRRR}t add_handler(topenert meth_specthandlersR;tmethsRR"((s-/usr/local/lib/python2.7/test/test_urllib2.pytadd_ordered_mock_handlersgs     cGs+t}x|D]}|j|qW|S(N(RR(thandler_instancesRR"((s-/usr/local/lib/python2.7/test/test_urllib2.pytbuild_test_openers  tMockHTTPHandlercBs#eZdZdZdZRS(cCs ||_||_|jdS(N(RCREtreset(RRCRE((s-/usr/local/lib/python2.7/test/test_urllib2.pyRBs  cCsd|_g|_dS(Ni(t_counttrequests(R((s-/usr/local/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(RAiiRxs iRmRZ(t mimetoolsthttplibtcopyRARR}tdeepcopyRt responsesRCtMessageRER{R5R:R/R@t get_full_url(RR/RRRRARRD((s-/usr/local/lib/python2.7/test/test_urllib2.pyt http_opens$  (R(R)RBRR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs  tMockHTTPSHandlercBseZdZdZRS(cCs tjj|t|_dS(N(R tAbstractHTTPHandlerRBR[thttpconn(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyRBscCs|j|j|S(N(tdo_openR(RR/((s-/usr/local/lib/python2.7/test/test_urllib2.pyt https_opens(R(R)RBR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs tMockPasswordManagercBseZdZdZRS(cCs(||_||_||_||_dS(N(trealmRFtusertpassword(RRturiRR((s-/usr/local/lib/python2.7/test/test_urllib2.pyt add_passwords   cCs"||_||_|j|jfS(N(t target_realmt target_urlRR(RRtauthuri((s-/usr/local/lib/python2.7/test/test_urllib2.pytfind_user_passwords  (R(R)RR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs tOpenerDirectorTestscBsGeZdZdZdZdZdZdZdZRS(cCs6dtfdY}|jttj|dS(Nt NonHandlercBseZRS((R(R)(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs(tobjectRt TypeErrorRR(RR((s-/usr/local/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 RRRRtUnknownHandlerRR2(RRtoRRtscheme((s-/usr/local/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(RRRR2R#tzipR|( RRRRR/trR|texpectedtgotthandlerRR4R((s-/usr/local/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/local/lib/python2.7/test/test_urllib2.pyRsshttp://example.com/i(s http_opens return self(RRoRR}RR2R#R|(RRRRRRR"R((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_handler_orders     cCs|t}dgdgg}t||}td}|jtj|j||j|j|dd|fifgdS(NRRys return selfshttp://example.com/i(s http_opensraise(s http_opens return self( RRRRR RR2R#R|(RRRRR/((s-/usr/local/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_400RyRsreturn responsethttp_error_303t http_errortUnknowncBseZdZRS(cSstS(N(R(RR((s-/usr/local/lib/python2.7/test/test_urllib2.pyt__eq__%s(R(R)R(((s-/usr/local/lib/python2.7/test/test_urllib2.pyR$sshttp://example.com/iii.RZ(s http_opens error 302(Rsraise(shttp_error_302sreturn response(( RRRR2RrR|RRR#( RRRRRR/RR|RRRt method_nameR4((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_http_errors$     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( RRRR2t enumerateR|R#RrtassertIsInstanceR7R@( RRRRR/RR|tiRRR4R((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_processors2s(    ( ( R(R)RRRRRRR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs      cCsJddl}|j|}tjdkrF|jdrF|d}n|S(NiRs///i(turllibt pathname2urlR RR~(R Rturlpath((s-/usr/local/lib/python2.7/test/test_urllib2.pytsanepathname2urlVs   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(R0(RR0((s-/usr/local/lib/python2.7/test/test_urllib2.pyRBbscSs2|||_|_tj|jt|jfS(N(tfilenametfiletypeRAR0Rr(RRR((s-/usr/local/lib/python2.7/test/test_urllib2.pytretrfilecscSsdS(N((R((s-/usr/local/lib/python2.7/test/test_urllib2.pyRfs(R(R)RBRR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRas  tNullFTPHandlercs&eZdZejfdZRS(cSs ||_dS(N(R0(RR0((s-/usr/local/lib/python2.7/test/test_urllib2.pyRBiscsH|||_|_|||_|_||_|j|_|jS(N(RtpasswdR`RftdirsR0t ftpwrapper(RRRR`RfRR1(R(s-/usr/local/lib/python2.7/test/test_urllib2.pyt connect_ftpjs  (R(R)RBR8R9R((R(s-/usr/local/lib/python2.7/test/test_urllib2.pyRhs isrheum rhaponicums ftp://localhost/foo/bar/baz.htmlt localhostRZtItfootbarsbaz.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 FTPHandlertftplibR.R{tFTP_PORTR7RR1RR#RRR`R8t gethostbynameRfRRRRRGtgetRRr(RRRR0R"RRFR`RfRRttype_RRtmimetypeR/RRE((Rs-/usr/local/lib/python2.7/test/test_urllib2.pyttest_ftp`sL  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%sRRZtwbs 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(&trfc822R8R t FileHandlerR.R{RtTESTFNRR R R Rt gethostnametgaierrorR}R2twriteRt file_openRRRGRHtstatt formatdatetst_mtimetremoveR#tgetcwdRRRR]tOSErrort assertTrueR/ttype(RRR8R"RRRttowriteturlst localaddrRFRRR0REtrespurltstatstmodifiedRR/((s-/usr/local/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/tGETtPOSTRzRtFootSpamteggsiRms example.comiRt ConnectionRRZsContent-lengths Content-typet0s!application/x-www-form-urlencodedtHostRtbaz(R N(R sblah(Rsclose(R sbar(RR(RR(RZN("R RR.R{R7RR1tadd_unredirected_headerR[RRR<RGRHRCRDRthas_keyR#R`RaRhRiR\R0RR^RRR6R@t do_request_t assertNotIntunredirected_hdrs( RR"RRFRhR0R/RxRthdrstnewreq((s-/usr/local/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( NRZs#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 RR.R{RRR#Rt set_proxyR7( RR"RR0tds_urlstds_urltds_reqt np_ds_reqtp_ds_req((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_http_doubleslash5s   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.orgRZ( R RR.R{RRR#tget_hostt get_selector(RR"Rt weird_urlR/Rturl_without_path((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_fixpath_in_weirdurlsOs   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/iRmRZR3itAcceptedisPartial contentis Bad gatewayRx( R tHTTPErrorProcessorR.R{RR@RRRR7R#R3R4(RR"RRFR/Rtnewr((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_errorsbs(  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/iRmRZs example.com(RIR tHTTPCookieProcessorR.R{RR@RRRJR#tget_origin_req_hosttis_unverifiableRRMRN(RtcjR"RR/RRR*((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_cookies{s  &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-LengthRtspamtBlahtlocationR scontent-lengths content-typec Ss-|j|tddti|d6dS(Ni.R3R4(RR:R=(R"R/RF((s-/usr/local/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 tHTTPRedirectHandlerR.R{R7tgetattrRt add_headerR8R9R1tstrRrRR:R=t HTTPErrorR#tassertIsNotNoneR/Rt get_methodtAttributeErrorRthas_dataREtlowerRRt max_repeatstmax_redirections( Rtfrom_urltto_urlR"RRCR0RhR/txRERR;((s-/usr/local/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.htmlRxthttpsRtfiletimaptldapsexample.com/b.htmls://i.sSecurity LoopholeR4s That's fine(R R6R.R{RR8R9R1RR:RR:R=R#R/R( RRBt valid_schemestinvalid_schemestschemeless_urlR"RR/Rt invalid_urlt valid_url((s-/usr/local/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 cookielibRPttest.test_cookielibRQRR tHTTPDefaultErrorHandlerR6R,RR2RR/t has_header( RRPRQR/thhthdehthrhtcpR((s-/usr/local/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 RUR6RR2R#RHtstrip(Rtredirected_urlRWRXRYRRW((s-/usr/local/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( NRxsproxy.example.com:3128Rsreturn responseshttp://acme.example.com/sacme.example.comii(s http_opensreturn response( RR t ProxyHandlertdictRRRR#R#R2R|(RRtphRRR/Rttup((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_proxys    cCsdtjd     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/local/lib/python2.7/test/test_urllib2.pyRst FooHandlercBseZdZRS(cSsdS(N((R((s-/usr/local/lib/python2.7/test/test_urllib2.pytfoo_opens(R(R)R(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRst BarHandlercBseZdZRS(cSsdS(N((R((s-/usr/local/lib/python2.7/test/test_urllib2.pytbar_opens(R(R)R(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRstMyOtherHTTPHandlercBseZRS((R(R)(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs(R t HTTPHandlert BaseHandlert build_openertopener_has_handler(RRRRRRR((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_build_openers,   cCs8x1|jD]}|j|kr Pq q W|jtdS(N(RRtRR](RRt handler_classR"((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs(R(R)RR(((s-/usr/local/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/R0REttestsX-Test(R RRtpost(R((s-/usr/local/lib/python2.7/test/test_urllib2.pytsetUps cCs6|jd|jj|jd|jjdS(NR R (R#RR<R(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_methodscCss|j|jj |jd|jj|jjd|j|jj|jd|jjdS(NR R2R (RRR>R#R<tadd_data(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_add_data s cCs|jd|jjdS(Nshttp://www.python.org/~jeremy/(R#RR(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_get_full_urls cCsB|jd|jjtjd}|jd|jdS(Ns /~jeremy/shttp://www.python.org/R(R#RR$R R(RR/((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_selectorscCs|jd|jjdS(NRx(R#Rtget_type(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_get_typescCs|jd|jjdS(Nswww.python.org(R#RR#(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_get_hostscCs)tjd}|jd|jdS(Nshttp://www.%70ython.org/swww.python.org(R RR#R#(RR/((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_get_host_unquote"scCsv|j|jj |jjdd|j|jj|jd|jj|jd|jjdS(Ns www.perl.orgRxswww.python.org(RRt has_proxyRR#R-R#(R((s-/usr/local/lib/python2.7/test/test_urllib2.pyRc&s cCs&td}|jd|jdS(Nsswww.python.org(RR#R#(RR/((s-/usr/local/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(RR/RF((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_url_fragment1s   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/local/lib/python2.7/test/test_urllib2.pyttest_HTTPError_interface<sc 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. RDssomething bad happenedRFRCRsContent-Length:42RWRYRGserr.info() failedN( R R:R7RRRtcallableRGR=tfailR#(Rterr((s-/usr/local/lib/python2.7/test/test_urllib2.pyttest_HTTPError_interface_callGs (R(R)RRRRRRRRRcRRRR(((s-/usr/local/lib/python2.7/test/test_urllib2.pyRs           cCsVddlm}tj||tjt|ttttt f}tj |dS(Ni(t test_urllib2( RRRt run_doctestR RRRRRt run_unittest(tverboseRR%((s-/usr/local/lib/python2.7/test/test_urllib2.pyt test_mainWs t__main__R()tunittestRRR R8RAR RRtTestCaseRR*R+R,R-R.R:R`R=R@RIRQRVR[RoRRRRRRRRRRRRR7RR(R(((s-/usr/local/lib/python2.7/test/test_urllib2.pytsF     #  ! A /  ),     v.W