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