a ze@s:dZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddlZz ddlZWney~dZYn0ddlZddlZddlmZmZddlmZddlZddZdad5d d Zd d Zd6ddZGdddeZ GdddeZ!Gddde j"Z#Gddde j"Z$Gddde j"Z%Gddde j"e e!Z&Gddde j"Z'Gddde j"Z(Gd d!d!e j"e Z)Gd"d#d#e j"Z*Gd$d%d%e j"Z+Gd&d'd'e j"Z,Gd(d)d)e j"Z-Gd*d+d+e j"Z.Gd,d-d-e e j"Z/Gd.d/d/e j"Z0Gd0d1d1e j"Z1Gd2d3d3e j"Z2e3d4kr6e 4dS)7z;Regression tests for what was in Python 2's "urllib" moduleN)patch)support) url2pathname pathname2url) b64encodecCs4tt|dd}t|dkr,d|}d|S)z!Escape char as RFC 2396 specifiesNz0%s%)hexordupperlen)charZhex_reprrD/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_urllib.py hexescapes rcCsN|durtjj|d}nts(t}|ant}|dur>||S|||SdS)z.urlopen(url [, data]) -> open file-like objectN)proxies)urllibrequestFancyURLopener _urlopeneropen)urldataropenerrrrurlopen$s rcCs<tdtftjWdS1s.0YdS)Nz8FancyURLopener style of invoking requests is deprecated.)rcheck_warningsDeprecationWarningrrrrrrrr4s rFcs:GfdddtjGfdddtjj|_S)Ncs@eZdZdZfddZddZd ddZdd d Zd d ZdS)zfakehttp..FakeSocketrcs |_dSN)bufselfrFakeHTTPConnectionrrsendall?sz$fakehttp..FakeSocket.sendallc_s|jd7_|S)Nr)io_refs)r!argskwdsrrrmakefileBsz%fakehttp..FakeSocket.makefileNcSs|jr dStj||SN)closedioBytesIOread)r!Zamtrrrr.Fsz!fakehttp..FakeSocket.readcSs|jr dStj||Sr))r+r,r-readline)r!lengthrrrr/Ksz%fakehttp..FakeSocket.readlinecSs(|jd8_|jdkr$tj|dS)Nrr)r%r,r-closer!rrrr1Ps z"fakehttp..FakeSocket.close)N)N) __name__ __module__ __qualname__r%r$r(r.r/r1rr"rr FakeSocket<s    r6cs(eZdZdZfddZr$ddZdS)z$fakehttp..FakeHTTPConnectionNcs|j|_|jt|_dSr)fakedatasocktypeZfakesockr2)r6rrconnectZs z,fakehttp..FakeHTTPConnection.connectcSsdSrrr2rrrr1csz*fakehttp..FakeHTTPConnection.close)r3r4r5rr:r1r)r6 mock_closerrr#Us r#)r,r-httpclientHTTPConnectionr7)r7r;r)r#r6r;rfakehttp;sr?c@seZdZdddZddZdS) FakeHTTPMixinFcCs"t||d}tjj|_|tj_dS)Nr;)r?r<r=r>_connection_class)r!r7r;Zfake_http_classrrrr?ks  zFakeHTTPMixin.fakehttpcCs|jtj_dSr)rBr<r=r>r2rrr unfakehttppszFakeHTTPMixin.unfakehttpN)F)r3r4r5r?rCrrrrr@js r@c@seZdZddZddZdS) FakeFTPMixincCs&Gdddt}tjj|_|tj_dS)Nc@s&eZdZd ddZddZddZdS) z,FakeFTPMixin.fakeftp..FakeFtpWrapperNTcSsdSrr)r!userpasswdhostportdirstimeoutZ persistentrrr__init__wsz5FakeFTPMixin.fakeftp..FakeFtpWrapper.__init__cSs tdfS)Nr)r,r-)r!filer9rrrretrfile{sz5FakeFTPMixin.fakeftp..FakeFtpWrapper.retrfilecSsdSrrr2rrrr1~sz2FakeFTPMixin.fakeftp..FakeFtpWrapper.close)NT)r3r4r5rKrMr1rrrrFakeFtpWrappervs  rN)objectrr ftpwrapper_ftpwrapper_class)r!rNrrrfakeftpus zFakeFTPMixin.fakeftpcCs|jtj_dSr)rQrrrPr2rrr unfakeftpszFakeFTPMixin.unfakeftpN)r3r4r5rRrSrrrrrDtsrDc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"S)#urlopen_FileTestszTest urlopen() opening a temporary file. Try to test as much functionality as possible so as to cut down on reliance on connecting to the Net for testing. cCsptd|jjd|_ttjd}z||jW|n |0tj|_ t j |j |_ td|j |_dS)Nztest_urllib: %s asciiwbfile:%s)bytes __class__r3textrrTESTFNwriter1pathnamerparsequotequoted_pathnamer returned_obj)r!frrrsetUps  zurlopen_FileTests.setUpcCs|jttjdS)zShut down the open objectN)rar1osremoverr[r2rrrtearDowns zurlopen_FileTests.tearDowncCs&dD]}|t|j|d|qdS)N) r.r/ readlinesfilenor1infogeturlgetcode__iter__/object returned by urlopen() lacks %s attribute) assertTruehasattrrar!attrrrrtest_interfaces z urlopen_FileTests.test_interfacecCs||j|jdSr) assertEqualrZrar.r2rrr test_readszurlopen_FileTests.test_readcCs,||j|j|d|jddS)Nr*zKcalling readline() after exhausting the file did not return an empty string)rsrZrar/r2rrr test_readlineszurlopen_FileTests.test_readlinecCs4|j}|t|dd||d|jddS)Nrz.readlines() returned the wrong number of linesrz"readlines() returned improper text)rargrsr rZ)r!Z lines_listrrrtest_readliness  z urlopen_FileTests.test_readlinescCs:|j}||td|t|t|j|jddS)Nzfileno() did not return an intzTReading on the file descriptor returned by fileno() did not return the expected text) rarhassertIsInstanceintrsrdr.r rZ)r!Zfile_numrrr test_filenos  zurlopen_FileTests.test_filenocCs|jdSr)rar1r2rrr test_closeszurlopen_FileTests.test_closecCs||jjtjjdSr)rwraZheadersemailmessageMessager2rrr test_headersszurlopen_FileTests.test_headerscCs||jj|jdSr)rsrarr`r2rrrtest_urlszurlopen_FileTests.test_urlcCs||jjdSr) assertIsNonerastatusr2rrr test_statusszurlopen_FileTests.test_statuscCs||jtjjdSr)rwrarir{r|r}r2rrr test_infoszurlopen_FileTests.test_infocCs||j|jdSr)rsrarjr`r2rrr test_geturlszurlopen_FileTests.test_geturlcCs||jdSr)rrarkr2rrr test_getcodeszurlopen_FileTests.test_getcodecCs|jD]}|||jqdSr)rarsrZ)r!linerrr test_iters zurlopen_FileTests.test_itercCs|ttjjd|jdS)Nz./) assertRaises ValueErrorrrrr]r2rrrtest_relativelocalfilesz(urlopen_FileTests.test_relativelocalfileN)r3r4r5__doc__rcrfrrrtrurvryrzr~rrrrrrrrrrrrTs"  rTc@sDeZdZddZddZddZddZd d Zd d Zd dZ dS) ProxyTestscCs6t|_ttjD]}d|vr|j|qdS)Nproxy)rZEnvironmentVarGuardenvlistrdenvironlowerunset)r!krrrrcs  zProxyTests.setUpcCs|j|`dSr)r__exit__r2rrrrfs zProxyTests.tearDowncCsp|jddtj}|d|d|jdd|tjd|tjd|tjddS)NNO_PROXY localhostno0localhost, anotherdomain.com, newdomain.com:1234anotherdomain.comanotherdomain.com:8888newdomain.com:1234)rsetrrgetproxies_environmentrsrnproxy_bypass_environmentr!rrrr+test_getproxies_environment_keep_no_proxiess z6ProxyTests.test_getproxies_environment_keep_no_proxiesc Cszh|jddtj}|d|d|jddtj}|d|W|jd|jdn|jd|jd0dS)N HTTP_PROXYhttp://somewhere:3128r<ZREQUEST_METHODGET)rrrrrrs assertNotInrrrrrtest_proxy_cgi_ignores    z ProxyTests.test_proxy_cgi_ignorecCstjj}|jdd||d||d||d||d||d||d||d ||d ||d ||d ||d ||d||d||ddS)Nrz8localhost, anotherdomain.com, newdomain.com:1234, .d.o.trZ LocalHostZ LOCALHOSTz .localhostrz.newdomain.com:1234z foo.d.o.tzd.o.trz.anotherdomain.com:8888zwww.newdomain.com:1234Z prelocalhost newdomain.comznewdomain.com:1235rrrrrrn assertFalser!Zbypassrrr(test_proxy_bypass_environment_host_match s$z3ProxyTests.test_proxy_bypass_environment_host_matchcCsntjj}|jdd||d||d|jdd||d||d||ddS)Nr*rrz*, anotherdomain.comrrrrrr*test_proxy_bypass_environment_always_matchsz5ProxyTests.test_proxy_bypass_environment_always_matchcCsDtjj}|jdd||d||d||ddS)Nrrz localhost zanotherdomain.com:8888 znewdomain.com:1234 )rrrrrrrrrr%test_proxy_bypass_environment_newline(sz0ProxyTests.test_proxy_bypass_environment_newlineN) r3r4r5rcrfrrrrrrrrrrs   rc@s$eZdZddZddZddZdS)ProxyTests_withOrderedEnvcCstj|_tt_dSr)rdr _saved_env collections OrderedDictr2rrrrc3szProxyTests_withOrderedEnv.setUpcCs |jt_dSr)rrdrr2rrrrf9sz"ProxyTests_withOrderedEnv.tearDowncCsdtjd<dtjd<|tjd|tjddtjd<dtjd<tj}|i|d tjd<d tjd<|tjd|tjd |tjd |tjd |tjddtjd<dtjd<tj}|d|ddS)NZno_proxyrZNo_ProxyZ arbitraryZ http_proxyrrz%localhost, noproxy.com, my.proxy:1234zxyz.comznoproxy.com:5678z my.proxy:1234zmy.proxyzhttp://somewhereelse:3128Z Http_Proxyr<) rdrrrrrrrsrnrrrr,test_getproxies_environment_prefer_lowercase<s&           zFProxyTests_withOrderedEnv.test_getproxies_environment_prefer_lowercaseN)r3r4r5rcrfrrrrrr1src@seZdZdZddZddZddZee dd d Z ee dd d Z ee dd dZ ee dddZ ddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zeejd'd(d)d*Zd+d,Zd-d.Zd/d0Z ee dd1d2Z!d3S)4urlopen_HttpTestsz.Test urlopen() opening a fake http connection.cCsv|d|dzTtd}||d||d||d||dW|n |0dS)NsHTTP/s 200 OK Hello!http://python.org/Hello!r*r?rrsr/rjrkrC)r!verfprrr check_readXszurlopen_HttpTests.check_readcCsFd}|dz(tj|}|||W|n |0dS)Nz-http://docs.python.org/library/urllib.html#OKHTTP/1.1 200 OK Hello!)r?rrrrsrjrC)r!rrrrrtest_url_fragmentcs   z#urlopen_HttpTests.test_url_fragmentcCs<|dz"td}||jjW|n |0dS)Nrhttp://www.python.org)r?rrnrZ will_closerC)r!resprrrtest_willclosems  z urlopen_HttpTests.test_willclosezssl module requiredc CsttdddgD]}t|}d|d}|dzt|dd}tjj}| |d |"t j d |Wdn1s0Y| |d |"t j d |Wdn1s0Yt d |}| ||W|q|0qdS) Nr!z//localhost:7777/test/HTTP/1.1 200 OK Hello.\\\contain control.*http:https:)rrangechrr?reprreplacer<r= InvalidURLassertRaisesRegexrrrrrjrC)r!char_norschemeless_urlescaped_char_reprrrrrr(test_url_path_with_control_char_rejectedus$   0 0z:urlopen_HttpTests.test_url_path_with_control_char_rejectedcCs|dd}d|d}ztjj}||d"tjd|Wdn1sX0Y||d"tjd|Wdn1s0Ytd|}|d | |d | |d | W| n | 0dS) Nrz:localhost:7777?a=1 HTTP/1.1 X-injected: header TEST: 123//:8080/test/?test=az*contain control.*\\r.*(found at least . .)rcontain control.*\\nr   ) r?r<r=rrrrrrrjrC)r!rGrrrrrr4test_url_path_with_newline_header_injection_rejecteds   00zFurlopen_HttpTests.test_url_path_with_newline_header_injection_rejectedc CsttdddgD]}t|}d|d}|dzt|dd}tjj}| |d |t d |Wdn1s0Y| |d |t d |Wdn1s0YW| q| 0qdS) Nrrrz //localhostz/test/rrrrrr) rrrr?rrr<r=rrrrC)r!rrrrrrrr(test_url_host_with_control_char_rejecteds   ,.z:urlopen_HttpTests.test_url_host_with_control_char_rejectedcCs|dd}d|d}ztjj}||dtd|Wdn1sT0Y||dtd|Wdn1s0YW|n |0dS) Nrzlocalhost X-injected: header rrzcontain control.*\\rrrr)r?r<r=rrrrC)r!rGrrrrr4test_url_host_with_newline_header_injection_rejecteds  ,.zFurlopen_HttpTests.test_url_host_with_newline_header_injection_rejectedcCs|ddS)Ns0.9rr2rrr test_read_0_9szurlopen_HttpTests.test_read_0_9cCs|ddS)Ns1.0rr2rrr test_read_1_0szurlopen_HttpTests.test_read_1_0cCs|ddS)Ns1.1rr2rrr test_read_1_1szurlopen_HttpTests.test_read_1_1cCs8|jdddz|ttdW|n |0dS)NsHTTP/1.1 401 Authentication Required Date: Wed, 02 Jan 2008 03:03:54 GMT Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e Connection: close Content-Type: text/html; charset=iso-8859-1 TrArr?rOSErrorrrCr2rrrtest_read_boguss z!urlopen_HttpTests.test_read_boguscCsf|jdddzHd}|tjj|tdWdn1sB0YW|n |0dS)NsHTTP/1.1 302 Found Date: Wed, 02 Jan 2008 03:03:54 GMT Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e Location: file://guidocomputer.athome.com:/python/license Connection: close Content-Type: text/html; charset=iso-8859-1 TrAzRedirection to url 'file:r)r?rrerror HTTPErrorrrC)r!msgrrrtest_invalid_redirects(z'urlopen_HttpTests.test_invalid_redirectc CsNttjD]<}|jdddz|tjjtdW| q | 0q dS)Ns_HTTP/1.1 302 Found Location: file://guidocomputer.athome.com:/python/license Connection: close TrAhttp://something) rrZmaxtriesr?rrrrrrC)r!irrrtest_redirect_limit_independents z1urlopen_HttpTests.test_redirect_limit_independentcCs4|dz|ttdW|n |0dS)Nr*rrr2rrrtest_empty_sockets z#urlopen_HttpTests.test_empty_socketcCsV|tjj}tdWdn1s,0Y||jj||jjdS)Nz/file://localhost/a/file/which/doesnot/exists.py) rrrURLErrorrrn exceptionfilenamereasonr!errrtest_missing_localfile s&z(urlopen_HttpTests.test_missing_localfilec Cst\}}d|tjjd}z\|tj|t|}||Wdn1s\0YWt |t |nt |t |0| tj|| t jjt|Wdn1s0YdS)Nzfile://localhost/r)tempfilemkstemprrdpathseprnexistsrr1unlinkrrrrr)r!fdZtmp_fileZ tmp_fileurlZfobjrrrtest_file_notexistss  *   z%urlopen_HttpTests.test_file_notexistscCsZd}|tjj}t|Wdn1s00Y||jj||jj dS)Nz ftp:///path rrrrrrrrrnr)r!Z test_ftp_urlrrrrtest_ftp_nohosts &z!urlopen_HttpTests.test_ftp_nohostcCsV|tjj}tdWdn1s,0Y||jj||jj dS)Nz.ftp://localhost/a/file/which/doesnot/exists.pyrrrrrtest_ftp_nonexisting%s&z&urlopen_HttpTests.test_ftp_nonexistingZ MAXFTPCACHErcCsH|z0tjddddgtjjd<tdW|n |0dS)NrEpassrtestzftp://localhost)rRrrrPZftpcacherrSr2rrrtest_ftp_cache_pruning+s  z(urlopen_HttpTests.test_ftp_cache_pruningcCsn|dzTtd}||d||d||d||dW|n |0dS)NHTTP/1.0 200 OK Hello!zhttp://user:pass@python.org/rr*rr)r!rrrrtest_userpass_inurl4s z%urlopen_HttpTests.test_userpass_inurlcCs|dzd}d|}tjj}dt|dd}t|}| ||j d| | d| | d| ||| |d W|n |0dS) Nrza b:c dzhttp://{}@python.org/zAuthorization: Basic %s ASCIIzUTF-8rr*r)r?formatr<r=r>rencodedecoderassertInrrsr/ZassertNotEqualrjrkrC)r!ZuserpassrZfakehttp_wrapperZ authorizationrrrrtest_userpass_inurl_w_spaces?s  z.urlopen_HttpTests.test_userpass_inurl_w_spacescCs<tdtftjWdn1s.0YdSNr)rrrrr URLopenerr2rrrtest_URLopener_deprecationRsz,urlopen_HttpTests.test_URLopener_deprecationc Csvt}tdtfL|t"tjj dd|dWdn1sJ0YWdn1sh0YdS)Nrzhttps://localhostz/nonexistent/path)Zcafilecontext) sslZcreate_default_contextrrrrrrrr)r!r rrrtest_cafile_and_contextVs  z)urlopen_HttpTests.test_cafile_and_contextN)"r3r4r5rrrrunittest skipUnlessrrrrrrrrrrrrrrrrrrOrrrrr r rrrrrrUs<               rc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)urlopen_DataTestsz"Test urlopen() opening a data URL.cCs`|tjjd|_d|_d|_d|_d|_tj |j|_ tj |j|_ tj |j|_ dS)Nutest data URLs :;,%=& ö Ä sUPNG  IHDR{@sRGBIDATc```? o/IENDB`zZdata:text/plain;charset=UTF-8,test%20data%20URLs%20%3A%3B%2C%25%3D%26%20%C3%B6%20%C3%84%20zPdata:text/plain;charset=ISO-8859-1;base64,dGVzdCBkYXRhIFVSTHMgOjssJT0mIPYgxCA%3Dzdata:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7 QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 vHgAAAABJRU5ErkJggg%3D%3D%0A%20) addCleanuprr urlcleanuprZimagetext_urltext_url_base64 image_urlr text_url_resptext_url_base64_respimage_url_respr2rrrrccszurlopen_DataTests.setUpcCs&dD]}|t|j|d|qdS)N)r.r/rgr1rirjrkrlrm)rnrorrprrrrrs z urlopen_DataTests.test_interfacecCst||jtjj||jddg||j dt t |j |t jdddgdS)N)z text/plainr)charsetz ISO-8859-1zcontent-lengthzdata:,)rzUS-ASCII)rwrrir{r|r}rsr get_paramsrstrr rrrrr2rrrrs zurlopen_DataTests.test_infocCs@||j|j||j|j||j|jdSr)rsrrjrrrrrr2rrrrs  zurlopen_DataTests.test_geturlcCs0||jt|jd|jdSNr)rsrr.rdictrirrZr2rrrtest_read_texts z urlopen_DataTests.test_read_textcCs0||jt|jd|jdSr)rsrr.rr rirrZr2rrrtest_read_text_base64s z'urlopen_DataTests.test_read_text_base64cCs||j|jdSr)rsrr.rr2rrrtest_read_imagesz!urlopen_DataTests.test_read_imagecCs|ttjjddS)Nzdata:text/plainrrrrrr2rrrtest_missing_commasz$urlopen_DataTests.test_missing_commacCs|ttjjddS)Nzdata:;base64,Cg=r$r2rrrtest_invalid_base64_datasz*urlopen_DataTests.test_invalid_base64_dataN) r3r4r5rrcrrrrr!r"r#r%r&rrrrr`s rc@sjeZdZdZddZddZddZdd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZdS)urlretrieve_FileTestsz(Test urllib.urlretrieve() on local filesc Cs|tjjg|_|tjd|_z>t tjd}| |j| Wz | WqYq0nz | Wn Yn00dS)Nstesting urllib.urlretrieverV) rrrr tempFilesregisterFileForCleanUprr[rZrr\r1)r!FILErrrrcs     zurlretrieve_FileTests.setUpcCs,|jD] }zt|WqYq0qdSr)r(rdre)r!Zeachrrrrfs zurlretrieve_FileTests.tearDowncCsHtj|}z|dWnty6tdYn0dtj |S)Nzutf-8z!filePath is not encodable to utf8z file://%s) rdrabspathrUnicodeEncodeErrorrZSkipTestrrr)r!ZfilePathrrrconstructLocalFileUrls   z+urlretrieve_FileTests.constructLocalFileUrlr*c Cstt\}}zF||t|d}|||Wz |WqpYqp0nz |Wn Yn00|S)zCreates a new temporary file containing the specified data, registers the file for deletion during the test fixture tear down, and returns the absolute path of the file.rV)rrr)rdfdopenr\r1)r!rZnewFdZ newFilePathZnewFilerrrcreateNewTempFiles     z'urlretrieve_FileTests.createNewTempFilecCs|j|dSr)r(append)r!ZfileNamerrrr)sz,urlretrieve_FileTests.registerFileForCleanUpcCs>tjdtj}||dtj||dtjj ddS)NrWrrzFdid not get an email.message.Message instance as second returned value) rr urlretrieverr[rsrwr{r|r})r!resultrrr test_basics z urlretrieve_FileTests.test_basicc Csdtj}||tj|tj|}|||d|t j |dt |d}z.| }|Wz |WqYq0nz |Wn Yn00||j|dS)N%s.2rzcopy of the file was not maderb)rr[r)rrr1r-rsrnrdrrrr.r1rZ)r! second_tempr2r*rZrrr test_copys$     zurlretrieve_FileTests.test_copycsBdgffdd }dtj}|tjtj||dS)NrcsH|t|t|t||d|dd|d<dS)Nrr)rwrxrs) block_countblock_read_size file_sizeZ count_holderr2rr hooktesters    z9urlretrieve_FileTests.test_reporthook..hooktesterr4)rr[r)rrr1r-)r!r;r6rr2rtest_reporthooks   z%urlretrieve_FileTests.test_reporthookcCsXg}|fdd}|}tj||tj||t|d||ddddS)NcSs||||fdSrr0r8r9r:Z_reportrrrr;szAurlretrieve_FileTests.test_reporthook_0_bytes..hooktesterrrr r/rrr1r-rr[rsr r!reportr;Z srcFileNamerrrtest_reporthook_0_bytess z-urlretrieve_FileTests.test_reporthook_0_bytescCsng}|fdd}|d}tj||tj||t|d||ddd||ddddS)NcSs||||fdSrr=r>rrrr;"szAurlretrieve_FileTests.test_reporthook_5_bytes..hooktestersxxxxxrrrr?r@rrrtest_reporthook_5_bytess  z-urlretrieve_FileTests.test_reporthook_5_bytescCsg}|fdd}|dd}tj||tj||t|d||ddd||ddd ||ddd ||ddd dS) NcSs||||fdSrr=r>rrrr;0szDurlretrieve_FileTests.test_reporthook_8193_bytes..hooktesterxi rrri r?r@rrrtest_reporthook_8193_bytes+s z0urlretrieve_FileTests.test_reporthook_8193_bytesN)r*)r3r4r5rrcrfr-r/r)r3r7r<rBrDrGrrrrr's   r'c@s eZdZdZddZddZdS)urlretrieve_HttpTestsz5Test urllib.urlretrieve() using fake http connectionsc Cs||tjj|ddd}|tjj:ztjjt j |dW| n | 0Wdn1sn0YdS)NHTTP/1.1 200 OK Date: Wed, 02 Jan 2008 03:03:54 GMT Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e Connection: close Content-Length: 100 Content-Type: text/html; charset=iso-8859-1 FF cSsdSrr)Zpar1Zpar2Zpar3rrr _reporthookLszYurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError.._reporthook)Z reporthook rrrrr?rrContentTooShortErrorr1rZ TEST_HTTP_URLrC)r!rJrrr.test_short_content_raises_ContentTooShortError?s  zDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorc Csp|tjj|d|tjj6ztjt j W| n | 0Wdn1sb0YdS)NrIrKr2rrrAtest_short_content_raises_ContentTooShortError_without_reporthookVs  zWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookN)r3r4r5rrMrNrrrrrH<srHc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS) QuotingTestsauTests for urllib.quote() and urllib.quote_plus() According to RFC 3986 (Uniform Resource Identifiers), to escape a character you write it as '%' + <2 character US-ASCII hex value>. The Python code of ``'%' + hex(ord())[2:]`` escapes a character properly. Case does not matter on the hex letters. The various character sets specified are: Reserved characters : ";/?:@&=+$," Have special meaning in URIs and must be escaped if not being used for their special meaning Data characters : letters, digits, and "-_.!~*'()" Unreserved and do not need to be escaped; can be, though, if desired Control characters : 0x00 - 0x1F, 0x7F Have no use in URIs so must be escaped space : 0x20 Must be escaped Delimiters : '<>#%"' Must be escaped Unwise : "{}|\^[]`" Must be escaped cCsVdgd}tj|}|||d||ftj|}|||d||fdS)Nr)ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789z_.-~using quote(): %r != %rusing quote_plus(): %r != %r)joinrr^r_rs quote_plus)r!Z do_not_quoter2rrrtest_never_quotes    zQuotingTests.test_never_quotecCs|tjjjdddS)Nrr)rsrr^r_ __defaults__r2rrrtest_default_safeszQuotingTests.test_default_safecCsd}tjj||d}|||d||ftjj||d}|||d||ftjj|dd}|||d||ftjjdddd }tjjddd d }|||d||ftjjddd d }tjjddd d }|||d||fdS) Nz<>saferSrTs<>uaüblatin-1ü)encodingr[r)rr^r_rsrV)r!Zquote_by_defaultr2expectrrr test_safes>  zQuotingTests.test_safec CsddtdD}|d|tdd|}|D]\}tj|}|t||d|t||ftj |}|t||d|t||fq8~d }d }tj|}|||d ||ftj |}|||d ||fdS) NcSsg|] }t|qSr)r).0numrrr r*z5QuotingTests.test_default_quoting.. z <>#%"{}|\^[]`rrz1using quote(): %s should be escaped to %s, not %sz6using quote_plus(): %s should be escapes to %s, not %szab[]cdz ab%5B%5DcdrSrT) rr0rrUrr^r_rsrrV)r!Z should_quoterr2Z partial_quoteexpectedrrrtest_default_quotings8            z!QuotingTests.test_default_quotingcCstjd}||tdd|tdftjd}||dd|d}|dtd}tj|}|||d||f|dd}tj|}|||d||fdS)NrrS+zusing quote_plus(): %r != +z a b cd e frT)rr^r_rsrrVr)r!r2givenr`rrrtest_quoting_spaces&        zQuotingTests.test_quoting_spacecCsX|tjdd|tjddd|tjdd|tjddddS)Nzalpha+beta gammazalpha%2Bbeta+gammarhzalpha+beta+gammasalpha+beta gamma+)rsrr^rVr2rrrtest_quoting_plusszQuotingTests.test_quoting_pluscCsfd}d}tj|}|||d||f|jttjj|ddtj|}|||d||fdS)Nab %A2%D8ab%FFrSr\r^z"using quote_from_bytes(): %r != %r)rr^r_rsr TypeErrorquote_from_bytesr!rir`r2rrrtest_quote_bytess   zQuotingTests.test_quote_bytescCs(d}d}tj|}|||d||ftjj|ddd}|||d||fd}d}tjj|dd}|||d||fd}d }tj|}|||d||fd}|jttjj|ddd}d }tjj|dd d}|||d||fd}d }tjj|dd d}|||d||fdS)Nu¢Øabÿz%C2%A2%C3%98ab%C3%BFrSr^errorsrnr\ro漢字%E6%BC%A2%E5%AD%97z%3F%3Frz%26%2328450%3B%26%2323383%3Bxmlcharrefreplace)rr^r_rsrr,rrrrrtest_quote_with_unicodesT          z$QuotingTests.test_quote_with_unicodecCsbd}d}tjj|dd}|||d||fd}d}tjj|ddd }|||d||fdS) Nu¢Ø ÿz %A2%D8+%FFr\rorTu ab漢字 cdz ab%3F%3F+cdrrt)rr^rVrsrrrrrtest_quote_plus_with_unicode&s   z)QuotingTests.test_quote_plus_with_unicodeN) r3r4r5rrWrYrargrjrlrsryrzrrrrrOis  *rOc@sPeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dS)UnquotingTestszwTests for unquote() and unquote_plus() See the doc string for quoting_Tests for details on quoting and such. cCsg}tdD]f}tt|}t|}tj|}|||d||ftj|}|||d||f||q d |}~tj|}|| ddd|| t t ftjjd| t t ftjjddS) Nusing unquote(): %r != %rusing unquote_plus(): %r != %rrr rz/using unquote(): not all characters escaped: %sr)rrrrr^unquoters unquote_plusr0rUcountrrpAttributeError)r!Z escape_listrcrir`r2Z escape_stringrrrtest_unquoting=s2        zUnquotingTests.test_unquotingcCs>d}|}tj|}|||d||fd}|}tj|}|||d||fd}|}tj|}|||d||fd}t|d}tj|}|||d||fd}t|d}tj|}|||d||fd}t|d}tj|}|||d||f|ttftjjd|ttftjjddS)Nz%xabr}z%xr rU"using unquote_to_bytes(): %r != %rr) rr^rrsrXunquote_to_bytesrrprrrrrrtest_unquoting_badpercentTsL               z(UnquotingTests.test_unquoting_badpercentcCs.d}d}tj|}|||d||fdS)Nz%Ab%eAsrrr^rrsrrrrrtest_unquoting_mixed_casexs z(UnquotingTests.test_unquoting_mixed_casecCsXdtd}d}tj|}|||d||ftj|}|||d||fdS)Nzab%sdcZabcdrSr~)rrr^rrsrrrrrrtest_unquoting_partss     z#UnquotingTests.test_unquoting_partscCs\d}|}tj|}|||d||f|dd}tj|}|||d||fdS)Nzare+there+spaces...r}rhrr~)rr^rrsrrrrrrrtest_unquoting_pluss     z"UnquotingTests.test_unquoting_pluscCsd}d}tj|}|||d||ftjd}d}|||d||fd}d}tj|}|||d||fd}d}tj|}|||d||fdS) N"br%C3%BCckner_sapporo_20050930.docsbrückner_sapporo_20050930.docr 漢%C3%BCs漢üs %A2%D8ab%FFrms %A2ab%FFrrrrrrtest_unquote_to_bytess>    z$UnquotingTests.test_unquote_to_bytescCsxd}d}tj|}|||d||ftjj|ddd}|||d||ftjjddd}d}|||d||fd}d }tj|}|||d||fd }d }tj|}|||d||ftjj|d d }|||d||fd }d}tjj|dd }|||d||ftjd}d}|||d||ftjjddd}d}|||d||fdS)Nrubrückner_sapporo_20050930.docr}rtzbr%FCckner_sapporo_20050930.docr\rorwrvz%F3%B1u�r)rurignoreru漢üu漢%FCrr^rrsrrrrrtest_unquote_with_unicodesb             z(UnquotingTests.test_unquote_with_unicodecCsd}d}tj|}|||d||fd}d}tj|}|||d||fd}d}tj|}|||d||fdS)Ns blueberryjamZ blueberryjamr}sblåbærsyltetøyublåbærsyltetøysbl%c3%a5b%c3%a6rsyltet%c3%b8jublåbærsyltetøjrrrrrrtest_unquoting_with_bytes_inputs$      z.UnquotingTests.test_unquoting_with_bytes_inputN) r3r4r5rrrrrrrrrrrrrr{6s$   :r{c@speZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZdS)urlencode_TestszTests for urlencode()c Csgd}tj|}|D]}|||d|||fq||ddd||df|d}||d}||d}||o|d||f|t |dd |t |dfd S) aHelper method for testing different input types. 'given' must lead to only the pairs: * 1st, 1 * 2nd, 2 * 3rd, 3 Test cannot assume anything about order. Docs make no guarantee and have possible dictionary input. )z1st=1z2nd=2z3rd=3ztesting %s: %s not found in %s&rz#testing %s: expected 2 '&'s; got %srz1testing %s: '&' not located in proper place in %sz5testing %s: unexpected number of characters: %s != %sN) rr^ urlencoderrsrindexrnisdigitr ) r!riZ test_typeZexpect_somewherer2rfZ amp_locationZ on_amp_leftZ on_amp_rightrrrhelp_inputtype s4        zurlencode_Tests.help_inputtypecCs|ddddddS)N123)1st2nd3rdzusing dict as input typerr2rrrtest_using_mapping,sz"urlencode_Tests.test_using_mappingcCs|gdddS)N))rr)rr)rrz*using sequence of two-item tuples as inputrr2rrrtest_using_sequence1s z#urlencode_Tests.test_using_sequencecCs\ddi}dtdtdf}tj|}|||ddi}d}tj|}|||dS)Nr=z%s=%szkey namezA bunch of pluseszkey+name=A+bunch+of+pluses)rrr^rrsrrrrr test_quoting6s   zurlencode_Tests.test_quotingcCsdgdi}dtjtgd}tj|}|||tj|d}|dD]}d|}|||qR||ddd|ddS)NZsequence)rrrz sequence=%sTrrzExpected 2 '&'s, got %s)rr^rVrrrsrr)r!rir`r2valuerrr test_doseqAs     zurlencode_Tests.test_doseqcCs,|dtji|dtjgdSr rsrr^rr2rrrtest_empty_sequenceNsz#urlencode_Tests.test_empty_sequencecCs4|dtjddi|dtjddidS)Nza=1arza=Nonerr2rrrtest_nonstring_valuesRsz%urlencode_Tests.test_nonstring_valuescCsh|dtjdddgid|dtjdddgidtddg}|d tjd|iddS) Nza=1&a=2rrrTz a=None&a=a)rr)brza=a&a=b)rsrr^rrrr rrrtest_nonstring_seq_valuesVsz)urlencode_Tests.test_nonstring_seq_valuescCsnd}d}tjj|ddd}|||d}d}tj|}|||d}d}tjj|dd }|||dS) N) Á%3F=%3Frrrt %C2%A0=%C3%81%A0=%C1r\rorr^rrsrrrrrtest_urlencode_encoding^s   z'urlencode_Tests.test_urlencode_encodingcCsd}d}tjj|dddd}|||d}d}tjj|dddd }|||d}d }tj|d}|||d }d }tj|d}|||d}d }tjj|ddd}|||d }d}tjj|ddd}|||dS)NrrTrr)doseqr^ru))r)rrz %3F=1&%3F=%3Frtr))r)*rz%C2%A0=42&%C2%A0=%C3%81rr\roz%A0=42&%A0=%C1rrrrrrtest_urlencode_encoding_doseqqs8       z-urlencode_Tests.test_urlencode_encoding_doseqcCs`d}d}tj|}|||tj|d}|||d}d}tj|d}|||dS)N)$$z %A0%24=%C1%24T))r)rrz%A0%24=42&%A0%24=%C1%24rrrrrrtest_urlencode_bytess   z$urlencode_Tests.test_urlencode_bytescCsd}tjj|dd}d}|||d}tjj|ddd}d}|||d}d}tjj|ddd}|||d}tjj|dd d }d}|||d}d}tjj|ddd d }d}d}tjj|ddd d }|||dS) Nrz:$rZz %A0$=%C1$T)rr[))r)r rz%A0$=%C1$&%A0$=13&%A0$=42r\)r[r^)rr[r^r)r!rir2r`rrr&test_urlencode_encoding_safe_parameters:       z6urlencode_Tests.test_urlencode_encoding_safe_parameterN)r3r4r5rrrrrrrrrrrrrrrrrr s   %rc@sTeZdZdZddZddZeej dkddd Z eej dkd d d Z d S)Pathname_Testsz&Test pathname2url() and url2pathname()cCs^tjdddd}d}tj|}|||d||ftj|}|||d||fdS)NpartsZofrrzparts/of/a/pathpathname2url() failed; %s != %szurl2pathame() failed; %s != %s)rdrrUrrrrsr)r!Z expected_pathZ expected_urlr2rrrr3s  zPathname_Tests.test_basiccCstjddd}dtjd}tj|}|||d||f|}tj |}|||d||ftjdd}d tjd}tj|}|||d||fd }tjd d }tj |}|||d||fdS) NZneedszquot=ingherez needs/%s/hererzurl2pathname() failed; %s != %sz make sureZ using_quotez%s/using_quotezmake+sure/using_unquotez make+sureZ using_unquote) rdrrUrr^r_rrrsrrrrrrrs>    zPathname_Tests.test_quotingwin32z*test specific to the nturl2path functions.cCsXd}d}tj|}|||d||fd}d}tj|}|||d||fdS)Nz \\?\C:\dirz ///C:/dirrz\\?\unc\server\share\dirz/server/share/dir)rrrrsrrrrr test_prefixess   zPathname_Tests.test_prefixesz.test specific to the urllib.url2path function.cCsbd}d}|D]&}tj|}|||d||fq d}d}tj|}|||d||fdS)N)z/C:/z///C:/z/C|//C:\z/urllib.request..url2pathname() failed; %s != %sz ///C|/pathzC:\pathz.urllib.request.url2pathname() failed; %s != %s)rrrrs)r!rir`rr2rrr test_ntpath s"  zPathname_Tests.test_ntpathN) r3r4r5rr3rrrsysplatformrrrrrrrs    rc@seZdZdZddZdS) Utility_Testsz=Testcase to test the various utility functions in the urllib.cCs|tjtdS)zATest the urllib.request.thishost utility function returns a tupleN)rwrrZthishosttupler2rrr test_thishostszUtility_Tests.test_thishostN)r3r4r5rrrrrrrsrc@sTeZdZdZddZejedddZejedddZ ejedd d Z d S) URLopener_Testsz4Testcase to test the open method of URLopener class.cCsnGdddtjj}tdtf8||dd||ddWdn1s`0YdS)Nc@seZdZddZdS)z8URLopener_Tests.test_quoted_open..DummyURLopenercSs|Srrr!rrrr open_spam'szBURLopener_Tests.test_quoted_open..DummyURLopener.open_spamN)r3r4r5rrrrrDummyURLopener&srz8DummyURLopener style of invoking requests is deprecated.zspam://example/ /z//example/%20/z,spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/z'//c:|windows%/:=&?~#+!$,;'@()*[]|/path/)rrr rrrrsr)r!rrrrtest_quoted_open%s   z URLopener_Tests.test_quoted_open)categorycCstj}tj|d\}}t|dtj|}tj |\}}| tj |tj |Wdn1sx0YdS)N)dirzfile:)rZtemp_dirrrrdr1rrrr retrieversrnormcase)r!ZtmpdirrZtmpfileZfileurlr_rrrtest_urlopener_retrieve_file4s   z,URLopener_Tests.test_urlopener_retrieve_filecCsJd}|d||jtj|\}}|tj |dddS)Nzhttp://www.python.org/file.txtrrz.txt) r?rrCrrr rrsrdrsplitext)r!rrrrrrtest_urlopener_retrieve_remote>s   z.URLopener_Tests.test_urlopener_retrieve_remotecCsGdddtjj}dD]f}|ttjj||ttjj||ttjj||t|j||t|j|qdS)Nc@seZdZddZdS)z.DummyURLopenercSs|Srrrrrropen_local_fileJszLURLopener_Tests.test_local_file_open..DummyURLopener.open_local_fileN)r3r4r5rrrrrrIsr)zlocal_file://examplezlocal-file://example)rrr rrrrr)r!rrrrrtest_local_file_openFsz$URLopener_Tests.test_local_file_openN) r3r4r5rrrZignore_warningsrrrrrrrrr"s    rc@s eZdZdZddZddZdS) RequestTestsz&Unit tests for urllib.request.Request.cCs>tjj}|d}||d|di}||ddS)NrrZPOST)rrRequestrs get_methodr!rrrrrtest_default_valuesWs  z RequestTests.test_default_valuescCstjj}|ddd}||jd||d|didd}||jd||d|ddd}||dd|_||ddS)NrZHEAD)methodr)rrrrsrrrrrrtest_with_method_arg^s  z!RequestTests.test_with_method_argN)r3r4r5rrrrrrrrTsrc@s4eZdZddZddZddZddZd d Zd S) URL2PathNameTestscCs4|tdd|tdd|tdddS)Nz///C|C:///C:z///C|/rrsrr2rrrtest_converting_drive_letternsz.URL2PathNameTests.test_converting_drive_lettercCs$|tdd|tdddS)Nz ///C/test/z \\\C\test\z ////C/test/z \\C\test\rr2rrr$test_converting_when_no_drive_letterssz6URL2PathNameTests.test_converting_when_no_drive_lettercCs|tdddS)Nz///C|/foo/bar/spam.fooC:\foo\bar\spam.foorr2rrrtest_simple_comparexs z%URL2PathNameTests.test_simple_comparecCs|ttddS)Nu///è|/)rIOErrorrr2rrrtest_non_ascii_drive_letter|sz-URL2PathNameTests.test_non_ascii_drive_lettercCs*gd}|D]}|tt||q dS)N)rz \\\C\test\\r)rsrrr!Z list_of_pathsrrrrtest_roundtrip_url2pathnamesz-URL2PathNameTests.test_roundtrip_url2pathnameN)r3r4r5rrrrrrrrrrls rc@s4eZdZddZddZddZddZd d Zd S) PathName2URLTestscCs$|tdd|tdddS)Nrrrrsrr2rrrrsz.PathName2URLTests.test_converting_drive_lettercCs4|tdd|tdd|tdddS)Nz\\\folder\test\/////folder/test/z\\folder\test\z////folder/test/z \folder\test\z /folder/test/rr2rrrrs   z6PathName2URLTests.test_converting_when_no_drive_lettercCs|tdddS)Nr///C:/foo/bar/spam.foorr2rrrrs z%PathName2URLTests.test_simple_comparecCs|ttddS)NzXX:\)rrrr2rrrtest_long_drive_lettersz(PathName2URLTests.test_long_drive_lettercCs*gd}|D]}|tt||q dS)N)rrr)rsrrrrrrtest_roundtrip_pathname2urlsz-PathName2URLTests.test_roundtrip_pathname2urlN)r3r4r5rrrrrrrrrrs r__main__)NN)F)5r urllib.parserZurllib.requestZ urllib.errorZ http.clientr<Z email.messager{r,rZ unittest.mockrrrrdr ImportErrorrrZ nturl2pathrrbase64rrrrrrr?rOr@rDZTestCaserTrrrrr'rHrOr{rrrrrrrr3mainrrrrsd        / ]L$ M-NTDN2