Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_urllib.py

    r2 r391  
    44import httplib
    55import unittest
    6 from test import test_support
    76import os
     7import sys
    88import mimetools
    99import tempfile
    1010import StringIO
     11
     12from test import test_support
     13from base64 import b64encode
     14
    1115
    1216def hexescape(char):
     
    1721    return "%" + hex_repr
    1822
     23
     24class FakeHTTPMixin(object):
     25    def fakehttp(self, fakedata):
     26        class FakeSocket(StringIO.StringIO):
     27
     28            def sendall(self, data):
     29                FakeHTTPConnection.buf = data
     30
     31            def makefile(self, *args, **kwds):
     32                return self
     33
     34            def read(self, amt=None):
     35                if self.closed:
     36                    return ""
     37                return StringIO.StringIO.read(self, amt)
     38
     39            def readline(self, length=None):
     40                if self.closed:
     41                    return ""
     42                return StringIO.StringIO.readline(self, length)
     43
     44        class FakeHTTPConnection(httplib.HTTPConnection):
     45
     46            # buffer to store data for verification in urlopen tests.
     47            buf = ""
     48
     49            def connect(self):
     50                self.sock = FakeSocket(fakedata)
     51
     52        assert httplib.HTTP._connection_class == httplib.HTTPConnection
     53
     54        httplib.HTTP._connection_class = FakeHTTPConnection
     55
     56    def unfakehttp(self):
     57        httplib.HTTP._connection_class = httplib.HTTPConnection
     58
     59
    1960class urlopen_FileTests(unittest.TestCase):
    2061    """Test urlopen() opening a temporary file.
     
    4586        for attr in ("read", "readline", "readlines", "fileno",
    4687                     "close", "info", "geturl", "getcode", "__iter__"):
    47             self.assert_(hasattr(self.returned_obj, attr),
     88            self.assertTrue(hasattr(self.returned_obj, attr),
    4889                         "object returned by urlopen() lacks %s attribute" %
    4990                         attr)
     
    67108    def test_fileno(self):
    68109        file_num = self.returned_obj.fileno()
    69         self.assert_(isinstance(file_num, int),
    70                      "fileno() did not return an int")
     110        self.assertIsInstance(file_num, int, "fileno() did not return an int")
    71111        self.assertEqual(os.read(file_num, len(self.text)), self.text,
    72112                         "Reading on the file descriptor returned by fileno() "
     
    79119
    80120    def test_info(self):
    81         self.assert_(isinstance(self.returned_obj.info(), mimetools.Message))
     121        self.assertIsInstance(self.returned_obj.info(), mimetools.Message)
    82122
    83123    def test_geturl(self):
     
    95135            self.assertEqual(line, self.text)
    96136
     137    def test_relativelocalfile(self):
     138        self.assertRaises(ValueError,urllib.urlopen,'./' + self.pathname)
     139
    97140class ProxyTests(unittest.TestCase):
    98141
     
    114157        proxies = urllib.getproxies_environment()
    115158        # getproxies_environment use lowered case truncated (no '_proxy') keys
    116         self.assertEquals('localhost', proxies['no'])
    117 
    118 
    119 class urlopen_HttpTests(unittest.TestCase):
     159        self.assertEqual('localhost', proxies['no'])
     160        # List of no_proxies with space.
     161        self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')
     162        self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com'))
     163
     164
     165class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin):
    120166    """Test urlopen() opening a fake http connection."""
    121 
    122     def fakehttp(self, fakedata):
    123         class FakeSocket(StringIO.StringIO):
    124             def sendall(self, str): pass
    125             def makefile(self, mode, name): return self
    126             def read(self, amt=None):
    127                 if self.closed: return ''
    128                 return StringIO.StringIO.read(self, amt)
    129             def readline(self, length=None):
    130                 if self.closed: return ''
    131                 return StringIO.StringIO.readline(self, length)
    132         class FakeHTTPConnection(httplib.HTTPConnection):
    133             def connect(self):
    134                 self.sock = FakeSocket(fakedata)
    135         assert httplib.HTTP._connection_class == httplib.HTTPConnection
    136         httplib.HTTP._connection_class = FakeHTTPConnection
    137 
    138     def unfakehttp(self):
    139         httplib.HTTP._connection_class = httplib.HTTPConnection
    140167
    141168    def test_read(self):
     
    147174            self.assertEqual(fp.geturl(), 'http://python.org/')
    148175            self.assertEqual(fp.getcode(), 200)
     176        finally:
     177            self.unfakehttp()
     178
     179    def test_url_fragment(self):
     180        # Issue #11703: geturl() omits fragments in the original URL.
     181        url = 'http://docs.python.org/library/urllib.html#OK'
     182        self.fakehttp('Hello!')
     183        try:
     184            fp = urllib.urlopen(url)
     185            self.assertEqual(fp.geturl(), url)
    149186        finally:
    150187            self.unfakehttp()
     
    163200            self.unfakehttp()
    164201
     202    def test_invalid_redirect(self):
     203        # urlopen() should raise IOError for many error codes.
     204        self.fakehttp("""HTTP/1.1 302 Found
     205Date: Wed, 02 Jan 2008 03:03:54 GMT
     206Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
     207Location: file:README
     208Connection: close
     209Content-Type: text/html; charset=iso-8859-1
     210""")
     211        try:
     212            self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
     213        finally:
     214            self.unfakehttp()
     215
    165216    def test_empty_socket(self):
    166217        # urlopen() raises IOError if the underlying socket does not send any
     
    171222        finally:
    172223            self.unfakehttp()
     224
     225    def test_missing_localfile(self):
     226        self.assertRaises(IOError, urllib.urlopen,
     227                'file://localhost/a/missing/file.py')
     228        fd, tmp_file = tempfile.mkstemp()
     229        tmp_fileurl = 'file://localhost/' + tmp_file.replace(os.path.sep, '/')
     230        self.assertTrue(os.path.exists(tmp_file))
     231        try:
     232            fp = urllib.urlopen(tmp_fileurl)
     233            fp.close()
     234        finally:
     235            os.close(fd)
     236            os.unlink(tmp_file)
     237
     238        self.assertFalse(os.path.exists(tmp_file))
     239        self.assertRaises(IOError, urllib.urlopen, tmp_fileurl)
     240
     241    def test_ftp_nonexisting(self):
     242        self.assertRaises(IOError, urllib.urlopen,
     243                'ftp://localhost/not/existing/file.py')
     244
     245
     246    def test_userpass_inurl(self):
     247        self.fakehttp('Hello!')
     248        try:
     249            fakehttp_wrapper = httplib.HTTP._connection_class
     250            fp = urllib.urlopen("http://user:pass@python.org/")
     251            authorization = ("Authorization: Basic %s\r\n" %
     252                            b64encode('user:pass'))
     253            # The authorization header must be in place
     254            self.assertIn(authorization, fakehttp_wrapper.buf)
     255            self.assertEqual(fp.readline(), "Hello!")
     256            self.assertEqual(fp.readline(), "")
     257            self.assertEqual(fp.geturl(), 'http://user:pass@python.org/')
     258            self.assertEqual(fp.getcode(), 200)
     259        finally:
     260            self.unfakehttp()
     261
     262    def test_userpass_with_spaces_inurl(self):
     263        self.fakehttp('Hello!')
     264        try:
     265            url = "http://a b:c d@python.org/"
     266            fakehttp_wrapper = httplib.HTTP._connection_class
     267            authorization = ("Authorization: Basic %s\r\n" %
     268                             b64encode('a b:c d'))
     269            fp = urllib.urlopen(url)
     270            # The authorization header must be in place
     271            self.assertIn(authorization, fakehttp_wrapper.buf)
     272            self.assertEqual(fp.readline(), "Hello!")
     273            self.assertEqual(fp.readline(), "")
     274            # the spaces are quoted in URL so no match
     275            self.assertNotEqual(fp.geturl(), url)
     276            self.assertEqual(fp.getcode(), 200)
     277        finally:
     278            self.unfakehttp()
     279
    173280
    174281class urlretrieve_FileTests(unittest.TestCase):
     
    229336        result = urllib.urlretrieve("file:%s" % test_support.TESTFN)
    230337        self.assertEqual(result[0], test_support.TESTFN)
    231         self.assert_(isinstance(result[1], mimetools.Message),
    232                      "did not get a mimetools.Message instance as second "
    233                      "returned value")
     338        self.assertIsInstance(result[1], mimetools.Message,
     339                              "did not get a mimetools.Message instance as "
     340                              "second returned value")
    234341
    235342    def test_copy(self):
     
    240347            test_support.TESTFN), second_temp)
    241348        self.assertEqual(second_temp, result[0])
    242         self.assert_(os.path.exists(second_temp), "copy of the file was not "
     349        self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
    243350                                                  "made")
    244351        FILE = file(second_temp, 'rb')
     
    254361        # Make sure that the reporthook works.
    255362        def hooktester(count, block_size, total_size, count_holder=[0]):
    256             self.assert_(isinstance(count, int))
    257             self.assert_(isinstance(block_size, int))
    258             self.assert_(isinstance(total_size, int))
     363            self.assertIsInstance(count, int)
     364            self.assertIsInstance(block_size, int)
     365            self.assertIsInstance(total_size, int)
    259366            self.assertEqual(count, count_holder[0])
    260367            count_holder[0] = count_holder[0] + 1
     
    303410        self.assertEqual(report[0][1], 8192)
    304411        self.assertEqual(report[0][2], 8193)
     412
     413
     414class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
     415    """Test urllib.urlretrieve() using fake http connections"""
     416
     417    def test_short_content_raises_ContentTooShortError(self):
     418        self.fakehttp('''HTTP/1.1 200 OK
     419Date: Wed, 02 Jan 2008 03:03:54 GMT
     420Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
     421Connection: close
     422Content-Length: 100
     423Content-Type: text/html; charset=iso-8859-1
     424
     425FF
     426''')
     427
     428        def _reporthook(par1, par2, par3):
     429            pass
     430
     431        try:
     432            self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve,
     433                    'http://example.com', reporthook=_reporthook)
     434        finally:
     435            self.unfakehttp()
     436
     437    def test_short_content_raises_ContentTooShortError_without_reporthook(self):
     438        self.fakehttp('''HTTP/1.1 200 OK
     439Date: Wed, 02 Jan 2008 03:03:54 GMT
     440Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
     441Connection: close
     442Content-Length: 100
     443Content-Type: text/html; charset=iso-8859-1
     444
     445FF
     446''')
     447        try:
     448            self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve, 'http://example.com/')
     449        finally:
     450            self.unfakehttp()
    305451
    306452class QuotingTests(unittest.TestCase):
     
    381527        self.assertEqual(expected, result,
    382528                         "using quote(): %s != %s" % (expected, result))
     529        result = urllib.quote_plus(partial_quote)
    383530        self.assertEqual(expected, result,
    384531                         "using quote_plus(): %s != %s" % (expected, result))
     532        self.assertRaises(TypeError, urllib.quote, None)
    385533
    386534    def test_quoting_space(self):
     
    441589                         "%s" % result)
    442590
     591    def test_unquoting_badpercent(self):
     592        # Test unquoting on bad percent-escapes
     593        given = '%xab'
     594        expect = given
     595        result = urllib.unquote(given)
     596        self.assertEqual(expect, result, "using unquote(): %r != %r"
     597                         % (expect, result))
     598        given = '%x'
     599        expect = given
     600        result = urllib.unquote(given)
     601        self.assertEqual(expect, result, "using unquote(): %r != %r"
     602                         % (expect, result))
     603        given = '%'
     604        expect = given
     605        result = urllib.unquote(given)
     606        self.assertEqual(expect, result, "using unquote(): %r != %r"
     607                         % (expect, result))
     608
     609    def test_unquoting_mixed_case(self):
     610        # Test unquoting on mixed-case hex digits in the percent-escapes
     611        given = '%Ab%eA'
     612        expect = '\xab\xea'
     613        result = urllib.unquote(given)
     614        self.assertEqual(expect, result, "using unquote(): %r != %r"
     615                         % (expect, result))
     616
    443617    def test_unquoting_parts(self):
    444618        # Make sure unquoting works when have non-quoted characters
     
    487661        result = urllib.urlencode(given)
    488662        for expected in expect_somewhere:
    489             self.assert_(expected in result,
     663            self.assertIn(expected, result,
    490664                         "testing %s: %s not found in %s" %
    491665                         (test_type, expected, result))
     
    496670        on_amp_left = result[amp_location - 1]
    497671        on_amp_right = result[amp_location + 1]
    498         self.assert_(on_amp_left.isdigit() and on_amp_right.isdigit(),
     672        self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
    499673                     "testing %s: '&' not located in proper place in %s" %
    500674                     (test_type, result))
     
    534708        for value in given["sequence"]:
    535709            expect = "sequence=%s" % value
    536             self.assert_(expect in result,
    537                          "%s not found in %s" % (expect, result))
     710            self.assertIn(expect, result)
    538711        self.assertEqual(result.count('&'), 2,
    539712                         "Expected 2 '&'s, got %s" % result.count('&'))
     
    582755                         (expect, result))
    583756
     757    @unittest.skipUnless(sys.platform == 'win32',
     758                         'test specific to the nturl2path library')
     759    def test_ntpath(self):
     760        given = ('/C:/', '///C:/', '/C|//')
     761        expect = 'C:\\'
     762        for url in given:
     763            result = urllib.url2pathname(url)
     764            self.assertEqual(expect, result,
     765                             'nturl2path.url2pathname() failed; %s != %s' %
     766                             (expect, result))
     767        given = '///C|/path'
     768        expect = 'C:\\path'
     769        result = urllib.url2pathname(given)
     770        self.assertEqual(expect, result,
     771                         'nturl2path.url2pathname() failed; %s != %s' %
     772                         (expect, result))
     773
     774class Utility_Tests(unittest.TestCase):
     775    """Testcase to test the various utility functions in the urllib."""
     776
     777    def test_splitpasswd(self):
     778        """Some of the password examples are not sensible, but it is added to
     779        confirming to RFC2617 and addressing issue4675.
     780        """
     781        self.assertEqual(('user', 'ab'),urllib.splitpasswd('user:ab'))
     782        self.assertEqual(('user', 'a\nb'),urllib.splitpasswd('user:a\nb'))
     783        self.assertEqual(('user', 'a\tb'),urllib.splitpasswd('user:a\tb'))
     784        self.assertEqual(('user', 'a\rb'),urllib.splitpasswd('user:a\rb'))
     785        self.assertEqual(('user', 'a\fb'),urllib.splitpasswd('user:a\fb'))
     786        self.assertEqual(('user', 'a\vb'),urllib.splitpasswd('user:a\vb'))
     787        self.assertEqual(('user', 'a:b'),urllib.splitpasswd('user:a:b'))
     788        self.assertEqual(('user', 'a b'),urllib.splitpasswd('user:a b'))
     789        self.assertEqual(('user 2', 'ab'),urllib.splitpasswd('user 2:ab'))
     790        self.assertEqual(('user+1', 'a+b'),urllib.splitpasswd('user+1:a+b'))
     791
     792
    584793class URLopener_Tests(unittest.TestCase):
    585794    """Testcase to test the open method of URLopener class."""
     795
    586796    def test_quoted_open(self):
    587797        class DummyURLopener(urllib.URLopener):
     
    600810# Just commented them out.
    601811# Can't really tell why keep failing in windows and sparc.
    602 # Everywhere else they work ok, but on those machines, someteimes
     812# Everywhere else they work ok, but on those machines, sometimes
    603813# fail in one of the tests, sometimes in other. I have a linux, and
    604814# the tests go ok.
    605 # If anybody has one of the problematic enviroments, please help!
     815# If anybody has one of the problematic environments, please help!
    606816# .   Facundo
    607817#
     
    649859#         # global default timeout is ignored
    650860#         import socket
    651 #         self.assert_(socket.getdefaulttimeout() is None)
     861#         self.assertTrue(socket.getdefaulttimeout() is None)
    652862#         socket.setdefaulttimeout(30)
    653863#         try:
     
    661871#         # global default timeout is used
    662872#         import socket
    663 #         self.assert_(socket.getdefaulttimeout() is None)
     873#         self.assertTrue(socket.getdefaulttimeout() is None)
    664874#         socket.setdefaulttimeout(30)
    665875#         try:
     
    687897            urlopen_HttpTests,
    688898            urlretrieve_FileTests,
     899            urlretrieve_HttpTests,
    689900            ProxyTests,
    690901            QuotingTests,
     
    692903            urlencode_Tests,
    693904            Pathname_Tests,
     905            Utility_Tests,
    694906            URLopener_Tests,
    695907            #FTPWrapperTests,
Note: See TracChangeset for help on using the changeset viewer.