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