{G_Tc@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z dZ defdYZdejfd YZd ejfd YZd ejefd YZdejfdYZdejefdYZdejfdYZdejfdYZdejfdYZdejfdYZdejfdYZdejfdYZdZedkrendS( sRegresssion tests for urllibiN(t test_support(t b64encodecCsCtt|dj}t|dkr;d|}nd|S(s!Escape char as RFC 2396 specifiesiis0%st%(thextordtuppertlen(tcharthex_repr((s,/usr/local/lib/python2.7/test/test_urllib.pyt hexescapes t FakeHTTPMixincBseZdZdZRS(csldtjffdYdtjffdYtjjtjks\ttj_dS(Nt FakeSocketcs8eZfdZdZddZddZRS(cs |_dS(N(tbuf(tselftdata(tFakeHTTPConnection(s,/usr/local/lib/python2.7/test/test_urllib.pytsendallsc_s|S(N((R targstkwds((s,/usr/local/lib/python2.7/test/test_urllib.pytmakefilescSs |jr dStjj||S(Nt(tclosedtStringIOtread(R tamt((s,/usr/local/lib/python2.7/test/test_urllib.pyR"s cSs |jr dStjj||S(NR(RRtreadline(R tlength((s,/usr/local/lib/python2.7/test/test_urllib.pyR's N(t__name__t __module__RRtNoneRR((R(s,/usr/local/lib/python2.7/test/test_urllib.pyR s  Rcs eZdZfdZRS(Rcs|_dS(N(tsock(R (R tfakedata(s,/usr/local/lib/python2.7/test/test_urllib.pytconnect1s(RRR R ((R R(s,/usr/local/lib/python2.7/test/test_urllib.pyR,s(RthttplibtHTTPConnectiontHTTPt_connection_classtAssertionError(R R((RR Rs,/usr/local/lib/python2.7/test/test_urllib.pytfakehttps"cCstjtj_dS(N(R!R"R#R$(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyt unfakehttp8s(RRR&R'(((s,/usr/local/lib/python2.7/test/test_urllib.pyR s turlopen_FileTestscBseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZRS(sTest 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. cCspd|jj|_ttjd}z|j|jWd|jXtj|_t j d|j|_ dS(s'Setup of a temp file to use for testingstest_urllib: %s twbNsfile:%s( t __class__RttexttfileRtTESTFNtwritetclosetpathnameturllibturlopent returned_obj(R tFILE((s,/usr/local/lib/python2.7/test/test_urllib.pytsetUpDs  cCs!|jjtjtjdS(sShut down the open objectN(R3R/tostremoveRR-(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttearDownOs c Cs5x.d D]&}|jt|j|d |qWdS( NRRt readlinestfilenoR/tinfotgeturltgetcodet__iter__s/object returned by urlopen() lacks %s attribute( sreadsreadlines readlinessfilenosclosesinfosgeturlsgetcodes__iter__(t assertTruethasattrR3(R tattr((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_interfaceTs  cCs |j|j|jjdS(N(t assertEqualR+R3R(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_read\scCs<|j|j|jj|jd|jjddS(NRsKcalling readline() after exhausting the file did not return an empty string(RCR+R3R(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_readline_scCsF|jj}|jt|dd|j|d|jddS(Nis.readlines() returned the wrong number of linesis"readlines() returned improper text(R3R9RCRR+(R t lines_list((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_readlineses cCsQ|jj}|j|td|jtj|t|j|jddS(Nsfileno() did not return an intsTReading on the file descriptor returned by fileno() did not return the expected text( R3R:tassertIsInstancetintRCR6RRR+(R tfile_num((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_filenols$cCs|jjdS(N(R3R/(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_closesscCs |j|jjtjdS(N(RHR3R;t mimetoolstMessage(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_infoxscCs |j|jj|jdS(N(RCR3R<R0(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_geturl{scCs|j|jjddS(N(RCR3R=R(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_getcode~scCs1x*|jjD]}|j||jqWdS(N(R3R>RCR+(R tline((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_iterscCs!|jttjd|jdS(Ns./(t assertRaisest ValueErrorR1R2R0(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_relativelocalfiles(RRt__doc__R5R8RBRDRERGRKRLRORPRQRSRV(((s,/usr/local/lib/python2.7/test/test_urllib.pyR(<s           t ProxyTestscBs#eZdZdZdZRS(cCsRtj|_x<tjjD]+}d|jkr|jj|qqWdS(Ntproxy(RtEnvironmentVarGuardtenvR6tenvirontkeystlowertunset(R tk((s,/usr/local/lib/python2.7/test/test_urllib.pyR5scCs|jj|`dS(N(R[t__exit__(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyR8s cCs`|jjddtj}|jd|d|jjdd|jtjddS(NtNO_PROXYt localhosttnos+localhost, anotherdomain.com, newdomain.comsanotherdomain.com(R[tsetR1tgetproxies_environmentRCR?tproxy_bypass_environment(R tproxies((s,/usr/local/lib/python2.7/test/test_urllib.pyt+test_getproxies_environment_keep_no_proxiess  (RRR5R8Ri(((s,/usr/local/lib/python2.7/test/test_urllib.pyRXs  turlopen_HttpTestscBs_eZdZdZdZdZdZdZdZdZ dZ d Z RS( s.Test urlopen() opening a fake http connection.cCs|jdzktjd}|j|jd|j|jd|j|jd|j|jdWd|jXdS(NsHello!shttp://python.org/Ri(R&R1R2RCRR<R=R'(R tfp((s,/usr/local/lib/python2.7/test/test_urllib.pyRDs cCsNd}|jdz)tj|}|j|j|Wd|jXdS(Ns-http://docs.python.org/library/urllib.html#OKsHello!(R&R1R2RCR<R'(R turlRk((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_url_fragments  cCs9|jdz|jttjdWd|jXdS(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 shttp://python.org/(R&RTtIOErrorR1R2R'(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_read_boguss cCs9|jdz|jttjdWd|jXdS(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:README Connection: close Content-Type: text/html; charset=iso-8859-1 shttp://python.org/(R&RTRnR1R2R'(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_invalid_redirects cCs9|jdz|jttjdWd|jXdS(NRshttp://something(R&RTRnR1R2R'(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_empty_sockets cCs|jttjdtj\}}d|jtjj d}|j tjj |ztj|}|j Wdtj |tj |X|jtjj ||jttj|dS(Ns"file://localhost/a/missing/file.pysfile://localhost/t/(RTRnR1R2ttempfiletmkstemptreplaceR6tpathtsepR?texistsR/tunlinkt assertFalse(R tfdttmp_filet tmp_fileurlRk((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_missing_localfiles cCs|jttjddS(Ns$ftp://localhost/not/existing/file.py(RTRnR1R2(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_ftp_nonexistingscCs|jdztjj}tjd}dtd}|j||j|j |j d|j |j d|j |j d|j |j dWd|j XdS(NsHello!shttp://user:pass@python.org/sAuthorization: Basic %s s user:passRi(R&R!R#R$R1R2RtassertInR RCRR<R=R'(R tfakehttp_wrapperRkt authorization((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_userpass_inurls   cCs|jdzd}tjj}dtd}tj|}|j||j|j |j d|j |j d|j |j ||j |j dWd|jXdS(NsHello!shttp://a b:c d@python.org/sAuthorization: Basic %s sa b:c dRi(R&R!R#R$RR1R2RR RCRtassertNotEqualR<R=R'(R RlRRRk((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_userpass_with_spaces_inurls   ( RRRWRDRmRoRpRqR~RRR(((s,/usr/local/lib/python2.7/test/test_urllib.pyRjs    turlretrieve_FileTestscBsteZdZdZdZdZddZdZdZdZ d Z d Z d Z d Z RS( s(Test urllib.urlretrieve() on local filescCsrg|_|jtjd|_z0ttjd}|j|j|jWdy|jWnnXXdS(Nstesting urllib.urlretrieveR)(t tempFilestregisterFileForCleanUpRR-R+R,R.R/(R R4((s,/usr/local/lib/python2.7/test/test_urllib.pyR5s  cCs3x,|jD]!}ytj|Wq q Xq WdS(N(RR6R7(R teach((s,/usr/local/lib/python2.7/test/test_urllib.pyR81s cCsdtjtjj|S(Ns file://%s(R1t pathname2urlR6Rvtabspath(R tfilePath((s,/usr/local/lib/python2.7/test/test_urllib.pytconstructLocalFileUrl7sRcCsltj\}}z:|j|tj|d}|j||jWdy|jWnnXX|S(sCreates 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.R)N(RsRtRR6tfdopenR.R/(R RtnewFdt newFilePathtnewFile((s,/usr/local/lib/python2.7/test/test_urllib.pytcreateNewTempFile:s  cCs|jj|dS(N(Rtappend(R tfileName((s,/usr/local/lib/python2.7/test/test_urllib.pyRJscCsKtjdtj}|j|dtj|j|dtjddS(Nsfile:%siisAdid not get a mimetools.Message instance as second returned value(R1t urlretrieveRR-RCRHRMRN(R tresult((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_basicMscCsdtj}|j|tj|jtj|}|j||d|jtj j |dt |d}z|j }|j Wdy|j WnnXX|j|j|dS(Ns%s.2iscopy of the file was not madetrb(RR-RR1RRRCR?R6RvRxR,RR/R+(R t second_tempRR4R+((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_copyVs    csRdgfd}dtj}j|tjjtj||dS(NicsZj|tj|tj|tj||d|dd|d. 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 cCswdjddddg}tj|}|j||d||ftj|}|j||d||fdS(NRtABCDEFGHIJKLMNOPQRSTUVWXYZtabcdefghijklmnopqrstuvwxyzt 0123456789s_.-susing quote(): %s != %ssusing quote_plus(): %s != %s(tjoinR1tquoteRCt quote_plus(R t do_not_quoteR((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_never_quotes    cCs|jtjjdddS(NiRr(RCR1Rt func_defaults(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_default_safescCsnd}tj|d|}|j||d||ftj|d|}|j||d||fdS(Ns<>tsafesusing quote(): %s != %ssusing quote_plus(): %s != %s(R1RRCR(R tquote_by_defaultR((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_safes  cCs\gtdD]}t|^q }|jd|jtddj|}x|D]|}tj|}|jt||d|t||ftj|}|jt||d|t||fq[W~d}d}tj|}|j||d ||ftj|}|j||d ||f|j t tjddS( Ni s <>#%"{}|\^[]`iRs1using quote(): %s should be escaped to %s, not %ss6using quote_plus(): %s should be escapes to %s, not %ssab[]cds ab%5B%5Dcdsusing quote(): %s != %ssusing quote_plus(): %s != %s( trangetchrRRR1RRCR RRTt TypeErrorR(R tnumt should_quoteRRt partial_quotetexpected((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_default_quotings.%    cCstjd}|j|tdd|tdftjd}|j|dd|d}|jdtd}tj|}|j||d||f|jdd}tj|}|j||d||fdS(Nt susing quote(): %s != %st+susing quote_plus(): %s != +s a b cd e fsusing quote_plus(): %s != %s(R1RRCR RRu(R Rtgiventexpect((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_quoting_spaces    cCs9|jtjdd|jtjddddS(Nsalpha+beta gammasalpha%2Bbeta+gammaRsalpha+beta+gamma(RCR1R(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_quoting_plus)s( RRRWRRRRRR(((s,/usr/local/lib/python2.7/test/test_urllib.pyRs   tUnquotingTestscBsDeZdZdZdZdZdZdZdZRS(swTests for unquote() and unquote_plus() See the doc string for quoting_Tests for details on quoting and such. cCsg}xtdD]}tt|}t|}tj|}|j||d||ftj|}|j||d||f|j|qWdj|}~tj|}|j|j ddd|tj|}|j|j ddd|dS( Nisusing unquote(): %s != %ssusing unquote_plus(): %s != %sRRis-using quote(): not all characters escaped; %ss/using unquote(): not all characters escaped: %s( RR RR1tunquoteRCt unquote_plusRRR(R t escape_listRRRRt escape_string((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_unquoting6s,   cCsd}|}tj|}|j||d||fd}|}tj|}|j||d||fd}|}tj|}|j||d||fdS(Ns%xabsusing unquote(): %r != %rs%xR(R1RRC(R RRR((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_unquoting_badpercentOscCs<d}d}tj|}|j||d||fdS(Ns%Ab%eAssusing unquote(): %r != %r(R1RRC(R RRR((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_unquoting_mixed_caseas cCsrdtd}d}tj|}|j||d||ftj|}|j||d||fdS(Nsab%sdtctabcdsusing quote(): %s != %ssusing unquote_plus(): %s != %s(R R1RRCR(R RRR((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_unquoting_partsis  cCszd}|}tj|}|j||d||f|jdd}tj|}|j||d||fdS(Nsare+there+spaces...susing unquote(): %s != %sRRsusing unquote_plus(): %s != %s(R1RRCRuR(R RRR((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_unquoting_plusus  cCs#tjd}|j|ddS(Nu"br%C3%BCckner_sapporo_20050930.docu brückner_sapporo_20050930.doc(R1RRC(R tr((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_unquote_with_unicodes( RRRWRRRRRR(((s,/usr/local/lib/python2.7/test/test_urllib.pyR/s    turlencode_TestscBs;eZdZdZdZdZdZdZRS(sTests for urlencode()c Csdddg}tj|}x.|D]&}|j||d|||fq%W|j|jddd||jdf|jd}||d}||d}|j|jo|jd ||f|jt|dd |t|dfd S(sHelper 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. s1st=1s2nd=2s3rd=3stesting %s: %s not found in %st&is#testing %s: expected 2 '&'s; got %sis1testing %s: '&' not located in proper place in %siis5testing %s: unexpected number of characters: %s != %sNiiii( R1t urlencodeRRCRtindexR?tisdigitR( R Rt test_typetexpect_somewhereRRt amp_locationt on_amp_leftt on_amp_right((s,/usr/local/lib/python2.7/test/test_urllib.pythelp_inputtypes$   cCs)|jidd6dd6dd6ddS(Nt1t1stt2t2ndt3t3rdsusing dict as input type(R(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_using_mappingscCs|jdd d gddS( NRRRRRRs*using sequence of two-item tuples as input(s1stR(s2ndR(s3rdR(R(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_using_sequencescCs~idd6}dtdtdf}tj|}|j||idd6}d}tj|}|j||dS(Nt=Rs%s=%ssA bunch of plusesskey nameskey+name=A+bunch+of+pluses(R R1RRC(R RRR((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_quotings  cCsidddgd6}dtjtdddg}tj|}|j||tj|t}x,|dD] }d|}|j||qtW|j|jddd|jddS( NRRRtsequences sequence=%sRisExpected 2 '&'s, got %s(R1RtstrRRCtTrueRR(R RRRtvalue((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_doseqs" (RRRWRRRRR(((s,/usr/local/lib/python2.7/test/test_urllib.pyRs    tPathname_TestscBsDeZdZdZdZejejdkddZ RS(s&Test pathname2url() and url2pathname()cCs}tjjdddd}d}tj|}|j||d||ftj|}|j||d||fdS(NtpartstoftaRvsparts/of/a/pathspathname2url() failed; %s != %ssurl2pathame() failed; %s != %s(R6RvRR1RRCt url2pathname(R t expected_patht expected_urlR((s,/usr/local/lib/python2.7/test/test_urllib.pyRs  cCs(tjjddd}dtjd}tj|}|j||d||f|}tj|}|j||d||ftjjdd}d tjd}tj|}|j||d||fd }tjjd d }tj|}|j||d||fdS( Ntneedssquot=ingtheres needs/%s/herespathname2url() failed; %s != %ssurl2pathname() failed; %s != %ss make suret using_quotes%s/using_quotesmake+sure/using_unquotes make+suret using_unquote(R6RvRR1RRRCR(R RRR((s,/usr/local/lib/python2.7/test/test_urllib.pyRs.    twin32s'test specific to the nturl2path librarycCsd}d}x:|D]2}tj|}|j||d||fqWd}d}tj|}|j||d||fdS( Ns/C:/s///C:/s/C|//sC:\s*nturl2path.url2pathname() failed; %s != %ss ///C|/pathsC:\path(s/C:/s///C:/s/C|//(R1RRC(R RRRlR((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_ntpaths   ( RRRWRRtunittestt skipUnlesstsystplatformR(((s,/usr/local/lib/python2.7/test/test_urllib.pyRs  t Utility_TestscBs)eZdZdZdZdZRS(s=Testcase to test the various utility functions in the urllib.cCs|jdtjd|jdtjd|jdtjd|jdtjd |jdtjd |jdtjd |jdtjd|jdtjd|jd tjd|jd!tjddS("sSome of the password examples are not sensible, but it is added to confirming to RFC2617 and addressing issue4675. tusertabsuser:absa bsuser:a bsa bsuser:a bsa bsuser:a bsa bsuser:a bsa bsuser:a bsa:bsuser:a:bsa bsuser:a bsuser 2s user 2:absuser+1sa+bs user+1:a+bN(RR (Rsa b(Rsa b(Rsa b(Rsa b(Rsa b(Rsa:b(Rsa b(suser 2R (suser+1sa+b(RCR1t splitpasswd(R ((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_splitpasswd scCs{tj}|j|dd|j|dd|j|dd |j|dd |j|dd dS( Ns parrot:88tparrott88sparrot:s 127.0.0.1s parrot:cheese(R R (R N(R N(s 127.0.0.1N(s parrot:cheeseN(R1t splitportRCR(R R((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_splitports  cCstj}|j|dd |j|dd |j|ddd |j|dd |j|ddd |j|dd|j|ddd|j|dd|j|ddddS(Ns parrot:88R iXii7sparrot:s 127.0.0.1s parrot:cheese(sparrotiX(sparroti(sparroti7(sparroti(sparroti7(s 127.0.0.1i(s 127.0.0.1i7(sparrotN(sparrotN(R1t splitnportRCR(R R((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_splitnport s (RRRWR RR(((s,/usr/local/lib/python2.7/test/test_urllib.pyRs  tURLopener_TestscBseZdZdZRS(s4Testcase to test the open method of URLopener class.cCsUdtjfdY}|j|jdd|j|jdddS(NtDummyURLopenercBseZdZRS(cSs|S(N((R Rl((s,/usr/local/lib/python2.7/test/test_urllib.pyt open_spam2s(RRR(((s,/usr/local/lib/python2.7/test/test_urllib.pyR1ssspam://example/ /s//example/%20/s,spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/s'//c:|windows%/:=&?~#+!$,;'@()*[]|/path/(R1t URLopenerRCtopen(R R((s,/usr/local/lib/python2.7/test/test_urllib.pyttest_quoted_open0s  (RRRWR(((s,/usr/local/lib/python2.7/test/test_urllib.pyR-scCsaddl}|jC|jddttjtttt t t t t ttt WdQXdS(Nitignores.*urllib\.urlopen.*Python 3.0(twarningstcatch_warningstfilterwarningstDeprecationWarningRt run_unittestR(RjRRRXRRRRRR(R((s,/usr/local/lib/python2.7/test/test_urllib.pyt test_mains    t__main__(RWR1R!RR6RRMRsRttestRtbase64RR tobjectR tTestCaseR(RXRjRRRRRRRRRR(((s,/usr/local/lib/python2.7/test/test_urllib.pyts4         $Pt&kVE<'a