Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1 | """Regresssion tests for urllib""" |
| 2 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 3 | import urllib.parse |
| 4 | import urllib.request |
guido@google.com | a119df9 | 2011-03-29 11:41:02 -0700 | [diff] [blame] | 5 | import urllib.error |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 6 | import http.client |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 7 | import email.message |
Jeremy Hylton | 66dc8c5 | 2007-08-04 03:42:26 +0000 | [diff] [blame] | 8 | import io |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 9 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test import support |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 11 | import os |
Senthil Kumaran | 2d2ea1b | 2011-04-14 13:16:30 +0800 | [diff] [blame] | 12 | import sys |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 13 | import tempfile |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 14 | |
Senthil Kumaran | c5c5a14 | 2012-01-14 19:09:04 +0800 | [diff] [blame] | 15 | from base64 import b64encode |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 16 | import collections |
Senthil Kumaran | c5c5a14 | 2012-01-14 19:09:04 +0800 | [diff] [blame] | 17 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 18 | def hexescape(char): |
| 19 | """Escape char as RFC 2396 specifies""" |
| 20 | hex_repr = hex(ord(char))[2:].upper() |
| 21 | if len(hex_repr) == 1: |
| 22 | hex_repr = "0%s" % hex_repr |
| 23 | return "%" + hex_repr |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 24 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 25 | # Shortcut for testing FancyURLopener |
| 26 | _urlopener = None |
| 27 | def urlopen(url, data=None, proxies=None): |
| 28 | """urlopen(url [, data]) -> open file-like object""" |
| 29 | global _urlopener |
| 30 | if proxies is not None: |
| 31 | opener = urllib.request.FancyURLopener(proxies=proxies) |
| 32 | elif not _urlopener: |
| 33 | opener = urllib.request.FancyURLopener() |
| 34 | _urlopener = opener |
| 35 | else: |
| 36 | opener = _urlopener |
| 37 | if data is None: |
| 38 | return opener.open(url) |
| 39 | else: |
| 40 | return opener.open(url, data) |
| 41 | |
Senthil Kumaran | ce26014 | 2011-11-01 01:35:17 +0800 | [diff] [blame] | 42 | |
| 43 | class FakeHTTPMixin(object): |
| 44 | def fakehttp(self, fakedata): |
| 45 | class FakeSocket(io.BytesIO): |
| 46 | io_refs = 1 |
| 47 | |
Senthil Kumaran | c5c5a14 | 2012-01-14 19:09:04 +0800 | [diff] [blame] | 48 | def sendall(self, data): |
| 49 | FakeHTTPConnection.buf = data |
Senthil Kumaran | ce26014 | 2011-11-01 01:35:17 +0800 | [diff] [blame] | 50 | |
| 51 | def makefile(self, *args, **kwds): |
| 52 | self.io_refs += 1 |
| 53 | return self |
| 54 | |
| 55 | def read(self, amt=None): |
| 56 | if self.closed: |
| 57 | return b"" |
| 58 | return io.BytesIO.read(self, amt) |
| 59 | |
| 60 | def readline(self, length=None): |
| 61 | if self.closed: |
| 62 | return b"" |
| 63 | return io.BytesIO.readline(self, length) |
| 64 | |
| 65 | def close(self): |
| 66 | self.io_refs -= 1 |
| 67 | if self.io_refs == 0: |
| 68 | io.BytesIO.close(self) |
| 69 | |
| 70 | class FakeHTTPConnection(http.client.HTTPConnection): |
Senthil Kumaran | c5c5a14 | 2012-01-14 19:09:04 +0800 | [diff] [blame] | 71 | |
| 72 | # buffer to store data for verification in urlopen tests. |
| 73 | buf = None |
| 74 | |
Senthil Kumaran | ce26014 | 2011-11-01 01:35:17 +0800 | [diff] [blame] | 75 | def connect(self): |
| 76 | self.sock = FakeSocket(fakedata) |
Senthil Kumaran | c5c5a14 | 2012-01-14 19:09:04 +0800 | [diff] [blame] | 77 | |
Senthil Kumaran | ce26014 | 2011-11-01 01:35:17 +0800 | [diff] [blame] | 78 | self._connection_class = http.client.HTTPConnection |
| 79 | http.client.HTTPConnection = FakeHTTPConnection |
| 80 | |
| 81 | def unfakehttp(self): |
| 82 | http.client.HTTPConnection = self._connection_class |
| 83 | |
| 84 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 85 | class urlopen_FileTests(unittest.TestCase): |
| 86 | """Test urlopen() opening a temporary file. |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 87 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 88 | Try to test as much functionality as possible so as to cut down on reliance |
Andrew M. Kuchling | f1a2f9e | 2004-06-29 13:07:53 +0000 | [diff] [blame] | 89 | on connecting to the Net for testing. |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 90 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 91 | """ |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 92 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 93 | def setUp(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 94 | # Create a temp file to use for testing |
| 95 | self.text = bytes("test_urllib: %s\n" % self.__class__.__name__, |
| 96 | "ascii") |
| 97 | f = open(support.TESTFN, 'wb') |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 98 | try: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 99 | f.write(self.text) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 100 | finally: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 101 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 102 | self.pathname = support.TESTFN |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 103 | self.returned_obj = urlopen("file:%s" % self.pathname) |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 104 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 105 | def tearDown(self): |
| 106 | """Shut down the open object""" |
| 107 | self.returned_obj.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 108 | os.remove(support.TESTFN) |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 109 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 110 | def test_interface(self): |
| 111 | # Make sure object returned by urlopen() has the specified methods |
| 112 | for attr in ("read", "readline", "readlines", "fileno", |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 113 | "close", "info", "geturl", "getcode", "__iter__"): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 114 | self.assertTrue(hasattr(self.returned_obj, attr), |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 115 | "object returned by urlopen() lacks %s attribute" % |
| 116 | attr) |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 117 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 118 | def test_read(self): |
| 119 | self.assertEqual(self.text, self.returned_obj.read()) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 120 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 121 | def test_readline(self): |
| 122 | self.assertEqual(self.text, self.returned_obj.readline()) |
Guido van Rossum | a098294 | 2007-07-10 08:30:03 +0000 | [diff] [blame] | 123 | self.assertEqual(b'', self.returned_obj.readline(), |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 124 | "calling readline() after exhausting the file did not" |
| 125 | " return an empty string") |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 126 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 127 | def test_readlines(self): |
| 128 | lines_list = self.returned_obj.readlines() |
| 129 | self.assertEqual(len(lines_list), 1, |
| 130 | "readlines() returned the wrong number of lines") |
| 131 | self.assertEqual(lines_list[0], self.text, |
| 132 | "readlines() returned improper text") |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 133 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 134 | def test_fileno(self): |
| 135 | file_num = self.returned_obj.fileno() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 136 | self.assertIsInstance(file_num, int, "fileno() did not return an int") |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 137 | self.assertEqual(os.read(file_num, len(self.text)), self.text, |
| 138 | "Reading on the file descriptor returned by fileno() " |
| 139 | "did not return the expected text") |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 140 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 141 | def test_close(self): |
Senthil Kumaran | d91ffca | 2011-03-19 17:25:27 +0800 | [diff] [blame] | 142 | # Test close() by calling it here and then having it be called again |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 143 | # by the tearDown() method for the test |
| 144 | self.returned_obj.close() |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 145 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 146 | def test_info(self): |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 147 | self.assertIsInstance(self.returned_obj.info(), email.message.Message) |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 148 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 149 | def test_geturl(self): |
| 150 | self.assertEqual(self.returned_obj.geturl(), self.pathname) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 151 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 152 | def test_getcode(self): |
Florent Xicluna | 419e384 | 2010-08-08 16:16:07 +0000 | [diff] [blame] | 153 | self.assertIsNone(self.returned_obj.getcode()) |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 154 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 155 | def test_iter(self): |
| 156 | # Test iterator |
| 157 | # Don't need to count number of iterations since test would fail the |
| 158 | # instant it returned anything beyond the first line from the |
Raymond Hettinger | 038018a | 2011-06-26 14:29:35 +0200 | [diff] [blame] | 159 | # comparison. |
| 160 | # Use the iterator in the usual implicit way to test for ticket #4608. |
| 161 | for line in self.returned_obj: |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 162 | self.assertEqual(line, self.text) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 163 | |
Senthil Kumaran | 3800ea9 | 2012-01-21 11:52:48 +0800 | [diff] [blame] | 164 | def test_relativelocalfile(self): |
| 165 | self.assertRaises(ValueError,urllib.request.urlopen,'./' + self.pathname) |
| 166 | |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 167 | class ProxyTests(unittest.TestCase): |
| 168 | |
| 169 | def setUp(self): |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 170 | # Records changes to env vars |
| 171 | self.env = support.EnvironmentVarGuard() |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 172 | # Delete all proxy related env vars |
Antoine Pitrou | b3a88b5 | 2010-10-14 18:31:39 +0000 | [diff] [blame] | 173 | for k in list(os.environ): |
Antoine Pitrou | 8c8f1ac | 2010-10-14 18:32:54 +0000 | [diff] [blame] | 174 | if 'proxy' in k.lower(): |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 175 | self.env.unset(k) |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 176 | |
| 177 | def tearDown(self): |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 178 | # Restore all proxy related env vars |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 179 | self.env.__exit__() |
| 180 | del self.env |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 181 | |
| 182 | def test_getproxies_environment_keep_no_proxies(self): |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 183 | self.env.set('NO_PROXY', 'localhost') |
| 184 | proxies = urllib.request.getproxies_environment() |
| 185 | # getproxies_environment use lowered case truncated (no '_proxy') keys |
Florent Xicluna | 419e384 | 2010-08-08 16:16:07 +0000 | [diff] [blame] | 186 | self.assertEqual('localhost', proxies['no']) |
Senthil Kumaran | 89976f1 | 2011-08-06 12:27:40 +0800 | [diff] [blame] | 187 | # List of no_proxies with space. |
| 188 | self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com') |
| 189 | self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com')) |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 190 | |
Senthil Kumaran | ce26014 | 2011-11-01 01:35:17 +0800 | [diff] [blame] | 191 | class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin): |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 192 | """Test urlopen() opening a fake http connection.""" |
| 193 | |
Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 194 | def check_read(self, ver): |
| 195 | self.fakehttp(b"HTTP/" + ver + b" 200 OK\r\n\r\nHello!") |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 196 | try: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 197 | fp = urlopen("http://python.org/") |
Jeremy Hylton | 66dc8c5 | 2007-08-04 03:42:26 +0000 | [diff] [blame] | 198 | self.assertEqual(fp.readline(), b"Hello!") |
| 199 | self.assertEqual(fp.readline(), b"") |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 200 | self.assertEqual(fp.geturl(), 'http://python.org/') |
| 201 | self.assertEqual(fp.getcode(), 200) |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 202 | finally: |
| 203 | self.unfakehttp() |
| 204 | |
Senthil Kumaran | 2643041 | 2011-04-13 07:01:19 +0800 | [diff] [blame] | 205 | def test_url_fragment(self): |
| 206 | # Issue #11703: geturl() omits fragments in the original URL. |
| 207 | url = 'http://docs.python.org/library/urllib.html#OK' |
Senthil Kumaran | b17abb1 | 2011-04-13 07:22:29 +0800 | [diff] [blame] | 208 | self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!") |
Senthil Kumaran | 2643041 | 2011-04-13 07:01:19 +0800 | [diff] [blame] | 209 | try: |
| 210 | fp = urllib.request.urlopen(url) |
| 211 | self.assertEqual(fp.geturl(), url) |
| 212 | finally: |
| 213 | self.unfakehttp() |
| 214 | |
Senthil Kumaran | d91ffca | 2011-03-19 17:25:27 +0800 | [diff] [blame] | 215 | def test_willclose(self): |
| 216 | self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!") |
Senthil Kumaran | acbaa92 | 2011-03-20 05:30:16 +0800 | [diff] [blame] | 217 | try: |
| 218 | resp = urlopen("http://www.python.org") |
| 219 | self.assertTrue(resp.fp.will_close) |
| 220 | finally: |
| 221 | self.unfakehttp() |
Senthil Kumaran | d91ffca | 2011-03-19 17:25:27 +0800 | [diff] [blame] | 222 | |
Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 223 | def test_read_0_9(self): |
| 224 | # "0.9" response accepted (but not "simple responses" without |
| 225 | # a status line) |
| 226 | self.check_read(b"0.9") |
| 227 | |
| 228 | def test_read_1_0(self): |
| 229 | self.check_read(b"1.0") |
| 230 | |
| 231 | def test_read_1_1(self): |
| 232 | self.check_read(b"1.1") |
| 233 | |
Christian Heimes | 57dddfb | 2008-01-02 18:30:52 +0000 | [diff] [blame] | 234 | def test_read_bogus(self): |
| 235 | # urlopen() should raise IOError for many error codes. |
| 236 | self.fakehttp(b'''HTTP/1.1 401 Authentication Required |
| 237 | Date: Wed, 02 Jan 2008 03:03:54 GMT |
| 238 | Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e |
| 239 | Connection: close |
| 240 | Content-Type: text/html; charset=iso-8859-1 |
| 241 | ''') |
| 242 | try: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 243 | self.assertRaises(IOError, urlopen, "http://python.org/") |
Christian Heimes | 57dddfb | 2008-01-02 18:30:52 +0000 | [diff] [blame] | 244 | finally: |
| 245 | self.unfakehttp() |
| 246 | |
guido@google.com | a119df9 | 2011-03-29 11:41:02 -0700 | [diff] [blame] | 247 | def test_invalid_redirect(self): |
| 248 | # urlopen() should raise IOError for many error codes. |
| 249 | self.fakehttp(b'''HTTP/1.1 302 Found |
| 250 | Date: Wed, 02 Jan 2008 03:03:54 GMT |
| 251 | Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e |
| 252 | Location: file://guidocomputer.athome.com:/python/license |
| 253 | Connection: close |
| 254 | Content-Type: text/html; charset=iso-8859-1 |
| 255 | ''') |
| 256 | try: |
| 257 | self.assertRaises(urllib.error.HTTPError, urlopen, |
| 258 | "http://python.org/") |
| 259 | finally: |
| 260 | self.unfakehttp() |
| 261 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 262 | def test_empty_socket(self): |
Jeremy Hylton | 66dc8c5 | 2007-08-04 03:42:26 +0000 | [diff] [blame] | 263 | # urlopen() raises IOError if the underlying socket does not send any |
| 264 | # data. (#1680230) |
Christian Heimes | 57dddfb | 2008-01-02 18:30:52 +0000 | [diff] [blame] | 265 | self.fakehttp(b'') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 266 | try: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 267 | self.assertRaises(IOError, urlopen, "http://something") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 268 | finally: |
| 269 | self.unfakehttp() |
| 270 | |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 271 | def test_missing_localfile(self): |
| 272 | # Test for #10836 |
Senthil Kumaran | cc2f042 | 2012-10-27 02:48:21 -0700 | [diff] [blame] | 273 | # 3.3 - URLError is not captured, explicit IOError is raised. |
| 274 | with self.assertRaises(IOError): |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 275 | urlopen('file://localhost/a/file/which/doesnot/exists.py') |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 276 | |
| 277 | def test_file_notexists(self): |
| 278 | fd, tmp_file = tempfile.mkstemp() |
| 279 | tmp_fileurl = 'file://localhost/' + tmp_file.replace(os.path.sep, '/') |
| 280 | try: |
| 281 | self.assertTrue(os.path.exists(tmp_file)) |
| 282 | with urlopen(tmp_fileurl) as fobj: |
| 283 | self.assertTrue(fobj) |
| 284 | finally: |
| 285 | os.close(fd) |
| 286 | os.unlink(tmp_file) |
| 287 | self.assertFalse(os.path.exists(tmp_file)) |
Senthil Kumaran | cc2f042 | 2012-10-27 02:48:21 -0700 | [diff] [blame] | 288 | # 3.3 - IOError instead of URLError |
| 289 | with self.assertRaises(IOError): |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 290 | urlopen(tmp_fileurl) |
| 291 | |
| 292 | def test_ftp_nohost(self): |
| 293 | test_ftp_url = 'ftp:///path' |
Senthil Kumaran | cc2f042 | 2012-10-27 02:48:21 -0700 | [diff] [blame] | 294 | # 3.3 - IOError instead of URLError |
| 295 | with self.assertRaises(IOError): |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 296 | urlopen(test_ftp_url) |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 297 | |
| 298 | def test_ftp_nonexisting(self): |
Senthil Kumaran | cc2f042 | 2012-10-27 02:48:21 -0700 | [diff] [blame] | 299 | # 3.3 - IOError instead of URLError |
| 300 | with self.assertRaises(IOError): |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 301 | urlopen('ftp://localhost/a/file/which/doesnot/exists.py') |
Senthil Kumaran | cad7b31 | 2012-10-27 02:26:46 -0700 | [diff] [blame] | 302 | |
| 303 | |
Senthil Kumaran | de0eb24 | 2010-08-01 17:53:37 +0000 | [diff] [blame] | 304 | def test_userpass_inurl(self): |
Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 305 | self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!") |
Senthil Kumaran | de0eb24 | 2010-08-01 17:53:37 +0000 | [diff] [blame] | 306 | try: |
| 307 | fp = urlopen("http://user:pass@python.org/") |
| 308 | self.assertEqual(fp.readline(), b"Hello!") |
| 309 | self.assertEqual(fp.readline(), b"") |
| 310 | self.assertEqual(fp.geturl(), 'http://user:pass@python.org/') |
| 311 | self.assertEqual(fp.getcode(), 200) |
| 312 | finally: |
| 313 | self.unfakehttp() |
| 314 | |
Senthil Kumaran | c5c5a14 | 2012-01-14 19:09:04 +0800 | [diff] [blame] | 315 | def test_userpass_inurl_w_spaces(self): |
| 316 | self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!") |
| 317 | try: |
| 318 | userpass = "a b:c d" |
| 319 | url = "http://{}@python.org/".format(userpass) |
| 320 | fakehttp_wrapper = http.client.HTTPConnection |
| 321 | authorization = ("Authorization: Basic %s\r\n" % |
| 322 | b64encode(userpass.encode("ASCII")).decode("ASCII")) |
| 323 | fp = urlopen(url) |
| 324 | # The authorization header must be in place |
| 325 | self.assertIn(authorization, fakehttp_wrapper.buf.decode("UTF-8")) |
| 326 | self.assertEqual(fp.readline(), b"Hello!") |
| 327 | self.assertEqual(fp.readline(), b"") |
| 328 | # the spaces are quoted in URL so no match |
| 329 | self.assertNotEqual(fp.geturl(), url) |
| 330 | self.assertEqual(fp.getcode(), 200) |
| 331 | finally: |
| 332 | self.unfakehttp() |
| 333 | |
Senthil Kumaran | 38b968b9 | 2012-03-14 13:43:53 -0700 | [diff] [blame] | 334 | def test_URLopener_deprecation(self): |
| 335 | with support.check_warnings(('',DeprecationWarning)): |
Senthil Kumaran | cc2f042 | 2012-10-27 02:48:21 -0700 | [diff] [blame] | 336 | urllib.request.URLopener() |
Senthil Kumaran | 38b968b9 | 2012-03-14 13:43:53 -0700 | [diff] [blame] | 337 | |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 338 | class urlretrieve_FileTests(unittest.TestCase): |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 339 | """Test urllib.urlretrieve() on local files""" |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 340 | |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 341 | def setUp(self): |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 342 | # Create a list of temporary files. Each item in the list is a file |
| 343 | # name (absolute path or relative to the current working directory). |
| 344 | # All files in this list will be deleted in the tearDown method. Note, |
| 345 | # this only helps to makes sure temporary files get deleted, but it |
| 346 | # does nothing about trying to close files that may still be open. It |
| 347 | # is the responsibility of the developer to properly close files even |
| 348 | # when exceptional conditions occur. |
| 349 | self.tempFiles = [] |
| 350 | |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 351 | # Create a temporary file. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 352 | self.registerFileForCleanUp(support.TESTFN) |
Guido van Rossum | a098294 | 2007-07-10 08:30:03 +0000 | [diff] [blame] | 353 | self.text = b'testing urllib.urlretrieve' |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 354 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 355 | FILE = open(support.TESTFN, 'wb') |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 356 | FILE.write(self.text) |
| 357 | FILE.close() |
| 358 | finally: |
| 359 | try: FILE.close() |
| 360 | except: pass |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 361 | |
| 362 | def tearDown(self): |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 363 | # Delete the temporary files. |
| 364 | for each in self.tempFiles: |
| 365 | try: os.remove(each) |
| 366 | except: pass |
| 367 | |
| 368 | def constructLocalFileUrl(self, filePath): |
Victor Stinner | 6c6f851 | 2010-08-07 10:09:35 +0000 | [diff] [blame] | 369 | filePath = os.path.abspath(filePath) |
| 370 | try: |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 371 | filePath.encode("utf-8") |
Victor Stinner | 6c6f851 | 2010-08-07 10:09:35 +0000 | [diff] [blame] | 372 | except UnicodeEncodeError: |
| 373 | raise unittest.SkipTest("filePath is not encodable to utf8") |
| 374 | return "file://%s" % urllib.request.pathname2url(filePath) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 375 | |
Guido van Rossum | 70d0dda | 2007-08-29 01:53:26 +0000 | [diff] [blame] | 376 | def createNewTempFile(self, data=b""): |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 377 | """Creates a new temporary file containing the specified data, |
| 378 | registers the file for deletion during the test fixture tear down, and |
| 379 | returns the absolute path of the file.""" |
| 380 | |
| 381 | newFd, newFilePath = tempfile.mkstemp() |
| 382 | try: |
| 383 | self.registerFileForCleanUp(newFilePath) |
| 384 | newFile = os.fdopen(newFd, "wb") |
| 385 | newFile.write(data) |
| 386 | newFile.close() |
| 387 | finally: |
| 388 | try: newFile.close() |
| 389 | except: pass |
| 390 | return newFilePath |
| 391 | |
| 392 | def registerFileForCleanUp(self, fileName): |
| 393 | self.tempFiles.append(fileName) |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 394 | |
| 395 | def test_basic(self): |
| 396 | # Make sure that a local file just gets its own location returned and |
| 397 | # a headers value is returned. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 398 | result = urllib.request.urlretrieve("file:%s" % support.TESTFN) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 399 | self.assertEqual(result[0], support.TESTFN) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 400 | self.assertIsInstance(result[1], email.message.Message, |
| 401 | "did not get a email.message.Message instance " |
| 402 | "as second returned value") |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 403 | |
| 404 | def test_copy(self): |
| 405 | # Test that setting the filename argument works. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 406 | second_temp = "%s.2" % support.TESTFN |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 407 | self.registerFileForCleanUp(second_temp) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 408 | result = urllib.request.urlretrieve(self.constructLocalFileUrl( |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 409 | support.TESTFN), second_temp) |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 410 | self.assertEqual(second_temp, result[0]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 411 | self.assertTrue(os.path.exists(second_temp), "copy of the file was not " |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 412 | "made") |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 413 | FILE = open(second_temp, 'rb') |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 414 | try: |
| 415 | text = FILE.read() |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 416 | FILE.close() |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 417 | finally: |
| 418 | try: FILE.close() |
| 419 | except: pass |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 420 | self.assertEqual(self.text, text) |
| 421 | |
| 422 | def test_reporthook(self): |
| 423 | # Make sure that the reporthook works. |
Senthil Kumaran | e24f96a | 2012-03-13 19:29:33 -0700 | [diff] [blame] | 424 | def hooktester(block_count, block_read_size, file_size, count_holder=[0]): |
| 425 | self.assertIsInstance(block_count, int) |
| 426 | self.assertIsInstance(block_read_size, int) |
| 427 | self.assertIsInstance(file_size, int) |
| 428 | self.assertEqual(block_count, count_holder[0]) |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 429 | count_holder[0] = count_holder[0] + 1 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 430 | second_temp = "%s.2" % support.TESTFN |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 431 | self.registerFileForCleanUp(second_temp) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 432 | urllib.request.urlretrieve( |
| 433 | self.constructLocalFileUrl(support.TESTFN), |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 434 | second_temp, hooktester) |
| 435 | |
| 436 | def test_reporthook_0_bytes(self): |
| 437 | # Test on zero length file. Should call reporthook only 1 time. |
| 438 | report = [] |
Senthil Kumaran | e24f96a | 2012-03-13 19:29:33 -0700 | [diff] [blame] | 439 | def hooktester(block_count, block_read_size, file_size, _report=report): |
| 440 | _report.append((block_count, block_read_size, file_size)) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 441 | srcFileName = self.createNewTempFile() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 442 | urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName), |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 443 | support.TESTFN, hooktester) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 444 | self.assertEqual(len(report), 1) |
| 445 | self.assertEqual(report[0][2], 0) |
| 446 | |
| 447 | def test_reporthook_5_bytes(self): |
| 448 | # Test on 5 byte file. Should call reporthook only 2 times (once when |
| 449 | # the "network connection" is established and once when the block is |
Senthil Kumaran | e24f96a | 2012-03-13 19:29:33 -0700 | [diff] [blame] | 450 | # read). |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 451 | report = [] |
Senthil Kumaran | e24f96a | 2012-03-13 19:29:33 -0700 | [diff] [blame] | 452 | def hooktester(block_count, block_read_size, file_size, _report=report): |
| 453 | _report.append((block_count, block_read_size, file_size)) |
Guido van Rossum | 70d0dda | 2007-08-29 01:53:26 +0000 | [diff] [blame] | 454 | srcFileName = self.createNewTempFile(b"x" * 5) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 455 | urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName), |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 456 | support.TESTFN, hooktester) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 457 | self.assertEqual(len(report), 2) |
Gregory P. Smith | 6d9388f | 2012-11-10 15:12:55 -0800 | [diff] [blame] | 458 | self.assertEqual(report[0][2], 5) |
| 459 | self.assertEqual(report[1][2], 5) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 460 | |
| 461 | def test_reporthook_8193_bytes(self): |
| 462 | # Test on 8193 byte file. Should call reporthook only 3 times (once |
| 463 | # when the "network connection" is established, once for the next 8192 |
| 464 | # bytes, and once for the last byte). |
| 465 | report = [] |
Senthil Kumaran | e24f96a | 2012-03-13 19:29:33 -0700 | [diff] [blame] | 466 | def hooktester(block_count, block_read_size, file_size, _report=report): |
| 467 | _report.append((block_count, block_read_size, file_size)) |
Guido van Rossum | 70d0dda | 2007-08-29 01:53:26 +0000 | [diff] [blame] | 468 | srcFileName = self.createNewTempFile(b"x" * 8193) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 469 | urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName), |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 470 | support.TESTFN, hooktester) |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 471 | self.assertEqual(len(report), 3) |
Gregory P. Smith | 6d9388f | 2012-11-10 15:12:55 -0800 | [diff] [blame] | 472 | self.assertEqual(report[0][2], 8193) |
| 473 | self.assertEqual(report[0][1], 8192) |
Senthil Kumaran | e24f96a | 2012-03-13 19:29:33 -0700 | [diff] [blame] | 474 | self.assertEqual(report[1][1], 8192) |
Gregory P. Smith | 6d9388f | 2012-11-10 15:12:55 -0800 | [diff] [blame] | 475 | self.assertEqual(report[2][1], 8192) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 476 | |
Senthil Kumaran | ce26014 | 2011-11-01 01:35:17 +0800 | [diff] [blame] | 477 | |
| 478 | class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin): |
| 479 | """Test urllib.urlretrieve() using fake http connections""" |
| 480 | |
| 481 | def test_short_content_raises_ContentTooShortError(self): |
| 482 | self.fakehttp(b'''HTTP/1.1 200 OK |
| 483 | Date: Wed, 02 Jan 2008 03:03:54 GMT |
| 484 | Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e |
| 485 | Connection: close |
| 486 | Content-Length: 100 |
| 487 | Content-Type: text/html; charset=iso-8859-1 |
| 488 | |
| 489 | FF |
| 490 | ''') |
| 491 | |
| 492 | def _reporthook(par1, par2, par3): |
| 493 | pass |
| 494 | |
| 495 | with self.assertRaises(urllib.error.ContentTooShortError): |
| 496 | try: |
| 497 | urllib.request.urlretrieve('http://example.com/', |
| 498 | reporthook=_reporthook) |
| 499 | finally: |
| 500 | self.unfakehttp() |
| 501 | |
| 502 | def test_short_content_raises_ContentTooShortError_without_reporthook(self): |
| 503 | self.fakehttp(b'''HTTP/1.1 200 OK |
| 504 | Date: Wed, 02 Jan 2008 03:03:54 GMT |
| 505 | Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e |
| 506 | Connection: close |
| 507 | Content-Length: 100 |
| 508 | Content-Type: text/html; charset=iso-8859-1 |
| 509 | |
| 510 | FF |
| 511 | ''') |
| 512 | with self.assertRaises(urllib.error.ContentTooShortError): |
| 513 | try: |
| 514 | urllib.request.urlretrieve('http://example.com/') |
| 515 | finally: |
| 516 | self.unfakehttp() |
| 517 | |
| 518 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 519 | class QuotingTests(unittest.TestCase): |
| 520 | """Tests for urllib.quote() and urllib.quote_plus() |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 521 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 522 | According to RFC 2396 (Uniform Resource Identifiers), to escape a |
| 523 | character you write it as '%' + <2 character US-ASCII hex value>. |
| 524 | The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a |
| 525 | character properly. Case does not matter on the hex letters. |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 526 | |
| 527 | The various character sets specified are: |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 528 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 529 | Reserved characters : ";/?:@&=+$," |
| 530 | Have special meaning in URIs and must be escaped if not being used for |
| 531 | their special meaning |
| 532 | Data characters : letters, digits, and "-_.!~*'()" |
| 533 | Unreserved and do not need to be escaped; can be, though, if desired |
| 534 | Control characters : 0x00 - 0x1F, 0x7F |
| 535 | Have no use in URIs so must be escaped |
| 536 | space : 0x20 |
| 537 | Must be escaped |
| 538 | Delimiters : '<>#%"' |
| 539 | Must be escaped |
| 540 | Unwise : "{}|\^[]`" |
| 541 | Must be escaped |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 542 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 543 | """ |
| 544 | |
| 545 | def test_never_quote(self): |
| 546 | # Make sure quote() does not quote letters, digits, and "_,.-" |
| 547 | do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 548 | "abcdefghijklmnopqrstuvwxyz", |
| 549 | "0123456789", |
| 550 | "_.-"]) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 551 | result = urllib.parse.quote(do_not_quote) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 552 | self.assertEqual(do_not_quote, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 553 | "using quote(): %r != %r" % (do_not_quote, result)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 554 | result = urllib.parse.quote_plus(do_not_quote) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 555 | self.assertEqual(do_not_quote, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 556 | "using quote_plus(): %r != %r" % (do_not_quote, result)) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 557 | |
| 558 | def test_default_safe(self): |
| 559 | # Test '/' is default value for 'safe' parameter |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 560 | self.assertEqual(urllib.parse.quote.__defaults__[0], '/') |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 561 | |
| 562 | def test_safe(self): |
| 563 | # Test setting 'safe' parameter does what it should do |
| 564 | quote_by_default = "<>" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 565 | result = urllib.parse.quote(quote_by_default, safe=quote_by_default) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 566 | self.assertEqual(quote_by_default, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 567 | "using quote(): %r != %r" % (quote_by_default, result)) |
Jeremy Hylton | 1ef7c6b | 2009-03-26 16:57:30 +0000 | [diff] [blame] | 568 | result = urllib.parse.quote_plus(quote_by_default, |
| 569 | safe=quote_by_default) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 570 | self.assertEqual(quote_by_default, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 571 | "using quote_plus(): %r != %r" % |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 572 | (quote_by_default, result)) |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 573 | # Safe expressed as bytes rather than str |
| 574 | result = urllib.parse.quote(quote_by_default, safe=b"<>") |
| 575 | self.assertEqual(quote_by_default, result, |
| 576 | "using quote(): %r != %r" % (quote_by_default, result)) |
| 577 | # "Safe" non-ASCII characters should have no effect |
| 578 | # (Since URIs are not allowed to have non-ASCII characters) |
| 579 | result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc") |
| 580 | expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="") |
| 581 | self.assertEqual(expect, result, |
| 582 | "using quote(): %r != %r" % |
| 583 | (expect, result)) |
| 584 | # Same as above, but using a bytes rather than str |
| 585 | result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc") |
| 586 | expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="") |
| 587 | self.assertEqual(expect, result, |
| 588 | "using quote(): %r != %r" % |
| 589 | (expect, result)) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 590 | |
| 591 | def test_default_quoting(self): |
| 592 | # Make sure all characters that should be quoted are by default sans |
| 593 | # space (separate test for that). |
| 594 | should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F |
| 595 | should_quote.append('<>#%"{}|\^[]`') |
| 596 | should_quote.append(chr(127)) # For 0x7F |
| 597 | should_quote = ''.join(should_quote) |
| 598 | for char in should_quote: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 599 | result = urllib.parse.quote(char) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 600 | self.assertEqual(hexescape(char), result, |
Jeremy Hylton | 1ef7c6b | 2009-03-26 16:57:30 +0000 | [diff] [blame] | 601 | "using quote(): " |
| 602 | "%s should be escaped to %s, not %s" % |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 603 | (char, hexescape(char), result)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 604 | result = urllib.parse.quote_plus(char) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 605 | self.assertEqual(hexescape(char), result, |
| 606 | "using quote_plus(): " |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 607 | "%s should be escapes to %s, not %s" % |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 608 | (char, hexescape(char), result)) |
| 609 | del should_quote |
| 610 | partial_quote = "ab[]cd" |
| 611 | expected = "ab%5B%5Dcd" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 612 | result = urllib.parse.quote(partial_quote) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 613 | self.assertEqual(expected, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 614 | "using quote(): %r != %r" % (expected, result)) |
Senthil Kumaran | 305a68e | 2011-09-13 06:40:27 +0800 | [diff] [blame] | 615 | result = urllib.parse.quote_plus(partial_quote) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 616 | self.assertEqual(expected, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 617 | "using quote_plus(): %r != %r" % (expected, result)) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 618 | |
| 619 | def test_quoting_space(self): |
| 620 | # Make sure quote() and quote_plus() handle spaces as specified in |
| 621 | # their unique way |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 622 | result = urllib.parse.quote(' ') |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 623 | self.assertEqual(result, hexescape(' '), |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 624 | "using quote(): %r != %r" % (result, hexescape(' '))) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 625 | result = urllib.parse.quote_plus(' ') |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 626 | self.assertEqual(result, '+', |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 627 | "using quote_plus(): %r != +" % result) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 628 | given = "a b cd e f" |
| 629 | expect = given.replace(' ', hexescape(' ')) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 630 | result = urllib.parse.quote(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 631 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 632 | "using quote(): %r != %r" % (expect, result)) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 633 | expect = given.replace(' ', '+') |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 634 | result = urllib.parse.quote_plus(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 635 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 636 | "using quote_plus(): %r != %r" % (expect, result)) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 637 | |
Raymond Hettinger | 2bdec7b | 2005-09-10 14:30:09 +0000 | [diff] [blame] | 638 | def test_quoting_plus(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 639 | self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'), |
Raymond Hettinger | 2bdec7b | 2005-09-10 14:30:09 +0000 | [diff] [blame] | 640 | 'alpha%2Bbeta+gamma') |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 641 | self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'), |
Raymond Hettinger | 2bdec7b | 2005-09-10 14:30:09 +0000 | [diff] [blame] | 642 | 'alpha+beta+gamma') |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 643 | # Test with bytes |
| 644 | self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'), |
| 645 | 'alpha%2Bbeta+gamma') |
| 646 | # Test with safe bytes |
| 647 | self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'), |
| 648 | 'alpha+beta+gamma') |
| 649 | |
| 650 | def test_quote_bytes(self): |
| 651 | # Bytes should quote directly to percent-encoded values |
| 652 | given = b"\xa2\xd8ab\xff" |
| 653 | expect = "%A2%D8ab%FF" |
| 654 | result = urllib.parse.quote(given) |
| 655 | self.assertEqual(expect, result, |
| 656 | "using quote(): %r != %r" % (expect, result)) |
| 657 | # Encoding argument should raise type error on bytes input |
| 658 | self.assertRaises(TypeError, urllib.parse.quote, given, |
| 659 | encoding="latin-1") |
| 660 | # quote_from_bytes should work the same |
| 661 | result = urllib.parse.quote_from_bytes(given) |
| 662 | self.assertEqual(expect, result, |
| 663 | "using quote_from_bytes(): %r != %r" |
| 664 | % (expect, result)) |
| 665 | |
| 666 | def test_quote_with_unicode(self): |
| 667 | # Characters in Latin-1 range, encoded by default in UTF-8 |
| 668 | given = "\xa2\xd8ab\xff" |
| 669 | expect = "%C2%A2%C3%98ab%C3%BF" |
| 670 | result = urllib.parse.quote(given) |
| 671 | self.assertEqual(expect, result, |
| 672 | "using quote(): %r != %r" % (expect, result)) |
| 673 | # Characters in Latin-1 range, encoded by with None (default) |
| 674 | result = urllib.parse.quote(given, encoding=None, errors=None) |
| 675 | self.assertEqual(expect, result, |
| 676 | "using quote(): %r != %r" % (expect, result)) |
| 677 | # Characters in Latin-1 range, encoded with Latin-1 |
| 678 | given = "\xa2\xd8ab\xff" |
| 679 | expect = "%A2%D8ab%FF" |
| 680 | result = urllib.parse.quote(given, encoding="latin-1") |
| 681 | self.assertEqual(expect, result, |
| 682 | "using quote(): %r != %r" % (expect, result)) |
| 683 | # Characters in BMP, encoded by default in UTF-8 |
| 684 | given = "\u6f22\u5b57" # "Kanji" |
| 685 | expect = "%E6%BC%A2%E5%AD%97" |
| 686 | result = urllib.parse.quote(given) |
| 687 | self.assertEqual(expect, result, |
| 688 | "using quote(): %r != %r" % (expect, result)) |
| 689 | # Characters in BMP, encoded with Latin-1 |
| 690 | given = "\u6f22\u5b57" |
| 691 | self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given, |
| 692 | encoding="latin-1") |
| 693 | # Characters in BMP, encoded with Latin-1, with replace error handling |
| 694 | given = "\u6f22\u5b57" |
| 695 | expect = "%3F%3F" # "??" |
| 696 | result = urllib.parse.quote(given, encoding="latin-1", |
| 697 | errors="replace") |
| 698 | self.assertEqual(expect, result, |
| 699 | "using quote(): %r != %r" % (expect, result)) |
| 700 | # Characters in BMP, Latin-1, with xmlcharref error handling |
| 701 | given = "\u6f22\u5b57" |
| 702 | expect = "%26%2328450%3B%26%2323383%3B" # "漢字" |
| 703 | result = urllib.parse.quote(given, encoding="latin-1", |
| 704 | errors="xmlcharrefreplace") |
| 705 | self.assertEqual(expect, result, |
| 706 | "using quote(): %r != %r" % (expect, result)) |
Raymond Hettinger | 2bdec7b | 2005-09-10 14:30:09 +0000 | [diff] [blame] | 707 | |
Georg Brandl | faf4149 | 2009-05-26 18:31:11 +0000 | [diff] [blame] | 708 | def test_quote_plus_with_unicode(self): |
| 709 | # Encoding (latin-1) test for quote_plus |
| 710 | given = "\xa2\xd8 \xff" |
| 711 | expect = "%A2%D8+%FF" |
| 712 | result = urllib.parse.quote_plus(given, encoding="latin-1") |
| 713 | self.assertEqual(expect, result, |
| 714 | "using quote_plus(): %r != %r" % (expect, result)) |
| 715 | # Errors test for quote_plus |
| 716 | given = "ab\u6f22\u5b57 cd" |
| 717 | expect = "ab%3F%3F+cd" |
| 718 | result = urllib.parse.quote_plus(given, encoding="latin-1", |
| 719 | errors="replace") |
| 720 | self.assertEqual(expect, result, |
| 721 | "using quote_plus(): %r != %r" % (expect, result)) |
| 722 | |
Senthil Kumaran | d496c4c | 2010-07-30 19:34:36 +0000 | [diff] [blame] | 723 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 724 | class UnquotingTests(unittest.TestCase): |
| 725 | """Tests for unquote() and unquote_plus() |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 726 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 727 | See the doc string for quoting_Tests for details on quoting and such. |
| 728 | |
| 729 | """ |
| 730 | |
| 731 | def test_unquoting(self): |
| 732 | # Make sure unquoting of all ASCII values works |
| 733 | escape_list = [] |
| 734 | for num in range(128): |
| 735 | given = hexescape(chr(num)) |
| 736 | expect = chr(num) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 737 | result = urllib.parse.unquote(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 738 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 739 | "using unquote(): %r != %r" % (expect, result)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 740 | result = urllib.parse.unquote_plus(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 741 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 742 | "using unquote_plus(): %r != %r" % |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 743 | (expect, result)) |
| 744 | escape_list.append(given) |
| 745 | escape_string = ''.join(escape_list) |
| 746 | del escape_list |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 747 | result = urllib.parse.unquote(escape_string) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 748 | self.assertEqual(result.count('%'), 1, |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 749 | "using unquote(): not all characters escaped: " |
| 750 | "%s" % result) |
Georg Brandl | 604ef37 | 2010-07-31 08:20:02 +0000 | [diff] [blame] | 751 | self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None) |
| 752 | self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ()) |
Florent Xicluna | 62829dc | 2010-08-14 20:51:58 +0000 | [diff] [blame] | 753 | with support.check_warnings(('', BytesWarning), quiet=True): |
| 754 | self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'') |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 755 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 756 | def test_unquoting_badpercent(self): |
| 757 | # Test unquoting on bad percent-escapes |
| 758 | given = '%xab' |
| 759 | expect = given |
| 760 | result = urllib.parse.unquote(given) |
| 761 | self.assertEqual(expect, result, "using unquote(): %r != %r" |
| 762 | % (expect, result)) |
| 763 | given = '%x' |
| 764 | expect = given |
| 765 | result = urllib.parse.unquote(given) |
| 766 | self.assertEqual(expect, result, "using unquote(): %r != %r" |
| 767 | % (expect, result)) |
| 768 | given = '%' |
| 769 | expect = given |
| 770 | result = urllib.parse.unquote(given) |
| 771 | self.assertEqual(expect, result, "using unquote(): %r != %r" |
| 772 | % (expect, result)) |
| 773 | # unquote_to_bytes |
| 774 | given = '%xab' |
| 775 | expect = bytes(given, 'ascii') |
| 776 | result = urllib.parse.unquote_to_bytes(given) |
| 777 | self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r" |
| 778 | % (expect, result)) |
| 779 | given = '%x' |
| 780 | expect = bytes(given, 'ascii') |
| 781 | result = urllib.parse.unquote_to_bytes(given) |
| 782 | self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r" |
| 783 | % (expect, result)) |
| 784 | given = '%' |
| 785 | expect = bytes(given, 'ascii') |
| 786 | result = urllib.parse.unquote_to_bytes(given) |
| 787 | self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r" |
| 788 | % (expect, result)) |
Georg Brandl | 604ef37 | 2010-07-31 08:20:02 +0000 | [diff] [blame] | 789 | self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, None) |
| 790 | self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, ()) |
Senthil Kumaran | 79e17f6 | 2010-07-19 18:17:19 +0000 | [diff] [blame] | 791 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 792 | def test_unquoting_mixed_case(self): |
| 793 | # Test unquoting on mixed-case hex digits in the percent-escapes |
| 794 | given = '%Ab%eA' |
| 795 | expect = b'\xab\xea' |
| 796 | result = urllib.parse.unquote_to_bytes(given) |
| 797 | self.assertEqual(expect, result, |
| 798 | "using unquote_to_bytes(): %r != %r" |
| 799 | % (expect, result)) |
| 800 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 801 | def test_unquoting_parts(self): |
| 802 | # Make sure unquoting works when have non-quoted characters |
| 803 | # interspersed |
| 804 | given = 'ab%sd' % hexescape('c') |
| 805 | expect = "abcd" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 806 | result = urllib.parse.unquote(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 807 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 808 | "using quote(): %r != %r" % (expect, result)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 809 | result = urllib.parse.unquote_plus(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 810 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 811 | "using unquote_plus(): %r != %r" % (expect, result)) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 812 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 813 | def test_unquoting_plus(self): |
| 814 | # Test difference between unquote() and unquote_plus() |
| 815 | given = "are+there+spaces..." |
| 816 | expect = given |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 817 | result = urllib.parse.unquote(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 818 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 819 | "using unquote(): %r != %r" % (expect, result)) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 820 | expect = given.replace('+', ' ') |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 821 | result = urllib.parse.unquote_plus(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 822 | self.assertEqual(expect, result, |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 823 | "using unquote_plus(): %r != %r" % (expect, result)) |
| 824 | |
| 825 | def test_unquote_to_bytes(self): |
| 826 | given = 'br%C3%BCckner_sapporo_20050930.doc' |
| 827 | expect = b'br\xc3\xbcckner_sapporo_20050930.doc' |
| 828 | result = urllib.parse.unquote_to_bytes(given) |
| 829 | self.assertEqual(expect, result, |
| 830 | "using unquote_to_bytes(): %r != %r" |
| 831 | % (expect, result)) |
| 832 | # Test on a string with unescaped non-ASCII characters |
| 833 | # (Technically an invalid URI; expect those characters to be UTF-8 |
| 834 | # encoded). |
| 835 | result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC") |
| 836 | expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc" |
| 837 | self.assertEqual(expect, result, |
| 838 | "using unquote_to_bytes(): %r != %r" |
| 839 | % (expect, result)) |
| 840 | # Test with a bytes as input |
| 841 | given = b'%A2%D8ab%FF' |
| 842 | expect = b'\xa2\xd8ab\xff' |
| 843 | result = urllib.parse.unquote_to_bytes(given) |
| 844 | self.assertEqual(expect, result, |
| 845 | "using unquote_to_bytes(): %r != %r" |
| 846 | % (expect, result)) |
| 847 | # Test with a bytes as input, with unescaped non-ASCII bytes |
| 848 | # (Technically an invalid URI; expect those bytes to be preserved) |
| 849 | given = b'%A2\xd8ab%FF' |
| 850 | expect = b'\xa2\xd8ab\xff' |
| 851 | result = urllib.parse.unquote_to_bytes(given) |
| 852 | self.assertEqual(expect, result, |
| 853 | "using unquote_to_bytes(): %r != %r" |
| 854 | % (expect, result)) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 855 | |
Raymond Hettinger | 4b0f20d | 2005-10-15 16:41:53 +0000 | [diff] [blame] | 856 | def test_unquote_with_unicode(self): |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 857 | # Characters in the Latin-1 range, encoded with UTF-8 |
| 858 | given = 'br%C3%BCckner_sapporo_20050930.doc' |
| 859 | expect = 'br\u00fcckner_sapporo_20050930.doc' |
| 860 | result = urllib.parse.unquote(given) |
| 861 | self.assertEqual(expect, result, |
| 862 | "using unquote(): %r != %r" % (expect, result)) |
| 863 | # Characters in the Latin-1 range, encoded with None (default) |
| 864 | result = urllib.parse.unquote(given, encoding=None, errors=None) |
| 865 | self.assertEqual(expect, result, |
| 866 | "using unquote(): %r != %r" % (expect, result)) |
| 867 | |
| 868 | # Characters in the Latin-1 range, encoded with Latin-1 |
| 869 | result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc', |
| 870 | encoding="latin-1") |
| 871 | expect = 'br\u00fcckner_sapporo_20050930.doc' |
| 872 | self.assertEqual(expect, result, |
| 873 | "using unquote(): %r != %r" % (expect, result)) |
| 874 | |
| 875 | # Characters in BMP, encoded with UTF-8 |
| 876 | given = "%E6%BC%A2%E5%AD%97" |
| 877 | expect = "\u6f22\u5b57" # "Kanji" |
| 878 | result = urllib.parse.unquote(given) |
| 879 | self.assertEqual(expect, result, |
| 880 | "using unquote(): %r != %r" % (expect, result)) |
| 881 | |
| 882 | # Decode with UTF-8, invalid sequence |
| 883 | given = "%F3%B1" |
| 884 | expect = "\ufffd" # Replacement character |
| 885 | result = urllib.parse.unquote(given) |
| 886 | self.assertEqual(expect, result, |
| 887 | "using unquote(): %r != %r" % (expect, result)) |
| 888 | |
| 889 | # Decode with UTF-8, invalid sequence, replace errors |
| 890 | result = urllib.parse.unquote(given, errors="replace") |
| 891 | self.assertEqual(expect, result, |
| 892 | "using unquote(): %r != %r" % (expect, result)) |
| 893 | |
| 894 | # Decode with UTF-8, invalid sequence, ignoring errors |
| 895 | given = "%F3%B1" |
| 896 | expect = "" |
| 897 | result = urllib.parse.unquote(given, errors="ignore") |
| 898 | self.assertEqual(expect, result, |
| 899 | "using unquote(): %r != %r" % (expect, result)) |
| 900 | |
| 901 | # A mix of non-ASCII and percent-encoded characters, UTF-8 |
| 902 | result = urllib.parse.unquote("\u6f22%C3%BC") |
| 903 | expect = '\u6f22\u00fc' |
| 904 | self.assertEqual(expect, result, |
| 905 | "using unquote(): %r != %r" % (expect, result)) |
| 906 | |
| 907 | # A mix of non-ASCII and percent-encoded characters, Latin-1 |
| 908 | # (Note, the string contains non-Latin-1-representable characters) |
| 909 | result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1") |
| 910 | expect = '\u6f22\u00fc' |
| 911 | self.assertEqual(expect, result, |
| 912 | "using unquote(): %r != %r" % (expect, result)) |
Raymond Hettinger | 4b0f20d | 2005-10-15 16:41:53 +0000 | [diff] [blame] | 913 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 914 | class urlencode_Tests(unittest.TestCase): |
| 915 | """Tests for urlencode()""" |
| 916 | |
| 917 | def help_inputtype(self, given, test_type): |
| 918 | """Helper method for testing different input types. |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 919 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 920 | 'given' must lead to only the pairs: |
| 921 | * 1st, 1 |
| 922 | * 2nd, 2 |
| 923 | * 3rd, 3 |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 924 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 925 | Test cannot assume anything about order. Docs make no guarantee and |
| 926 | have possible dictionary input. |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 927 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 928 | """ |
| 929 | expect_somewhere = ["1st=1", "2nd=2", "3rd=3"] |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 930 | result = urllib.parse.urlencode(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 931 | for expected in expect_somewhere: |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 932 | self.assertIn(expected, result, |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 933 | "testing %s: %s not found in %s" % |
| 934 | (test_type, expected, result)) |
| 935 | self.assertEqual(result.count('&'), 2, |
| 936 | "testing %s: expected 2 '&'s; got %s" % |
| 937 | (test_type, result.count('&'))) |
| 938 | amp_location = result.index('&') |
| 939 | on_amp_left = result[amp_location - 1] |
| 940 | on_amp_right = result[amp_location + 1] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 941 | self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(), |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 942 | "testing %s: '&' not located in proper place in %s" % |
| 943 | (test_type, result)) |
| 944 | self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps |
| 945 | "testing %s: " |
| 946 | "unexpected number of characters: %s != %s" % |
| 947 | (test_type, len(result), (5 * 3) + 2)) |
| 948 | |
| 949 | def test_using_mapping(self): |
| 950 | # Test passing in a mapping object as an argument. |
| 951 | self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'}, |
| 952 | "using dict as input type") |
| 953 | |
| 954 | def test_using_sequence(self): |
| 955 | # Test passing in a sequence of two-item sequences as an argument. |
| 956 | self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')], |
| 957 | "using sequence of two-item tuples as input") |
| 958 | |
| 959 | def test_quoting(self): |
| 960 | # Make sure keys and values are quoted using quote_plus() |
| 961 | given = {"&":"="} |
| 962 | expect = "%s=%s" % (hexescape('&'), hexescape('=')) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 963 | result = urllib.parse.urlencode(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 964 | self.assertEqual(expect, result) |
| 965 | given = {"key name":"A bunch of pluses"} |
| 966 | expect = "key+name=A+bunch+of+pluses" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 967 | result = urllib.parse.urlencode(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 968 | self.assertEqual(expect, result) |
| 969 | |
| 970 | def test_doseq(self): |
| 971 | # Test that passing True for 'doseq' parameter works correctly |
| 972 | given = {'sequence':['1', '2', '3']} |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 973 | expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3'])) |
| 974 | result = urllib.parse.urlencode(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 975 | self.assertEqual(expect, result) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 976 | result = urllib.parse.urlencode(given, True) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 977 | for value in given["sequence"]: |
| 978 | expect = "sequence=%s" % value |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 979 | self.assertIn(expect, result) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 980 | self.assertEqual(result.count('&'), 2, |
| 981 | "Expected 2 '&'s, got %s" % result.count('&')) |
| 982 | |
Jeremy Hylton | 1ef7c6b | 2009-03-26 16:57:30 +0000 | [diff] [blame] | 983 | def test_empty_sequence(self): |
| 984 | self.assertEqual("", urllib.parse.urlencode({})) |
| 985 | self.assertEqual("", urllib.parse.urlencode([])) |
| 986 | |
| 987 | def test_nonstring_values(self): |
| 988 | self.assertEqual("a=1", urllib.parse.urlencode({"a": 1})) |
| 989 | self.assertEqual("a=None", urllib.parse.urlencode({"a": None})) |
| 990 | |
| 991 | def test_nonstring_seq_values(self): |
| 992 | self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True)) |
| 993 | self.assertEqual("a=None&a=a", |
| 994 | urllib.parse.urlencode({"a": [None, "a"]}, True)) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 995 | data = collections.OrderedDict([("a", 1), ("b", 1)]) |
Jeremy Hylton | 1ef7c6b | 2009-03-26 16:57:30 +0000 | [diff] [blame] | 996 | self.assertEqual("a=a&a=b", |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 997 | urllib.parse.urlencode({"a": data}, True)) |
Jeremy Hylton | 1ef7c6b | 2009-03-26 16:57:30 +0000 | [diff] [blame] | 998 | |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 999 | def test_urlencode_encoding(self): |
| 1000 | # ASCII encoding. Expect %3F with errors="replace' |
| 1001 | given = (('\u00a0', '\u00c1'),) |
| 1002 | expect = '%3F=%3F' |
| 1003 | result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace") |
| 1004 | self.assertEqual(expect, result) |
| 1005 | |
| 1006 | # Default is UTF-8 encoding. |
| 1007 | given = (('\u00a0', '\u00c1'),) |
| 1008 | expect = '%C2%A0=%C3%81' |
| 1009 | result = urllib.parse.urlencode(given) |
| 1010 | self.assertEqual(expect, result) |
| 1011 | |
| 1012 | # Latin-1 encoding. |
| 1013 | given = (('\u00a0', '\u00c1'),) |
| 1014 | expect = '%A0=%C1' |
| 1015 | result = urllib.parse.urlencode(given, encoding="latin-1") |
| 1016 | self.assertEqual(expect, result) |
| 1017 | |
| 1018 | def test_urlencode_encoding_doseq(self): |
| 1019 | # ASCII Encoding. Expect %3F with errors="replace' |
| 1020 | given = (('\u00a0', '\u00c1'),) |
| 1021 | expect = '%3F=%3F' |
| 1022 | result = urllib.parse.urlencode(given, doseq=True, |
| 1023 | encoding="ASCII", errors="replace") |
| 1024 | self.assertEqual(expect, result) |
| 1025 | |
| 1026 | # ASCII Encoding. On a sequence of values. |
| 1027 | given = (("\u00a0", (1, "\u00c1")),) |
| 1028 | expect = '%3F=1&%3F=%3F' |
| 1029 | result = urllib.parse.urlencode(given, True, |
| 1030 | encoding="ASCII", errors="replace") |
| 1031 | self.assertEqual(expect, result) |
| 1032 | |
| 1033 | # Utf-8 |
| 1034 | given = (("\u00a0", "\u00c1"),) |
| 1035 | expect = '%C2%A0=%C3%81' |
| 1036 | result = urllib.parse.urlencode(given, True) |
| 1037 | self.assertEqual(expect, result) |
| 1038 | |
| 1039 | given = (("\u00a0", (42, "\u00c1")),) |
| 1040 | expect = '%C2%A0=42&%C2%A0=%C3%81' |
| 1041 | result = urllib.parse.urlencode(given, True) |
| 1042 | self.assertEqual(expect, result) |
| 1043 | |
| 1044 | # latin-1 |
| 1045 | given = (("\u00a0", "\u00c1"),) |
| 1046 | expect = '%A0=%C1' |
| 1047 | result = urllib.parse.urlencode(given, True, encoding="latin-1") |
| 1048 | self.assertEqual(expect, result) |
| 1049 | |
| 1050 | given = (("\u00a0", (42, "\u00c1")),) |
| 1051 | expect = '%A0=42&%A0=%C1' |
| 1052 | result = urllib.parse.urlencode(given, True, encoding="latin-1") |
| 1053 | self.assertEqual(expect, result) |
| 1054 | |
| 1055 | def test_urlencode_bytes(self): |
| 1056 | given = ((b'\xa0\x24', b'\xc1\x24'),) |
| 1057 | expect = '%A0%24=%C1%24' |
| 1058 | result = urllib.parse.urlencode(given) |
| 1059 | self.assertEqual(expect, result) |
| 1060 | result = urllib.parse.urlencode(given, True) |
| 1061 | self.assertEqual(expect, result) |
| 1062 | |
| 1063 | # Sequence of values |
| 1064 | given = ((b'\xa0\x24', (42, b'\xc1\x24')),) |
| 1065 | expect = '%A0%24=42&%A0%24=%C1%24' |
| 1066 | result = urllib.parse.urlencode(given, True) |
| 1067 | self.assertEqual(expect, result) |
| 1068 | |
| 1069 | def test_urlencode_encoding_safe_parameter(self): |
| 1070 | |
| 1071 | # Send '$' (\x24) as safe character |
| 1072 | # Default utf-8 encoding |
| 1073 | |
| 1074 | given = ((b'\xa0\x24', b'\xc1\x24'),) |
| 1075 | result = urllib.parse.urlencode(given, safe=":$") |
| 1076 | expect = '%A0$=%C1$' |
| 1077 | self.assertEqual(expect, result) |
| 1078 | |
| 1079 | given = ((b'\xa0\x24', b'\xc1\x24'),) |
| 1080 | result = urllib.parse.urlencode(given, doseq=True, safe=":$") |
| 1081 | expect = '%A0$=%C1$' |
| 1082 | self.assertEqual(expect, result) |
| 1083 | |
| 1084 | # Safe parameter in sequence |
| 1085 | given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),) |
| 1086 | expect = '%A0$=%C1$&%A0$=13&%A0$=42' |
| 1087 | result = urllib.parse.urlencode(given, True, safe=":$") |
| 1088 | self.assertEqual(expect, result) |
| 1089 | |
| 1090 | # Test all above in latin-1 encoding |
| 1091 | |
| 1092 | given = ((b'\xa0\x24', b'\xc1\x24'),) |
| 1093 | result = urllib.parse.urlencode(given, safe=":$", |
| 1094 | encoding="latin-1") |
| 1095 | expect = '%A0$=%C1$' |
| 1096 | self.assertEqual(expect, result) |
| 1097 | |
| 1098 | given = ((b'\xa0\x24', b'\xc1\x24'),) |
| 1099 | expect = '%A0$=%C1$' |
| 1100 | result = urllib.parse.urlencode(given, doseq=True, safe=":$", |
| 1101 | encoding="latin-1") |
| 1102 | |
| 1103 | given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),) |
| 1104 | expect = '%A0$=%C1$&%A0$=13&%A0$=42' |
| 1105 | result = urllib.parse.urlencode(given, True, safe=":$", |
| 1106 | encoding="latin-1") |
| 1107 | self.assertEqual(expect, result) |
| 1108 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1109 | class Pathname_Tests(unittest.TestCase): |
| 1110 | """Test pathname2url() and url2pathname()""" |
| 1111 | |
| 1112 | def test_basic(self): |
| 1113 | # Make sure simple tests pass |
| 1114 | expected_path = os.path.join("parts", "of", "a", "path") |
| 1115 | expected_url = "parts/of/a/path" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1116 | result = urllib.request.pathname2url(expected_path) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1117 | self.assertEqual(expected_url, result, |
| 1118 | "pathname2url() failed; %s != %s" % |
| 1119 | (result, expected_url)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1120 | result = urllib.request.url2pathname(expected_url) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1121 | self.assertEqual(expected_path, result, |
| 1122 | "url2pathame() failed; %s != %s" % |
| 1123 | (result, expected_path)) |
| 1124 | |
| 1125 | def test_quoting(self): |
| 1126 | # Test automatic quoting and unquoting works for pathnam2url() and |
| 1127 | # url2pathname() respectively |
| 1128 | given = os.path.join("needs", "quot=ing", "here") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1129 | expect = "needs/%s/here" % urllib.parse.quote("quot=ing") |
| 1130 | result = urllib.request.pathname2url(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1131 | self.assertEqual(expect, result, |
| 1132 | "pathname2url() failed; %s != %s" % |
| 1133 | (expect, result)) |
| 1134 | expect = given |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1135 | result = urllib.request.url2pathname(result) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1136 | self.assertEqual(expect, result, |
| 1137 | "url2pathname() failed; %s != %s" % |
| 1138 | (expect, result)) |
| 1139 | given = os.path.join("make sure", "using_quote") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1140 | expect = "%s/using_quote" % urllib.parse.quote("make sure") |
| 1141 | result = urllib.request.pathname2url(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1142 | self.assertEqual(expect, result, |
| 1143 | "pathname2url() failed; %s != %s" % |
| 1144 | (expect, result)) |
| 1145 | given = "make+sure/using_unquote" |
| 1146 | expect = os.path.join("make+sure", "using_unquote") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1147 | result = urllib.request.url2pathname(given) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1148 | self.assertEqual(expect, result, |
| 1149 | "url2pathname() failed; %s != %s" % |
| 1150 | (expect, result)) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 1151 | |
Senthil Kumaran | 2d2ea1b | 2011-04-14 13:16:30 +0800 | [diff] [blame] | 1152 | @unittest.skipUnless(sys.platform == 'win32', |
| 1153 | 'test specific to the urllib.url2path function.') |
| 1154 | def test_ntpath(self): |
| 1155 | given = ('/C:/', '///C:/', '/C|//') |
| 1156 | expect = 'C:\\' |
| 1157 | for url in given: |
| 1158 | result = urllib.request.url2pathname(url) |
| 1159 | self.assertEqual(expect, result, |
| 1160 | 'urllib.request..url2pathname() failed; %s != %s' % |
| 1161 | (expect, result)) |
| 1162 | given = '///C|/path' |
| 1163 | expect = 'C:\\path' |
| 1164 | result = urllib.request.url2pathname(given) |
| 1165 | self.assertEqual(expect, result, |
| 1166 | 'urllib.request.url2pathname() failed; %s != %s' % |
| 1167 | (expect, result)) |
| 1168 | |
Senthil Kumaran | eaaec27 | 2009-03-30 21:54:41 +0000 | [diff] [blame] | 1169 | class Utility_Tests(unittest.TestCase): |
| 1170 | """Testcase to test the various utility functions in the urllib.""" |
| 1171 | |
| 1172 | def test_splitpasswd(self): |
| 1173 | """Some of password examples are not sensible, but it is added to |
| 1174 | confirming to RFC2617 and addressing issue4675. |
| 1175 | """ |
| 1176 | self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab')) |
| 1177 | self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb')) |
| 1178 | self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb')) |
| 1179 | self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb')) |
| 1180 | self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb')) |
| 1181 | self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb')) |
| 1182 | self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b')) |
Senthil Kumaran | c5c5a14 | 2012-01-14 19:09:04 +0800 | [diff] [blame] | 1183 | self.assertEqual(('user', 'a b'),urllib.parse.splitpasswd('user:a b')) |
| 1184 | self.assertEqual(('user 2', 'ab'),urllib.parse.splitpasswd('user 2:ab')) |
| 1185 | self.assertEqual(('user+1', 'a+b'),urllib.parse.splitpasswd('user+1:a+b')) |
Senthil Kumaran | eaaec27 | 2009-03-30 21:54:41 +0000 | [diff] [blame] | 1186 | |
Senthil Kumaran | 1b7da51 | 2011-10-06 00:32:02 +0800 | [diff] [blame] | 1187 | def test_thishost(self): |
| 1188 | """Test the urllib.request.thishost utility function returns a tuple""" |
| 1189 | self.assertIsInstance(urllib.request.thishost(), tuple) |
| 1190 | |
Senthil Kumaran | 690ce9b | 2009-05-05 18:41:13 +0000 | [diff] [blame] | 1191 | |
| 1192 | class URLopener_Tests(unittest.TestCase): |
| 1193 | """Testcase to test the open method of URLopener class.""" |
| 1194 | |
| 1195 | def test_quoted_open(self): |
| 1196 | class DummyURLopener(urllib.request.URLopener): |
| 1197 | def open_spam(self, url): |
| 1198 | return url |
| 1199 | |
| 1200 | self.assertEqual(DummyURLopener().open( |
| 1201 | 'spam://example/ /'),'//example/%20/') |
| 1202 | |
Senthil Kumaran | 734f059 | 2010-02-20 22:19:04 +0000 | [diff] [blame] | 1203 | # test the safe characters are not quoted by urlopen |
| 1204 | self.assertEqual(DummyURLopener().open( |
| 1205 | "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"), |
| 1206 | "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/") |
| 1207 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1208 | # Just commented them out. |
| 1209 | # Can't really tell why keep failing in windows and sparc. |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1210 | # Everywhere else they work ok, but on those machines, sometimes |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1211 | # fail in one of the tests, sometimes in other. I have a linux, and |
| 1212 | # the tests go ok. |
| 1213 | # If anybody has one of the problematic enviroments, please help! |
| 1214 | # . Facundo |
| 1215 | # |
| 1216 | # def server(evt): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1217 | # import socket, time |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1218 | # serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1219 | # serv.settimeout(3) |
| 1220 | # serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1221 | # serv.bind(("", 9093)) |
| 1222 | # serv.listen(5) |
| 1223 | # try: |
| 1224 | # conn, addr = serv.accept() |
| 1225 | # conn.send("1 Hola mundo\n") |
| 1226 | # cantdata = 0 |
| 1227 | # while cantdata < 13: |
| 1228 | # data = conn.recv(13-cantdata) |
| 1229 | # cantdata += len(data) |
| 1230 | # time.sleep(.3) |
| 1231 | # conn.send("2 No more lines\n") |
| 1232 | # conn.close() |
| 1233 | # except socket.timeout: |
| 1234 | # pass |
| 1235 | # finally: |
| 1236 | # serv.close() |
| 1237 | # evt.set() |
| 1238 | # |
| 1239 | # class FTPWrapperTests(unittest.TestCase): |
| 1240 | # |
| 1241 | # def setUp(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1242 | # import ftplib, time, threading |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1243 | # ftplib.FTP.port = 9093 |
| 1244 | # self.evt = threading.Event() |
| 1245 | # threading.Thread(target=server, args=(self.evt,)).start() |
| 1246 | # time.sleep(.1) |
| 1247 | # |
| 1248 | # def tearDown(self): |
| 1249 | # self.evt.wait() |
| 1250 | # |
| 1251 | # def testBasic(self): |
| 1252 | # # connects |
| 1253 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1254 | # ftp.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1255 | # |
| 1256 | # def testTimeoutNone(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1257 | # # global default timeout is ignored |
| 1258 | # import socket |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1259 | # self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1260 | # socket.setdefaulttimeout(30) |
| 1261 | # try: |
| 1262 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
| 1263 | # finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1264 | # socket.setdefaulttimeout(None) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1265 | # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1266 | # ftp.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1267 | # |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1268 | # def testTimeoutDefault(self): |
| 1269 | # # global default timeout is used |
| 1270 | # import socket |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1271 | # self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1272 | # socket.setdefaulttimeout(30) |
| 1273 | # try: |
| 1274 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
| 1275 | # finally: |
| 1276 | # socket.setdefaulttimeout(None) |
| 1277 | # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
| 1278 | # ftp.close() |
| 1279 | # |
| 1280 | # def testTimeoutValue(self): |
| 1281 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [], |
| 1282 | # timeout=30) |
| 1283 | # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
| 1284 | # ftp.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1285 | |
Senthil Kumaran | de49d64 | 2011-10-16 23:54:44 +0800 | [diff] [blame] | 1286 | class RequestTests(unittest.TestCase): |
| 1287 | """Unit tests for urllib.request.Request.""" |
| 1288 | |
| 1289 | def test_default_values(self): |
| 1290 | Request = urllib.request.Request |
| 1291 | request = Request("http://www.python.org") |
| 1292 | self.assertEqual(request.get_method(), 'GET') |
| 1293 | request = Request("http://www.python.org", {}) |
| 1294 | self.assertEqual(request.get_method(), 'POST') |
| 1295 | |
| 1296 | def test_with_method_arg(self): |
| 1297 | Request = urllib.request.Request |
| 1298 | request = Request("http://www.python.org", method='HEAD') |
| 1299 | self.assertEqual(request.method, 'HEAD') |
| 1300 | self.assertEqual(request.get_method(), 'HEAD') |
| 1301 | request = Request("http://www.python.org", {}, method='HEAD') |
| 1302 | self.assertEqual(request.method, 'HEAD') |
| 1303 | self.assertEqual(request.get_method(), 'HEAD') |
| 1304 | request = Request("http://www.python.org", method='GET') |
| 1305 | self.assertEqual(request.get_method(), 'GET') |
| 1306 | request.method = 'HEAD' |
| 1307 | self.assertEqual(request.get_method(), 'HEAD') |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 1308 | |
| 1309 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1310 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1311 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1312 | urlopen_FileTests, |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 1313 | urlopen_HttpTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1314 | urlretrieve_FileTests, |
Senthil Kumaran | ce26014 | 2011-11-01 01:35:17 +0800 | [diff] [blame] | 1315 | urlretrieve_HttpTests, |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 1316 | ProxyTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1317 | QuotingTests, |
| 1318 | UnquotingTests, |
| 1319 | urlencode_Tests, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1320 | Pathname_Tests, |
Senthil Kumaran | eaaec27 | 2009-03-30 21:54:41 +0000 | [diff] [blame] | 1321 | Utility_Tests, |
Senthil Kumaran | 690ce9b | 2009-05-05 18:41:13 +0000 | [diff] [blame] | 1322 | URLopener_Tests, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1323 | #FTPWrapperTests, |
Senthil Kumaran | de49d64 | 2011-10-16 23:54:44 +0800 | [diff] [blame] | 1324 | RequestTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1325 | ) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1326 | |
| 1327 | |
| 1328 | |
| 1329 | if __name__ == '__main__': |
| 1330 | test_main() |