Changeset 391 for python/trunk/Lib/test/test_urllib.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_urllib.py
r2 r391 4 4 import httplib 5 5 import unittest 6 from test import test_support7 6 import os 7 import sys 8 8 import mimetools 9 9 import tempfile 10 10 import StringIO 11 12 from test import test_support 13 from base64 import b64encode 14 11 15 12 16 def hexescape(char): … … 17 21 return "%" + hex_repr 18 22 23 24 class 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 19 60 class urlopen_FileTests(unittest.TestCase): 20 61 """Test urlopen() opening a temporary file. … … 45 86 for attr in ("read", "readline", "readlines", "fileno", 46 87 "close", "info", "geturl", "getcode", "__iter__"): 47 self.assert _(hasattr(self.returned_obj, attr),88 self.assertTrue(hasattr(self.returned_obj, attr), 48 89 "object returned by urlopen() lacks %s attribute" % 49 90 attr) … … 67 108 def test_fileno(self): 68 109 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") 71 111 self.assertEqual(os.read(file_num, len(self.text)), self.text, 72 112 "Reading on the file descriptor returned by fileno() " … … 79 119 80 120 def test_info(self): 81 self.assert _(isinstance(self.returned_obj.info(), mimetools.Message))121 self.assertIsInstance(self.returned_obj.info(), mimetools.Message) 82 122 83 123 def test_geturl(self): … … 95 135 self.assertEqual(line, self.text) 96 136 137 def test_relativelocalfile(self): 138 self.assertRaises(ValueError,urllib.urlopen,'./' + self.pathname) 139 97 140 class ProxyTests(unittest.TestCase): 98 141 … … 114 157 proxies = urllib.getproxies_environment() 115 158 # 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 165 class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin): 120 166 """Test urlopen() opening a fake http connection.""" 121 122 def fakehttp(self, fakedata):123 class FakeSocket(StringIO.StringIO):124 def sendall(self, str): pass125 def makefile(self, mode, name): return self126 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.HTTPConnection136 httplib.HTTP._connection_class = FakeHTTPConnection137 138 def unfakehttp(self):139 httplib.HTTP._connection_class = httplib.HTTPConnection140 167 141 168 def test_read(self): … … 147 174 self.assertEqual(fp.geturl(), 'http://python.org/') 148 175 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) 149 186 finally: 150 187 self.unfakehttp() … … 163 200 self.unfakehttp() 164 201 202 def test_invalid_redirect(self): 203 # urlopen() should raise IOError for many error codes. 204 self.fakehttp("""HTTP/1.1 302 Found 205 Date: Wed, 02 Jan 2008 03:03:54 GMT 206 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e 207 Location: file:README 208 Connection: close 209 Content-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 165 216 def test_empty_socket(self): 166 217 # urlopen() raises IOError if the underlying socket does not send any … … 171 222 finally: 172 223 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 173 280 174 281 class urlretrieve_FileTests(unittest.TestCase): … … 229 336 result = urllib.urlretrieve("file:%s" % test_support.TESTFN) 230 337 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") 234 341 235 342 def test_copy(self): … … 240 347 test_support.TESTFN), second_temp) 241 348 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 " 243 350 "made") 244 351 FILE = file(second_temp, 'rb') … … 254 361 # Make sure that the reporthook works. 255 362 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) 259 366 self.assertEqual(count, count_holder[0]) 260 367 count_holder[0] = count_holder[0] + 1 … … 303 410 self.assertEqual(report[0][1], 8192) 304 411 self.assertEqual(report[0][2], 8193) 412 413 414 class 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 419 Date: Wed, 02 Jan 2008 03:03:54 GMT 420 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e 421 Connection: close 422 Content-Length: 100 423 Content-Type: text/html; charset=iso-8859-1 424 425 FF 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 439 Date: Wed, 02 Jan 2008 03:03:54 GMT 440 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e 441 Connection: close 442 Content-Length: 100 443 Content-Type: text/html; charset=iso-8859-1 444 445 FF 446 ''') 447 try: 448 self.assertRaises(urllib.ContentTooShortError, urllib.urlretrieve, 'http://example.com/') 449 finally: 450 self.unfakehttp() 305 451 306 452 class QuotingTests(unittest.TestCase): … … 381 527 self.assertEqual(expected, result, 382 528 "using quote(): %s != %s" % (expected, result)) 529 result = urllib.quote_plus(partial_quote) 383 530 self.assertEqual(expected, result, 384 531 "using quote_plus(): %s != %s" % (expected, result)) 532 self.assertRaises(TypeError, urllib.quote, None) 385 533 386 534 def test_quoting_space(self): … … 441 589 "%s" % result) 442 590 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 443 617 def test_unquoting_parts(self): 444 618 # Make sure unquoting works when have non-quoted characters … … 487 661 result = urllib.urlencode(given) 488 662 for expected in expect_somewhere: 489 self.assert _(expected inresult,663 self.assertIn(expected, result, 490 664 "testing %s: %s not found in %s" % 491 665 (test_type, expected, result)) … … 496 670 on_amp_left = result[amp_location - 1] 497 671 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(), 499 673 "testing %s: '&' not located in proper place in %s" % 500 674 (test_type, result)) … … 534 708 for value in given["sequence"]: 535 709 expect = "sequence=%s" % value 536 self.assert_(expect in result, 537 "%s not found in %s" % (expect, result)) 710 self.assertIn(expect, result) 538 711 self.assertEqual(result.count('&'), 2, 539 712 "Expected 2 '&'s, got %s" % result.count('&')) … … 582 755 (expect, result)) 583 756 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 774 class 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 584 793 class URLopener_Tests(unittest.TestCase): 585 794 """Testcase to test the open method of URLopener class.""" 795 586 796 def test_quoted_open(self): 587 797 class DummyURLopener(urllib.URLopener): … … 600 810 # Just commented them out. 601 811 # Can't really tell why keep failing in windows and sparc. 602 # Everywhere else they work ok, but on those machines, somet eimes812 # Everywhere else they work ok, but on those machines, sometimes 603 813 # fail in one of the tests, sometimes in other. I have a linux, and 604 814 # the tests go ok. 605 # If anybody has one of the problematic enviro ments, please help!815 # If anybody has one of the problematic environments, please help! 606 816 # . Facundo 607 817 # … … 649 859 # # global default timeout is ignored 650 860 # import socket 651 # self.assert _(socket.getdefaulttimeout() is None)861 # self.assertTrue(socket.getdefaulttimeout() is None) 652 862 # socket.setdefaulttimeout(30) 653 863 # try: … … 661 871 # # global default timeout is used 662 872 # import socket 663 # self.assert _(socket.getdefaulttimeout() is None)873 # self.assertTrue(socket.getdefaulttimeout() is None) 664 874 # socket.setdefaulttimeout(30) 665 875 # try: … … 687 897 urlopen_HttpTests, 688 898 urlretrieve_FileTests, 899 urlretrieve_HttpTests, 689 900 ProxyTests, 690 901 QuotingTests, … … 692 903 urlencode_Tests, 693 904 Pathname_Tests, 905 Utility_Tests, 694 906 URLopener_Tests, 695 907 #FTPWrapperTests,
Note:
See TracChangeset
for help on using the changeset viewer.