a zed4@sddlZddlmZddlmZddlmZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Zddl mZmZmZmZmZmZmZddlmZddlZddlZGdddejZGd d d ejZGd d d ZGd ddZGddde Z!Gdddej"Z#GdddZ$GdddZ%Gdddej&Z'GdddZ(GdddZ)ddZ*dd Z+Gd!d"d"ej,j-Z.Gd#d$d$ej,j/Z0Gd%d&d&ej,j-Z1Gd'd(d(Z2Gd)d*d*ejZ3d+d,Z4Gd-d.d.ejZ5Gd/d0d0ejZ6Gd1d2d2ejZ7e8d3kre9dS)4N)support) socket_helper) test_urllib)RequestOpenerDirectorHTTPBasicAuthHandlerHTTPPasswordMgrWithPriorAuth _parse_proxy_proxy_bypass_macosx_sysconfAbstractDigestAuthHandler)urlparsec@s,eZdZddZddZddZddZd S) TrivialTestsc CsdD]}i}td|||d=|dkrdtjdkrd|d|d}}||jd||jd|D](\}}||jd |d |||jfqlqdS) N)requestresponseparseerror robotparserzfrom urllib.%s import * __builtins__rnt url2pathname pathname2urlZ nturl2pathz urllib.%sz.%r is exposed in 'urllib.%s' but defined in %r)execosnamepop assertEqual __module__items)selfmodulecontextupkvr%E/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_urllib2.py test___all__!s zTrivialTests.test___all__cCs|tjj|ttjjdtj tjj  tj d}tj dkrNd|}nd|}tj|}|Wdn1s0YdS)Nz bogus url/rz file:///%s file://%s) addCleanupurllibr urlcleanup assertRaises ValueErrorurlopenrpathabspath__file__replaceseprread)rZfnameZfile_urlfr%r%r& test_trivial0s  zTrivialTests.test_trivialcCsRdgdfdddgfdgdfdgd fg}|D]\}}|tj||q0dS) Nza,b,c)abczpath"o,l"og"i"cal, examplezpath"o,l"og"i"calZexampleza, b, "c", "d", "e,f", g, h)r8r9z"c"z"d"z"e,f"ghza="b\"c", d="e\,f", g="h\\i")za="b"c"zd="e,f"zg="h\i")rr+rZparse_http_list)rtestsstringlistr%r%r&test_parse_http_listCs   z!TrivialTests.test_parse_http_listcCs"tjd}||jt|dS)Nreason)r+rURLErrorassertInrAstr)rerrr%r%r&test_URLError_reasonstrNs z$TrivialTests.test_URLError_reasonstrN)__name__r __qualname__r'r7r@rFr%r%r%r&r s r c@s,eZdZddZddZddZddZd S) RequestHdrsTestscCsDd}|t|ddidjdd|t|ddidjdddS)a( 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. http://example.com Spam-eggsblahheadersz spam-EggSN)rrrN)rurlr%r%r&test_request_headers_dictUs z*RequestHdrsTests.test_request_headers_dictcCsd}t|ddid}||d||dg|dd|t|ddg||d || d || d d d | d||d| d d ||d | d ||d d S)a; 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 http.client). 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. Method r.remove_header should remove items both from r.headers and r.unredirected_hdrs dictionaries rJrKrLrM)rKrLzFoo-Barbaz)zFoo-barrQz Not-theredefaultzUnredirected-spamZEggsN) r assertTrue has_headerrZ header_items add_headersorted assertFalse assertIsNone get_headerZ remove_headeradd_unredirected_header)rrOreqr%r%r&test_request_headers_methodsjs"    z-RequestHdrsTests.test_request_headers_methodscCstj}|j}|j}|dddd|dddd|dddd |dd d d |dd dd||ddd||ddd||ddd||ddd||ddd||ddd|d ddd|d ddd||d dd||d dd|dddd|dddd ||ddd!|d"dd#d"||d"dd$||d"d%d&||dd'd&||dd(d)||ddd)||dd d*||dd d+dS),Nz Some Realmhttp://example.com/joepasswordzhttp://example.com/ninizhttp://c.example.com:31283r:z d.example.com4dze.example.com:31285e example.com)r^r_rJzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foofoozhttp://example.com/barbarnini)rgr`)rhrir9firstrLsecondspam)rkrlr81)rmr8zhttp://a.example.com/NNz c.example.comzc.example.com:3128)rar:)rbrc)rdrer+rZHTTPPasswordMgr add_passwordfind_user_passwordrrZmgraddZfind_user_passr%r%r&test_password_managers              z&RequestHdrsTests.test_password_managercCsrtj}|j}|j}|dddd|dddd|d d d d |d ddd||ddd||ddd||ddd||ddd||ddd||ddd||ddd||ddd||ddd||d dd||d d d||d dd||d dd||d d d||d d!d||d d"dd#S)$z 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. r6zhttp://g.example.com:8010jr;zhttp://h.example.com11r#r<zi.example.com:8012liz j.example.com13mzg.example.com:100rnzg.example.com:80)rurvz g.example.comzhttp://g.example.com:100zhttp://g.example.comz h.example.com)rwr#zh.example.com:80zhttp://h.example.com:80z i.example.com)rxryzhttp://i.example.com:80)r{r|zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80Nrorrr%r%r&"test_password_manager_default_portsZ            z3RequestHdrsTests.test_password_manager_default_portN)rGrrHrPr\rtr}r%r%r%r&rISs$FrIc@s(eZdZgZdejfddZddZdS) MockOpenerNcCs||||_|_|_dSN)r[datatimeout)rr[rrr%r%r&openszMockOpener.opencGs|||_|_dSr)protoargs)rrrr%r%r&rszMockOpener.error)rGrrH addheaderssocket_GLOBAL_DEFAULT_TIMEOUTrrr%r%r%r&r~sr~c@s(eZdZdddZd ddZddZdS) MockFileNcCsdSrr%rcountr%r%r&r5 sz MockFile.readcCsdSrr%rr%r%r&readline szMockFile.readlinecCsdSrr%rr%r%r&closeszMockFile.close)N)N)rGrrHr5rrr%r%r%r&rs  rc@seZdZddZdS) MockHeaderscCs t|Sr)r?values)rrr%r%r& getheadersszMockHeaders.getheadersN)rGrrHrr%r%r%r&rsrc@s&eZdZdddZddZddZdS) MockResponseNcCs.tj||||||f\|_|_|_|_dSr)ioStringIO__init__codemsgrNrO)rrrrNrrOr%r%r&rszMockResponse.__init__cCs|jSrrMrr%r%r&infoszMockResponse.infocCs|jSrrOrr%r%r&geturl szMockResponse.geturl)N)rGrrHrrrr%r%r%r&rs rc@seZdZddZddZdS) MockCookieJarcCs ||_dSr)ach_req)rrr%r%r&add_cookie_header%szMockCookieJar.add_cookie_headercCs|||_|_dSr)ec_reqec_r)rrrr%r%r&extract_cookies(szMockCookieJar.extract_cookiesN)rGrrHrrr%r%r%r&r$src@seZdZddZddZdS) FakeMethodcCs||_||_||_dSr) meth_namehandleaction)rrrrr%r%r&r-szFakeMethod.__init__cGs|j|j|jg|RSr)rrr)rrr%r%r&__call__2szFakeMethod.__call__N)rGrrHrrr%r%r%r&r,src@s,eZdZddZddZddZddZd S) MockHTTPResponsecCs"||_||_||_||_d|_dS)N)fprstatusrAr)rrrrrAr%r%r&r7s zMockHTTPResponse.__init__cCsdS)Nr%rr%r%r&r5>szMockHTTPResponse.readcCsiSrr%rr%r%r&rAszMockHTTPResponse.infocCs|jSrrrr%r%r&rDszMockHTTPResponse.geturlN)rGrrHrr5rrr%r%r%r&r6src@sTeZdZddZejfddZddZddd Zdd d d d Z ddZ ddZ dS) MockHTTPClasscCs(d|_g|_d|_d|_d|_i|_dS)NrF)level req_headersrraise_on_endheaderssock_tunnel_headersrr%r%r&rIs zMockHTTPClass.__init__cCs||_||_|Sr)hostr)rrrr%r%r&rQszMockHTTPClass.__call__cCs ||_dSr)r)rrr%r%r&set_debuglevelVszMockHTTPClass.set_debuglevelNcCs&||_||_|r||_n |jdSr) _tunnel_hostZ _tunnel_portrclear)rrportrNr%r%r& set_tunnelYs zMockHTTPClass.set_tunnelF)encode_chunkedcCsP||_||_|dur&|j|7_|j|r:||_||_|jrLtdSr) methodselectorrrsortrrrOSError)rrrObodyrNrr%r%r&ras zMockHTTPClass.requestcCsttiddS)NrOK)rrrr%r%r& getresponsenszMockHTTPClass.getresponsecCsdSrr%rr%r%r&rqszMockHTTPClass.close)NN)NN) rGrrHrrrrrrrrrr%r%r%r&rHs  rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) MockHandlercCs||dSr)_define_methods)rmethodsr%r%r&rzszMockHandler.__init__cCsJ|D]@}t|dkr|\}}n |d}}t|||j}t|j||qdS)N)lenrrsetattr __class__)rrspecrrmethr%r%r&r}s    zMockHandler._define_methodscOs|jj||||f|dur"dS|dkr.|S|dkrHtddid}|S|dkrXtdS|dr||d d d}z t|}WntyYn0tddid}|j d |d ||diS|d krt j ddsJdS)N return selfreturn responserrrreturn requestz http://blah/r httprraiserLF) parentcallsappendrr startswithrfindintr.rr+rB)rZfn_namerrkwdsresrr%r%r&rs*    zMockHandler.handlecCsdSrr%rr%r%r&rszMockHandler.closecCs||_g|j_dSr)rr)rrr%r%r& add_parentszMockHandler.add_parentcCst|dsdS|j|jkS)N handler_orderT)hasattrr)rotherr%r%r&__lt__s zMockHandler.__lt__N) rGrrHrrrrrrrr%r%r%r&rus rcCsbg}d}|D]P}Gdddt}||}|j|7_|||d}||||q |S)aCreate 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. rc@s eZdZdS)z6add_ordered_mock_handlers..MockHandlerSubclassNrGrrHr%r%r%r&MockHandlerSubclasssrr)rrrr add_handler)opener meth_spechandlersrmethsrr<r%r%r&add_ordered_mock_handlerss   rcGst}|D]}||q |Sr)rr)Zhandler_instancesrr<r%r%r&build_test_openers rc@s$eZdZddZddZddZdS)MockHTTPHandlercCs||_||_|dSr)rrNreset)rrrNr%r%r&rszMockHTTPHandler.__init__cCsd|_g|_dS)Nr)_countrequestsrr%r%r&rszMockHTTPHandler.resetcCsddl}ddl}|j|||jdkrl|jd|_tjj|j }| |j }|j d|t|j ||S||_| d}tdd|d|SdS)Nrrr rrr)emailcopyrrdeepcopyrrclient responsesrZmessage_from_stringrNrrrr[r get_full_url)rr[rrrrr%r%r& http_opens    zMockHTTPHandler.http_openNrGrrHrrrr%r%r%r&rsrc@seZdZdddZddZdS)MockHTTPSHandlerrcCstjjj||dt|_dS)N debuglevel)r+rAbstractHTTPHandlerrrhttpconn)rrr%r%r&rszMockHTTPSHandler.__init__cCs||j|Sr)do_openrrr[r%r%r& https_openszMockHTTPSHandler.https_openN)r)rGrrHrrr%r%r%r&rs rc@s$eZdZddZddZddZdS)MockHTTPHandlerCheckAuthcCs||_d|_dSNF)rhas_auth_header)rrr%r%r&rsz!MockHTTPHandlerCheckAuth.__init__cCs d|_dSr)rrr%r%r&rszMockHTTPHandlerCheckAuth.resetcCs6|drd|_tjj|j}t|j|td|S)N AuthorizationTr) rTrrrrrrrr)rr[rr%r%r&rs z"MockHTTPHandlerCheckAuth.http_openNrr%r%r%r&rsrc@seZdZddZddZdS)MockPasswordManagercCs||_||_||_||_dSr)realmrOuserr_)rrZurirr_r%r%r&rpsz MockPasswordManager.add_passwordcCs||_||_|j|jfSr) target_realm target_urlrr_)rrZauthurir%r%r&rqsz&MockPasswordManager.find_user_passwordN)rGrrHrprqr%r%r%r&rsrc@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)OpenerDirectorTestscCs(Gdddt}|ttj|dS)Nc@s eZdZdS)z.NonHandlerNrr%r%r%r& NonHandlersr)objectr- TypeErrorrr)rrr%r%r&test_add_non_handlers z(OpenerDirectorTests.test_add_non_handlercCs\ddlm}t}ddgdgg}t|||tjdD]}|||j |dq>dS)Nr)rB)rr)Z proxy_openr)Zredirect_requestr)Zdoproxyredirectz://example.com/) urllib.errorrBrrrr+rZUnknownHandlerr-r)rrBorschemer%r%r&test_badly_named_methods$s  z,OpenerDirectorTests.test_badly_named_methodsc Cst}gddgdgdgg}t||}td}||}|||d|ddf|ddfg}t||jD]2\}}|\} } } } || | f||| |fqldS)N)rftp_openhttp_error_302rrrr]rrr)rrrrrzipr) rr rrr[rrexpectedgothandlerrrrr%r%r& test_handled9s   z OpenerDirectorTests.test_handledcCst}g}dgdfdgdffD]:\}}Gdddt}||}||_||||q|d||jdd|d||jdd|ddS) Nrrrrc@s eZdZdS)zCOpenerDirectorTests.test_handler_order..MockHandlerSubclassNrr%r%r%r&rXsrr]r)rrrrrrrr)rr rrrrr<r%r%r&test_handler_orderSs    z&OpenerDirectorTests.test_handler_ordercCsZt}dgdgg}t||}td}|tjj|j|||j |dd|fifgdS)N)rrrr]rr) rrrr-r+rrBrrr)rr rrr[r%r%r& test_raisees zOpenerDirectorTests.test_raisec Cst}dgddggddgg}t||}td}||t|jdksLJ|dd|ff|dd|tjd d iffg}t||jD]:\}}|\}} } | || f|dd| | |dqdS) N)rz error 302)Zhttp_error_400rr))rrZhttp_error_303Z http_errorrr]rr.r) rrrrrrrZ ALWAYS_EQrr) rr rrr[rrrrZ method_namerr%r%r&test_http_errorrs&   z#OpenerDirectorTests.test_http_errorc Cs t}ddgddgg}t||}td}|||ddf|ddf|ddf|ddfg}t|jD]\}\}}} } |dkr|||f|||t| d|| dtqh|||f|||t| d|| dt| ddurh|| dt qhdS) N) http_requestr) http_responserr]rrrrr) rrrr enumeraterrrZassertIsInstancer) rr rrr[rrzrrrrr%r%r&test_processorss0   z#OpenerDirectorTests.test_processorsN) rGrrHrr rrrrrr%r%r%r&rs rcCs\z|dWnty*tdYn0tj|}tjdkrX| drX|dd}|S)Nzutf-8zpath is not encodable to utf8rz///r) encodeUnicodeEncodeErrorunittestZSkipTestr+rrrrr)r0urlpathr%r%r&sanepathname2urls   r#c@s^eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Ze e!j"d9kd:d;d<Z#d=d>Z$d?d@Z%dAdBZ&dCdDZ'dEdFZ(dGdHZ)dIdJZ*dKdLZ+dMdNZ,dOdPZ-dQdRZ.dSS)T HandlerTestscsGdddGfdddtjj}ddl}d}||}t|_dd|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 fD]\ }}}}} } } } } t|}d|_| |}| |j || |j | | |j t|| |j|| |j| | |jj| | |jj| |}| |d| | t|dt|qdS)Nc@s$eZdZddZddZddZdS)z-HandlerTests.test_ftp..MockFTPWrappercSs ||_dSrrrrr%r%r&rsz6HandlerTests.test_ftp..MockFTPWrapper.__init__cSs$|||_|_t|jt|jfSr)filenamefiletyperrrr)rr'r(r%r%r&retrfilesz6HandlerTests.test_ftp..MockFTPWrapper.retrfilecSsdSrr%rr%r%r&rsz3HandlerTests.test_ftp..MockFTPWrapper.closeN)rGrrHrr)rr%r%r%r&MockFTPWrappersr*cs&eZdZddZejffdd ZdS)z-HandlerTests.test_ftp..NullFTPHandlercSs ||_dSrr%r&r%r%r&rsz6HandlerTests.test_ftp..NullFTPHandler.__init__cs4|||_|_|||_|_||_|j|_|jSr)rpasswdrrdirsr ftpwrapper)rrr+rrr,rr*r%r& connect_ftps  z9HandlerTests.test_ftp..NullFTPHandler.connect_ftpN)rGrrHrrrr/r%r.r%r&NullFTPHandlersr0rzrheum rhaponicumz ftp://localhost/foo/bar/baz.html localhostrIrgrhzbaz.htmlz text/htmlz'ftp://parrot@localhost/foo/bar/baz.htmlZparrotz*ftp://%25parrot@localhost/foo/bar/baz.htmlz%parrotz,ftp://%2542parrot@localhost/foo/bar/baz.htmlz %42parrotzftp://localhost:80/foo/bar/PDzftp://localhost/baz.gif;type=aAzbaz.gif Content-typeContent-length)r+rZ FTPHandlerftplibr~rZFTP_PORTrrrrrr+rr gethostbynamerr,r-r'r(rgetrr)rr0r8rr<rOrrrr+type_r,r'Zmimetyper[rrNr%r.r&test_ftpsX              zHandlerTests.test_ftpc Csddl}tj}t}|_tj}tt j |}d}d|d|dt d|fg}zt t }Wnt jyd}Yn0|r|d||f|D]} t|d} zz| |W| n | 0|t| } z$| } | } | }W| n | 0t |}|jj|jd d }Wt |n t |0|| ||| d d || d d|| d|||| qd|dddt dt |fdt |ffD]f} zRt|d} z| |W| n | 0| tj!j"|jt| Wt |n t |0qtj}t}|_dD]r\} }t| }z||Wn"tj!j"y|#|Yn0|$|j%|||j&d||j&dk|qBdS)Nrs hello, world zfile://localhost%sr)z file://%s%sr1rwbT)Zusegmtr6z text/plainr7r{z Last-modifiedzfile://localhost:80%szfile:///file_does_not_exist.txtz)file://not-a-local-host.com//dir/file.txtzfile://%s:80%s/%sz,file://somerandomhost.ontheinternet.com%s/%s))zfile://ftp.example.com//foo.txtF)z file://ftp.example.com///foo.txtF)zfile://ftp.example.com/foo.txtF)z"file://somehost//foo/something.txtF)z#file://localhost//foo/something.txtFftp)'Z email.utilsr+r FileHandlerr~rrTESTFNr#rr0r1rr9 gethostnamegaierrorrrwriterZ file_openrr5rrstatZutilsZ formatdatest_mtimeremovergetcwdr-rrBrWassertIsr[type)rrr<r r@r"ZtowriteurlsZ localaddrrOr6rrrNZrespurlZstatsZmodifiedr>r[r%r%r& test_files                  zHandlerTests.test_filec Csftj}t}|_d}dD]\}}t||ddi}d|_|ddt}| ||}|j |j |j |j |j|jdkdf| } | j| j|| |||jd ||jd ||j|||jd ||jgd ||j|qd |_|tjj|j ||tdd}|t|j|dg|_dD]}td|}t ddid}||} |dur|!d|j"|!d|j"n$||j"dd||j"dd||j"dd ||j"dd|dd|dd|dd|dd||} ||j"dd||j"dd||j"dd||j"ddqDdS)Nr]))GETN)POSTsblahFoorhSpameggsrrrfrr()) Connectionr)rNrhrOrPTZbadpostrR)Nrr7r60z!application/x-www-form-urlencodedHostrgrQ)#r+rrr~rrrrZrrr5rrrrrr: __contains__rrrrrrrrr-rrBr do_request_rr assertNotInunredirected_hdrs) rr<r rOrrr[rrhdrsnewreqr%r%r& test_httpWsd                   zHandlerTests.test_httpc Cstj}t}|_tjddd}|j}|| t j |t |dL}t d|i}||}|d}||d||dWdn1s0Yt |dP}t d|d d i}||}|t|dd ||dWdn1s0YdS) Nzw+bF)modedeleterbr]Transfer-encodingchunkedr7Content-Length)r+rrr~rtempfileNamedTemporaryFilerrr*runlinkrrrWrYrrWrTr) rr<r file_objZ file_pathr6r[r[ter%r%r&test_http_body_files"       .  z HandlerTests.test_http_body_filecCstj}t}|_t}td|i}||}| | dd| | dddi}td||}||}| t | dd| | d|dS)Nr]r`rar7rbrc)r+rrr~rrBytesIOrrWrrYrWrTrr)rr<r rgr[r[rNr%r%r&test_http_body_fileobjs      z#HandlerTests.test_http_body_fileobjc Cstj}t}|_tjddg}iddifD]}tj|tj dh}t d|j |}| |}|s| |dd| |dd n| t|ddWdq.1s0Yq.dS) Nz-cpassrbrc)stdoutr]r7r`ra)r+rrr~rsys executable subprocessPopenPIPErrmrWrrYr)rr<r cmdrNprocr[r[r%r%r&test_http_body_pipes     z HandlerTests.test_http_body_pipecCstj}t}|_dd}iddifD]\}td||}||}|sp||dd||ddq*|t |ddq*dS) Ncss dVdS)Nsoner%r%r%r%r& iterable_bodysz;HandlerTests.test_http_body_iterable..iterable_bodyrb r]r7r`ra) r+rrr~rrrWrrYr)rr<r rvrNr[r[r%r%r&test_http_body_iterables    z$HandlerTests.test_http_body_iterablecCsHtj}t|_|tdd}||dd| | ddS)Nr]r%r`rar7) r+rrr~rrWrrrYrWrT)rr<r[r%r%r&test_http_body_empty_seqs  z%HandlerTests.test_http_body_empty_seqcCshtj}t}|_tdgd}iddifD]0}td||}||}|t | ddq2dS)Nr2)rrrbr]r7) r+rrr~rarrayrrWrrrY)rr<r Ziterable_arrayrNr[r[r%r%r&test_http_body_arrays    z!HandlerTests.test_http_body_arraycCs6t}tdd}|||d||jddS)Nrrzhttps://www.example.com)rrrrrZ _debuglevel)rr r<r%r%r&test_http_handler_debuglevels    z)HandlerTests.test_http_handler_debuglevelcCsztj}t|_d}gd}|D]R}t||}||}||jdd| dd||}||jddq"dS)NrS)z#http://example.com/foo/bar/baz.htmlz$http://example.com//foo/bar/baz.htmlz$http://example.com/foo//bar/baz.htmlz$http://example.com/foo/bar//baz.htmlrUrfzsomeproxy:3128) r+rrr~rrrWrrY set_proxy)rr<rZds_urlsZds_urlZds_reqZ np_ds_reqZp_ds_reqr%r%r&test_http_doubleslashs     z"HandlerTests.test_http_doubleslashcCshgd}td}|D]N}||_t|}|||||jpBd|j|t|j|jqdS)N)zhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashrJrJr)rfull_urlr rrfragmentquery)rrJrrOparsedr%r%r&test_full_url_setter*sz!HandlerTests.test_full_url_settercCs6td}|`||j||j||jddS)Nhttp://www.example.comr)rrrXrrr)rrr%r%r&test_full_url_deleterAs   z"HandlerTests.test_full_url_deletercCsztj}t|_d}t|}||}||jd||j dd}t|}||}||jd||j ddS)Nzhttp://www.python.org?getspamwww.python.orgz /?getspamhttp://www.python.orgr) r+rrr~rrrWrrr)rr<Z weird_urlr[r[Zurl_without_pathr%r%r&test_fixpath_in_weirdurlsHs   z&HandlerTests.test_fixpath_in_weirdurlscCstj}t}|_d}t|}tddid|}|||}|||| t |dtddid|}|||}|||| t |dtdd id|}|||}|||| t |dtd d id|}| |||| |j d | |j||d d ifdS) Nr]rrrrZAcceptedzPartial contentiz Bad gatewayr)r+rZHTTPErrorProcessorr~rrrrrHrWrrXrrr)rr<r rOr[rnewrr%r%r& test_errors[s(        zHandlerTests.test_errorscCst}tj|}t|_td}tddid}||}| |j || |j || |j d| |j|||}| |j|| |j|| ||dS)Nr]rrrrf)rr+rHTTPCookieProcessorr~rrrrrHrrorigin_req_hostrWZ unverifiablerrr)rcjr<r[rr[rr%r%r& test_cookiests    zHandlerTests.test_cookiesc Csdd}d}tj}t}|_dD]Z}dD]N}t|d|}t||}tj|_ | dd|durz| dt t || d d z||t|d td |iWn,tjjy||d ||Yn0||j|z||jdWn"ty||jjYn0dd|jjD} |d| |d| ||jjdd|d |jj|d |jjq,q"t|}tj|_ |fdd} t|dd}d} tj|_ z| ||d| d} qWn*tjjy|| tjjjYn0t|dd}d} tj|_ z | ||d| | d} qWn*tjjy^|| tjjjYn0dS)Nhttp://example.com/a.htmlzhttp://example.com/b.html)i-ri/3)Nz blah blah z http_error_%sZNonsensezviking=withholdrbrOrlBlahlocationrrLcSsg|] }|qSr%)lower).0xr%r%r& rSz.HandlerTests.test_redirect..zcontent-lengthz content-typec Ss ||tddtd|idS)Nrrr)rrr)r<r[rOr%r%r&r s z,HandlerTests.test_redirect..redirectrf)rrr]rzhttp://example.com/%d)r+rHTTPRedirectHandlerr~rgetattrrrrrrUrDrrZrrr HTTPErrorrassertIsNotNoner[r get_methodAttributeErrorrWrrNrXrYZ max_repeatsZmax_redirections) rfrom_urlZto_urlr<r rrrr[rNr rr%r%r& test_redirectsl                zHandlerTests.test_redirectc Csd}gd}gd}d}tj}t}|_t|}tj|_|D]6}|d|} | tj j |j |t ddtd| iqB|D]>}|d|} | |t dd td| i||j| q~dS) Nr)rhttpsr>)fileimapZldapzexample.com/b.htmlz://rzSecurity Loopholer That's fine)r+rrr~rrrrrr-rrrrrrr[r) rrZ valid_schemesZinvalid_schemesZschemeless_urlr<r r[r Z invalid_url valid_urlr%r%r&test_invalid_redirects(       z"HandlerTests.test_invalid_redirectc Csnd}d}tj}t}|_t|}tj|_tj ||}| |t ddt d|i||j|dS)Nrz/b.htmlrrr)r+rrr~rrrrrrurljoinrrrrr[r)rrZ relative_urlr<r r[rr%r%r&test_relative_redirects   z#HandlerTests.test_relative_redirectc Csddlm}ddlm}|}||ddtdd}tj}tj}tj |}t ||||}| d| |j ddS) Nr) CookieJar)interact_netscapehttp://www.example.com/z spam=eggsrz%Location: http://www.cracker.com/ Cookie)Zhttp.cookiejarrZtest.test_http_cookiejarrrr+rHTTPDefaultErrorHandlerrrrrrWr[rT) rrrrhhhdehhrhZcpr r%r%r&test_cookie_redirects        z!HandlerTests.test_cookie_redirectcCsTd}tdd|}tj}tj}t|||}|d}||| dS)Nz(http://www.example.com/index.html#OK rz Location: r) rr+rrrrrrrstrip)rZredirected_urlrrrr rr%r%r&test_redirect_fragments    z#HandlerTests.test_redirect_fragmentcs|tjjtjjd}t|tj_t tjdt dfdd}|tjj_tj d} | ddS)Ns(HTTP/1.1 302 Found Location: ?query HTTPConnection)z/pathz /path?querycs8|tj|||g|Ri|d|j_dS)NsHTTP/1.1 200 OK Hello!)rnextrrZfakedata)connrrOposkwZ real_classrrJr%r&rsz3HandlerTests.test_redirect_no_path..requestzhttp://python.org/pathzhttp://python.org/path?query)r*r+rr,rrrrfakehttpriterr/rr)rZ response1rrr%rr&test_redirect_no_path s  z"HandlerTests.test_redirect_no_pathc CsGdddtjj}|}tj|}d}|D]|\}}|t|Vtd|d|_| d}d|d}|j } | | |t| Wdq.1s0Yq.dS) Nc@seZdZddZdS)z4HandlerTests.test_redirect_encoding..HandlercSs(||j|}|jj|_td|_|S)Ns)HTTP/1.1 200 OK Content-Length: 3 123)r connectionbuflast_bufrr)rr[resultr%r%r&r"s  z>HandlerTests.test_redirect_encoding..Handler.http_openN)rGrrHrr%r%r%r&Handler!sr))s /på-dansk/s/p%C3%A5-dansk/)/spaced%20path/r)s /spaced path/r)s /?på-dansks/?p%C3%A5-dansks!HTTP/1.1 302 Redirect Location: s r]sGET  ) r+r HTTPHandler build_openersubTestreprrrrrrrSr) rrrrr=rrrrrr%r%r&test_redirect_encodings"     z#HandlerTests.test_redirect_encodingcCsd}t|dt|dfD]}t}tj|}||dgg}t||}td}||j d| |||j |||ddfgd d |j DqdS) Nproxy.example.com:3128r)ZHTTP)rrzhttp://acme.example.com/zacme.example.comrrcSsg|]}|ddqSrrr%rtupr%r%r&rQrSz+HandlerTests.test_proxy..) dictrr+r ProxyHandlerrrrrrrr)rr!rcr phrrr[r%r%r& test_proxyAs    zHandlerTests.test_proxycCsdtjd<t}tjtdd}||td}| |j d| || |j dtd}| |j d| || |j dtjd=dS) Nz python.orgno_proxyproxy.example.comrzhttp://www.perl.org/ www.perl.orgrr renvironrr+rrrrrrrrrr rr[r%r%r&test_proxy_no_proxySs    z HandlerTests.test_proxy_no_proxycCsfdtjd<t}tjtdd}||td}| |j d| || |j dtjd=dS)N*rrrrrrrr%r%r&test_proxy_no_proxy_allbs   z$HandlerTests.test_proxy_no_proxy_allcCst}tjtdd}||dgg}t||}td}||j d| |||j d||ddfgdd |j DdS) Nrr)rrhttps://www.example.com/www.example.comrrcSsg|]}|ddqSrr%rr%r%r&r{rSz1HandlerTests.test_proxy_https..) rr+rrrrrrrrrr)rr rrrr[r%r%r&test_proxy_httpsms   zHandlerTests.test_proxy_httpscCst}tjtdd}||t}||td}|dd|dd| |j d| |j | ||d |jj|d |jj||j | |j d| |d ddS) NrrrProxy-AuthorizationFooBar User-AgentGrailr)rr)rrProxy-authorization)rr+rrrrrrrUrrrXrrrXrrrCrrY)rr rZ https_handlerr[r%r%r&$test_proxy_https_proxy_authorization}s(       z1HandlerTests.test_proxy_https_proxy_authorizationdarwinzonly relevant for OSXcCsdgdd}dD]}|t||d|qdD]}|t||d|q2dgd}|td |dd d gd}d }|t||d|d }|t||d|dS)NF)foo.barz *.bar.com 127.0.0.1z10.10z10.0/16)Zexclude_simple exceptions)rz www.bar.comrz 10.10.0.1z10.0.0.1z expected bypass of %s to be True)z abc.foo.barzbar.comz 127.0.0.2z 10.11.0.1Z notinbypassz!expected bypass of %s to be FalseTtestz 10.0.0.0/40z172.19.10.0/24z 172.19.10.5z10.0.1.5)rSr rW)rZbypassrr%r%r&test_osx_proxy_bypasss0     z"HandlerTests.test_osx_proxy_bypassc Cs|j||dlt}t}tj|}d|d}td|}||||| ||d|||ddWdn1s0YdS)N)rrNz rr!http://acme.example.com/protected) rrrr+rrjoinrr_test_basic_auth)rrNrrpassword_manager auth_handlerr http_handlerr%r%r&check_basic_auths     zHandlerTests.check_basic_authc Cs.d}d}d|d}d|d}d}d|d}d|dd|dd|d |d ||d ||d ||d ||d |fD]}d |g}|||qtd tf(d |g}|||Wdn1s0Y||g||g||gfD] } dd| D}|||qdS)Nzrealm2@example.comz Basic realm=""zOtherscheme xxxzDigest realm="zn", qop="auth, auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"z Basic realm=''z", charset="UTF-8"z, WWW-Authenticate: zBasic Auth Realm was unquotedzWWW-Authenticate: Basic realm=cSsg|]}d|qS)rr%)rZ challenger%r%r&rsz0HandlerTests.test_basic_auth..)rrZcheck_warnings UserWarning) rrZrealm2basicZbasic2Zother_no_realmdigestZ realm_strrNZ challengesr%r%r&test_basic_auths<             *zHandlerTests.test_basic_authc Csvt}tjtdd}||t}tj|}d}tdd|}||||| ||d|||dddS)Nrr ACME Networksiz(Proxy-Authenticate: Basic realm="%s" rz&http://acme.example.com:3128/protected) rr+rrrrrZProxyBasicAuthHandlerrr)rrrrrrrr%r%r&test_proxy_basic_auths      z"HandlerTests.test_proxy_basic_authc CsGdddt}Gdddtjj}Gdddtjj}|}t}||}||}d}tdd |} |||||| |||d || |d d | |j d d gddS)Nc@seZdZddZddZdS)zQHandlerTests.test_basic_and_digest_auth_handlers..RecordingOpenerDirectorcSst|g|_dSr)rrrecordedrr%r%r&r s zZHandlerTests.test_basic_and_digest_auth_handlers..RecordingOpenerDirector.__init__cSs|j|dSr)rr)rrr%r%r&recordszXHandlerTests.test_basic_and_digest_auth_handlers..RecordingOpenerDirector.recordN)rGrrHrrr%r%r%r&RecordingOpenerDirector src@seZdZddZdS)zOHandlerTests.test_basic_and_digest_auth_handlers..TestDigestAuthHandlerc_s,|jdtjjj|g|Ri|dS)Nr)rrr+rHTTPDigestAuthHandlerhttp_error_401rrrr%r%r&rs   z^HandlerTests.test_basic_and_digest_auth_handlers..TestDigestAuthHandler.http_error_401NrGrrHrr%r%r%r&TestDigestAuthHandlersrc@seZdZddZdS)zNHandlerTests.test_basic_and_digest_auth_handlers..TestBasicAuthHandlerc_s,|jdtjjj|g|Ri|dS)Nr)rrr+rrrrr%r%r&rs   z]HandlerTests.test_basic_and_digest_auth_handlers..TestBasicAuthHandler.http_error_401Nrr%r%r%r&TestBasicAuthHandlersrrr&WWW-Authenticate: Basic realm="%s" rrrrr) rr+rrrrrrrrr) rrrrrrZdigest_handlerZ basic_handlerrrr%r%r&#test_basic_and_digest_auth_handlerss(     z0HandlerTests.test_basic_and_digest_auth_handlerscCsDt}tjd}tdd}|||||t|jddS)NrzWWW-Authenticate: Kerberos r) rr+rrrrr-r.r)rrZdigest_auth_handlerrr%r%r&$test_unsupported_auth_digest_handler4s   z1HandlerTests.test_unsupported_auth_digest_handlercCsDt}tjd}tdd}|||||t|jddS)NrzWWW-Authenticate: NTLM r) rr+rrrrr-r.r)rrZbasic_auth_handlerrr%r%r&#test_unsupported_auth_basic_handler>s   z0HandlerTests.test_unsupported_auth_basic_handlerc CsHddl} d\} } |||| | |||j|||j|| |j|| |j||||j|||j ||t |j d| |j d |td| | fd} d| | } ||j d|| ||j dj|| d|_|_||||t |j d| |j d |dS)NrZwileZcoyoterz%s:%sasciizBasic r)base64rprrrOrr_rrrrrrWrTbytesZ encodebytesrdecoderYrYr)rrrZ auth_headerrrr request_urlZ protected_urlrrr_ZuserpassZauth_hdr_valuer%r%r&rHs6   zHandlerTests._test_basic_authc Cs~d\}}d}td}t}t|}|jd|||dd||}||t}||||||||j dS)Nr rrT)is_authenticated) rrrrprrSrrrr) rrr_rr pwd_managerauth_prior_handleris_authrr%r%r&test_basic_prior_auth_auto_sendms      z,HandlerTests.test_basic_prior_auth_auto_sendc Csd\}}d}d}t}t|}|||||||}||t}||tddd} || ||||}| |t d} || j t}|||| ||| | j dS)Nr rZACMErrr) rrrprrWrrrrrSrr) rrr_rrrrrrrr%r%r&.test_basic_prior_auth_send_after_first_successs0           z;HandlerTests.test_basic_prior_auth_send_after_first_successc CsdD]\}}d|}t||}tj}td}d|_| ||}| Wdn1sj0Y| |j j d|qdS)z=Test the connection is cleaned up when the response is closed))zConnection: closedata)zTransfer-Encoding: chunkeds4 data 0 )zContent-Length: 4rzHTTP/1.1 200 OK {} http://dummy/NzConnection not closed with {!r})formatrrrr+rrrrrr5rSfakesockclosed)rZtransferrheaderrrr[Zrespr%r%r&test_http_closeds   & zHandlerTests.test_http_closedcCsptd}tj}td}d|_|tj j | ||Wdn1sR0Y| |j jddS)z;Test the connection is cleaned up after an invalid responserSrNzConnection not closed)rrr+rrrrr-rrZ BadStatusLinerrSrr)rrrr[r%r%r&test_invalid_closeds  *z HandlerTests.test_invalid_closedN)/rGrrHr<rKr\rirkrurxryr~rrrrrrrrrrrrrrrrrrrr! skipUnlessrnplatformrrrrr r r rrrrrr%r%r%r&r$sR@\B J  #  !-2  %(r$c@sNeZdZddZddZeeddddZ d d Z d d Z d dZ dS) MiscTestscs"|tfdd|jDdS)Nc3s|]}|jkVqdSr)r)rr< handler_classr%r& sz/MiscTests.opener_has_handler..)rSanyr)rrr$r%r#r&opener_has_handlerszMiscTests.opener_has_handlercCs(Gdddtjj}Gdddtjj}Gdddtjj}tjj}|||}|||||||||}||||||||}||||}||tjj|tjj}||tjj|tj}||tjjGdddtjj}|||}||||||dS) Nc@s eZdZdS)z2MiscTests.test_build_opener..MyHTTPHandlerNrr%r%r%r& MyHTTPHandlersr(c@seZdZddZdS)z/MiscTests.test_build_opener..FooHandlercSsdSrr%rr%r%r&foo_opensz8MiscTests.test_build_opener..FooHandler.foo_openN)rGrrHr)r%r%r%r& FooHandlersr*c@seZdZddZdS)z/MiscTests.test_build_opener..BarHandlercSsdSrr%rr%r%r&bar_opensz8MiscTests.test_build_opener..BarHandler.bar_openN)rGrrHr+r%r%r%r& BarHandlersr,c@s eZdZdS)z7MiscTests.test_build_opener..MyOtherHTTPHandlerNrr%r%r%r&MyOtherHTTPHandlersr-)r+rr BaseHandlerrr')rr(r*r,rr r-r%r%r&test_build_openers,          zMiscTests.test_build_openerZnetworkztest requires network accesscCstdtj}tjd}|d|j||d d|d|j|d| d||d d|d|j|d| dWdn1s0YdS) Nrrmzus-ascii1r7Z 1234567890s 1234567890ru) rZtransient_internetr+rrrrrrrrY)rrrr%r%r&test_issue16464s   zMiscTests.test_issue16464cCsd}d}}}d}tj|||||}|t|d||jd|t|d||jdd|j|j f}|t ||d|j|j f}|t ||dS)z Issue 13211 reveals that HTTPError didn't implement the URLError interface even though HTTPError is a subclass of URLError. zsomething bad happenedNzContent-Length: 42rArNzHTTP Error %s: %sz) r+rrrSrrrArNrrrDr)rrrOrrrZrEZexpected_errmsgr%r%r&test_HTTPError_interface s z"MiscTests.test_HTTPError_interfacecCs:gd}|D]\}}|t||q |ttdfdS)N)r)NNNrr)NNNrr3r4)zjoe:password@proxy.example.com)Nr^r_r)z#joe:password@proxy.example.com:3128)Nr^r_r)zhttp://proxy.example.com/)rNNr)zhttp://proxy.example.com:3128/)rNNr)z&http://joe:password@proxy.example.com/rr^r_r)z*http://joe:password@proxy.example.com:3128)rr^r_r)z1ftp://joe:password@proxy.example.com/rubbish:3128)r>r^r_r)z%http://joe:password@proxy.example.comr5)z&http://user/name:password@localhost:22)r user/namer_ localhost:22)z&http://username:pass/word@localhost:22)rusername pass/wordr7)z'http://user/name:pass/word@localhost:22)rr6r9r7zfile:/ftp.example.com)rr r-r.)rZparse_proxy_test_casesZtcrr%r%r&test_parse_proxys' zMiscTests.test_parse_proxycCsPt}|t}|dWdn1s00Y|t|jddS)Ninvalidz5Unsupported digest authentication algorithm 'invalid')r r-r.Zget_algorithm_implsrrD exception)rrexcr%r%r&test_unsupported_algorithmLs (z$MiscTests.test_unsupported_algorithmN) rGrrHr'r/r!r rZis_resource_enabledr1r2r:r>r%r%r%r&r"s,  -r"c@seZdZGdddeZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZddZddZddZddZdS) RequestTestsc@seZdZdZdS)zRequestTests.PutRequestPUTN)rGrrHrr%r%r%r& PutRequestWsrAcCsLtd|_tddddid|_tddd|_|d|_|jddd|_dS) Nhttp://www.python.org/~jeremy/rzX-TestrrMHEAD)rrM)rr:postheadrAput force_postrr%r%r&setUpZs  zRequestTests.setUpcCs^|d|j|d|j|d|j|d|j|d|jdS)NrMrLrCr@)rrDrr:rErFrGrr%r%r& test_methodds zRequestTests.test_methodcCsL||jj|d|jd|j_||jj|d|jdS)NrLrlrM)rWr:rrrrSrr%r%r& test_dataks zRequestTests.test_datacCsN|d|jj|jdd|d|jjdd|j_|d|jjdS)Nr7*rl)rXr:rYrZrrrr%r%r&.test_setting_data_should_remove_content_lengthus z;RequestTests.test_setting_data_should_remove_content_lengthcCsT|d|jjd|j_|jdd|d|jjd|j`|d|jjdS)Nr7rgrz)rXr:rYrrZrrr%r%r&/test_deleting_data_should_remove_content_length}s zrrRrr%r%r&test_wrapped_urlszRequestTests.test_wrapped_urlcCsLtd}|d|jtd}|d|jd}t|}|||dS)N-http://www.python.org/?qs=query#fragment=truez /?qs=queryzhttp://www.python.org/#fun=truer(.http://docs.python.org/library/urllib2.html#OK)rrrr)rr[rOr%r%r&test_url_fragmentszRequestTests.test_url_fragmentcCs0gd}|D]}t|}|||jq dS)N)zhttp://docs.python.orgrVrU)rrrr)rrJrOr[r%r%r&test_url_fullurl_get_full_urlsz*RequestTests.test_url_fullurl_get_full_urlN)rGrrHrrArHrIrJrLrMrNrOrPrQrSrrTrWrXr%r%r%r&r?Vs   r?__main__):r!rrZ test.supportrrrrrr}rnrdrpZurllib.requestr+rrrrr r r urllib.parser r Z http.clientrZTestCaser rIr~rrrrrrrIOBaserrrrrrr.rrrrrrr#r$r"r?rGmainr%r%r%r&sd   $ 4,    -6"    a