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