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