Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 1 | """Regresssion tests for urllib""" |
| 2 | |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 3 | import urllib |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 4 | import httplib |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | from test import test_support |
| 7 | import os |
| 8 | import mimetools |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 9 | import tempfile |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 10 | import StringIO |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 11 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 12 | def hexescape(char): |
| 13 | """Escape char as RFC 2396 specifies""" |
| 14 | hex_repr = hex(ord(char))[2:].upper() |
| 15 | if len(hex_repr) == 1: |
| 16 | hex_repr = "0%s" % hex_repr |
| 17 | return "%" + hex_repr |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 18 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 19 | class urlopen_FileTests(unittest.TestCase): |
| 20 | """Test urlopen() opening a temporary file. |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 21 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 22 | 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] | 23 | on connecting to the Net for testing. |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 24 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 25 | """ |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 26 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 27 | def setUp(self): |
| 28 | """Setup of a temp file to use for testing""" |
| 29 | self.text = "test_urllib: %s\n" % self.__class__.__name__ |
Guido van Rossum | 51735b0 | 2003-04-25 15:01:05 +0000 | [diff] [blame] | 30 | FILE = file(test_support.TESTFN, 'wb') |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 31 | try: |
| 32 | FILE.write(self.text) |
| 33 | finally: |
| 34 | FILE.close() |
| 35 | self.pathname = test_support.TESTFN |
| 36 | self.returned_obj = urllib.urlopen("file:%s" % self.pathname) |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 37 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 38 | def tearDown(self): |
| 39 | """Shut down the open object""" |
| 40 | self.returned_obj.close() |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 41 | os.remove(test_support.TESTFN) |
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 | def test_interface(self): |
| 44 | # Make sure object returned by urlopen() has the specified methods |
| 45 | for attr in ("read", "readline", "readlines", "fileno", |
Georg Brandl | 9b0d46d | 2008-01-20 11:43:03 +0000 | [diff] [blame] | 46 | "close", "info", "geturl", "getcode", "__iter__"): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 47 | self.assertTrue(hasattr(self.returned_obj, attr), |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 48 | "object returned by urlopen() lacks %s attribute" % |
| 49 | attr) |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 50 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 51 | def test_read(self): |
| 52 | self.assertEqual(self.text, self.returned_obj.read()) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 53 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 54 | def test_readline(self): |
| 55 | self.assertEqual(self.text, self.returned_obj.readline()) |
| 56 | self.assertEqual('', self.returned_obj.readline(), |
| 57 | "calling readline() after exhausting the file did not" |
| 58 | " return an empty string") |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 59 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 60 | def test_readlines(self): |
| 61 | lines_list = self.returned_obj.readlines() |
| 62 | self.assertEqual(len(lines_list), 1, |
| 63 | "readlines() returned the wrong number of lines") |
| 64 | self.assertEqual(lines_list[0], self.text, |
| 65 | "readlines() returned improper text") |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 66 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 67 | def test_fileno(self): |
| 68 | file_num = self.returned_obj.fileno() |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 69 | self.assertIsInstance(file_num, int, "fileno() did not return an int") |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 70 | self.assertEqual(os.read(file_num, len(self.text)), self.text, |
| 71 | "Reading on the file descriptor returned by fileno() " |
| 72 | "did not return the expected text") |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 73 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 74 | def test_close(self): |
| 75 | # Test close() by calling it hear and then having it be called again |
| 76 | # by the tearDown() method for the test |
| 77 | self.returned_obj.close() |
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_info(self): |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 80 | self.assertIsInstance(self.returned_obj.info(), mimetools.Message) |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 81 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 82 | def test_geturl(self): |
| 83 | self.assertEqual(self.returned_obj.geturl(), self.pathname) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 84 | |
Georg Brandl | 9b0d46d | 2008-01-20 11:43:03 +0000 | [diff] [blame] | 85 | def test_getcode(self): |
| 86 | self.assertEqual(self.returned_obj.getcode(), None) |
| 87 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 88 | def test_iter(self): |
| 89 | # Test iterator |
| 90 | # Don't need to count number of iterations since test would fail the |
| 91 | # instant it returned anything beyond the first line from the |
| 92 | # comparison |
| 93 | for line in self.returned_obj.__iter__(): |
| 94 | self.assertEqual(line, self.text) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 95 | |
Benjamin Peterson | 2c7470d | 2008-09-21 21:27:51 +0000 | [diff] [blame] | 96 | class ProxyTests(unittest.TestCase): |
| 97 | |
| 98 | def setUp(self): |
Walter Dörwald | 4b965f6 | 2009-04-26 20:51:44 +0000 | [diff] [blame] | 99 | # Records changes to env vars |
| 100 | self.env = test_support.EnvironmentVarGuard() |
Benjamin Peterson | 2c7470d | 2008-09-21 21:27:51 +0000 | [diff] [blame] | 101 | # Delete all proxy related env vars |
Senthil Kumaran | 7a2ee0b | 2010-01-08 19:20:25 +0000 | [diff] [blame] | 102 | for k in os.environ.keys(): |
Walter Dörwald | 4b965f6 | 2009-04-26 20:51:44 +0000 | [diff] [blame] | 103 | if 'proxy' in k.lower(): |
Senthil Kumaran | dc61ec3 | 2009-10-01 01:50:13 +0000 | [diff] [blame] | 104 | self.env.unset(k) |
Benjamin Peterson | 2c7470d | 2008-09-21 21:27:51 +0000 | [diff] [blame] | 105 | |
| 106 | def tearDown(self): |
Benjamin Peterson | 2c7470d | 2008-09-21 21:27:51 +0000 | [diff] [blame] | 107 | # Restore all proxy related env vars |
Walter Dörwald | 4b965f6 | 2009-04-26 20:51:44 +0000 | [diff] [blame] | 108 | self.env.__exit__() |
| 109 | del self.env |
Benjamin Peterson | 2c7470d | 2008-09-21 21:27:51 +0000 | [diff] [blame] | 110 | |
| 111 | def test_getproxies_environment_keep_no_proxies(self): |
Walter Dörwald | 4b965f6 | 2009-04-26 20:51:44 +0000 | [diff] [blame] | 112 | self.env.set('NO_PROXY', 'localhost') |
Benjamin Peterson | 2c7470d | 2008-09-21 21:27:51 +0000 | [diff] [blame] | 113 | proxies = urllib.getproxies_environment() |
| 114 | # getproxies_environment use lowered case truncated (no '_proxy') keys |
| 115 | self.assertEquals('localhost', proxies['no']) |
| 116 | |
| 117 | |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 118 | class urlopen_HttpTests(unittest.TestCase): |
| 119 | """Test urlopen() opening a fake http connection.""" |
| 120 | |
| 121 | def fakehttp(self, fakedata): |
| 122 | class FakeSocket(StringIO.StringIO): |
| 123 | def sendall(self, str): pass |
| 124 | def makefile(self, mode, name): return self |
| 125 | def read(self, amt=None): |
| 126 | if self.closed: return '' |
| 127 | return StringIO.StringIO.read(self, amt) |
| 128 | def readline(self, length=None): |
| 129 | if self.closed: return '' |
| 130 | return StringIO.StringIO.readline(self, length) |
| 131 | class FakeHTTPConnection(httplib.HTTPConnection): |
| 132 | def connect(self): |
| 133 | self.sock = FakeSocket(fakedata) |
| 134 | assert httplib.HTTP._connection_class == httplib.HTTPConnection |
| 135 | httplib.HTTP._connection_class = FakeHTTPConnection |
| 136 | |
| 137 | def unfakehttp(self): |
| 138 | httplib.HTTP._connection_class = httplib.HTTPConnection |
| 139 | |
| 140 | def test_read(self): |
| 141 | self.fakehttp('Hello!') |
| 142 | try: |
| 143 | fp = urllib.urlopen("http://python.org/") |
| 144 | self.assertEqual(fp.readline(), 'Hello!') |
| 145 | self.assertEqual(fp.readline(), '') |
Georg Brandl | 9b0d46d | 2008-01-20 11:43:03 +0000 | [diff] [blame] | 146 | self.assertEqual(fp.geturl(), 'http://python.org/') |
| 147 | self.assertEqual(fp.getcode(), 200) |
Hye-Shik Chang | 39aef79 | 2004-06-05 13:30:56 +0000 | [diff] [blame] | 148 | finally: |
| 149 | self.unfakehttp() |
| 150 | |
Kurt B. Kaiser | 0f7c25d | 2008-01-02 04:11:28 +0000 | [diff] [blame] | 151 | def test_read_bogus(self): |
Kurt B. Kaiser | 0a11232 | 2008-01-02 05:23:38 +0000 | [diff] [blame] | 152 | # urlopen() should raise IOError for many error codes. |
Kurt B. Kaiser | 0f7c25d | 2008-01-02 04:11:28 +0000 | [diff] [blame] | 153 | self.fakehttp('''HTTP/1.1 401 Authentication Required |
| 154 | Date: Wed, 02 Jan 2008 03:03:54 GMT |
| 155 | Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e |
| 156 | Connection: close |
| 157 | Content-Type: text/html; charset=iso-8859-1 |
| 158 | ''') |
| 159 | try: |
| 160 | self.assertRaises(IOError, urllib.urlopen, "http://python.org/") |
| 161 | finally: |
| 162 | self.unfakehttp() |
| 163 | |
Georg Brandl | f66b603 | 2007-03-14 08:27:52 +0000 | [diff] [blame] | 164 | def test_empty_socket(self): |
Kurt B. Kaiser | 0a11232 | 2008-01-02 05:23:38 +0000 | [diff] [blame] | 165 | # urlopen() raises IOError if the underlying socket does not send any |
| 166 | # data. (#1680230) |
Georg Brandl | f66b603 | 2007-03-14 08:27:52 +0000 | [diff] [blame] | 167 | self.fakehttp('') |
| 168 | try: |
| 169 | self.assertRaises(IOError, urllib.urlopen, 'http://something') |
| 170 | finally: |
| 171 | self.unfakehttp() |
| 172 | |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 173 | class urlretrieve_FileTests(unittest.TestCase): |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 174 | """Test urllib.urlretrieve() on local files""" |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 175 | |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 176 | def setUp(self): |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 177 | # Create a list of temporary files. Each item in the list is a file |
| 178 | # name (absolute path or relative to the current working directory). |
| 179 | # All files in this list will be deleted in the tearDown method. Note, |
| 180 | # this only helps to makes sure temporary files get deleted, but it |
| 181 | # does nothing about trying to close files that may still be open. It |
| 182 | # is the responsibility of the developer to properly close files even |
| 183 | # when exceptional conditions occur. |
| 184 | self.tempFiles = [] |
| 185 | |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 186 | # Create a temporary file. |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 187 | self.registerFileForCleanUp(test_support.TESTFN) |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 188 | self.text = 'testing urllib.urlretrieve' |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 189 | try: |
| 190 | FILE = file(test_support.TESTFN, 'wb') |
| 191 | FILE.write(self.text) |
| 192 | FILE.close() |
| 193 | finally: |
| 194 | try: FILE.close() |
| 195 | except: pass |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 196 | |
| 197 | def tearDown(self): |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 198 | # Delete the temporary files. |
| 199 | for each in self.tempFiles: |
| 200 | try: os.remove(each) |
| 201 | except: pass |
| 202 | |
| 203 | def constructLocalFileUrl(self, filePath): |
| 204 | return "file://%s" % urllib.pathname2url(os.path.abspath(filePath)) |
| 205 | |
| 206 | def createNewTempFile(self, data=""): |
| 207 | """Creates a new temporary file containing the specified data, |
| 208 | registers the file for deletion during the test fixture tear down, and |
| 209 | returns the absolute path of the file.""" |
| 210 | |
| 211 | newFd, newFilePath = tempfile.mkstemp() |
| 212 | try: |
| 213 | self.registerFileForCleanUp(newFilePath) |
| 214 | newFile = os.fdopen(newFd, "wb") |
| 215 | newFile.write(data) |
| 216 | newFile.close() |
| 217 | finally: |
| 218 | try: newFile.close() |
| 219 | except: pass |
| 220 | return newFilePath |
| 221 | |
| 222 | def registerFileForCleanUp(self, fileName): |
| 223 | self.tempFiles.append(fileName) |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 224 | |
| 225 | def test_basic(self): |
| 226 | # Make sure that a local file just gets its own location returned and |
| 227 | # a headers value is returned. |
| 228 | result = urllib.urlretrieve("file:%s" % test_support.TESTFN) |
| 229 | self.assertEqual(result[0], test_support.TESTFN) |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 230 | self.assertIsInstance(result[1], mimetools.Message, |
| 231 | "did not get a mimetools.Message instance as " |
| 232 | "second returned value") |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 233 | |
| 234 | def test_copy(self): |
| 235 | # Test that setting the filename argument works. |
| 236 | second_temp = "%s.2" % test_support.TESTFN |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 237 | self.registerFileForCleanUp(second_temp) |
| 238 | result = urllib.urlretrieve(self.constructLocalFileUrl( |
| 239 | test_support.TESTFN), second_temp) |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 240 | self.assertEqual(second_temp, result[0]) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 241 | 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] | 242 | "made") |
| 243 | FILE = file(second_temp, 'rb') |
| 244 | try: |
| 245 | text = FILE.read() |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 246 | FILE.close() |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 247 | finally: |
| 248 | try: FILE.close() |
| 249 | except: pass |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 250 | self.assertEqual(self.text, text) |
| 251 | |
| 252 | def test_reporthook(self): |
| 253 | # Make sure that the reporthook works. |
| 254 | def hooktester(count, block_size, total_size, count_holder=[0]): |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 255 | self.assertIsInstance(count, int) |
| 256 | self.assertIsInstance(block_size, int) |
| 257 | self.assertIsInstance(total_size, int) |
Brett Cannon | 1969136 | 2003-04-29 05:08:06 +0000 | [diff] [blame] | 258 | self.assertEqual(count, count_holder[0]) |
| 259 | count_holder[0] = count_holder[0] + 1 |
| 260 | second_temp = "%s.2" % test_support.TESTFN |
Georg Brandl | 5a650a2 | 2005-08-26 08:51:34 +0000 | [diff] [blame] | 261 | self.registerFileForCleanUp(second_temp) |
| 262 | urllib.urlretrieve(self.constructLocalFileUrl(test_support.TESTFN), |
| 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() |
| 271 | urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), |
| 272 | test_support.TESTFN, hooktester) |
| 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)) |
| 284 | srcFileName = self.createNewTempFile("x" * 5) |
| 285 | urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), |
| 286 | test_support.TESTFN, hooktester) |
| 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)) |
| 298 | srcFileName = self.createNewTempFile("x" * 8193) |
| 299 | urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), |
| 300 | test_support.TESTFN, hooktester) |
| 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 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +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>. The Python |
| 310 | code of ``'%' + hex(ord(<character>))[2:]`` escapes a character properly. |
| 311 | Case does not matter on the hex letters. |
| 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 | "_.-"]) |
| 337 | result = urllib.quote(do_not_quote) |
| 338 | self.assertEqual(do_not_quote, result, |
| 339 | "using quote(): %s != %s" % (do_not_quote, result)) |
| 340 | result = urllib.quote_plus(do_not_quote) |
| 341 | self.assertEqual(do_not_quote, result, |
| 342 | "using quote_plus(): %s != %s" % (do_not_quote, result)) |
| 343 | |
| 344 | def test_default_safe(self): |
| 345 | # Test '/' is default value for 'safe' parameter |
| 346 | self.assertEqual(urllib.quote.func_defaults[0], '/') |
| 347 | |
| 348 | def test_safe(self): |
| 349 | # Test setting 'safe' parameter does what it should do |
| 350 | quote_by_default = "<>" |
| 351 | result = urllib.quote(quote_by_default, safe=quote_by_default) |
| 352 | self.assertEqual(quote_by_default, result, |
| 353 | "using quote(): %s != %s" % (quote_by_default, result)) |
| 354 | result = urllib.quote_plus(quote_by_default, safe=quote_by_default) |
| 355 | self.assertEqual(quote_by_default, result, |
| 356 | "using quote_plus(): %s != %s" % |
| 357 | (quote_by_default, result)) |
| 358 | |
| 359 | def test_default_quoting(self): |
| 360 | # Make sure all characters that should be quoted are by default sans |
| 361 | # space (separate test for that). |
| 362 | should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F |
| 363 | should_quote.append('<>#%"{}|\^[]`') |
| 364 | should_quote.append(chr(127)) # For 0x7F |
| 365 | should_quote = ''.join(should_quote) |
| 366 | for char in should_quote: |
| 367 | result = urllib.quote(char) |
| 368 | self.assertEqual(hexescape(char), result, |
| 369 | "using quote(): %s should be escaped to %s, not %s" % |
| 370 | (char, hexescape(char), result)) |
| 371 | result = urllib.quote_plus(char) |
| 372 | self.assertEqual(hexescape(char), result, |
| 373 | "using quote_plus(): " |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 374 | "%s should be escapes to %s, not %s" % |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 375 | (char, hexescape(char), result)) |
| 376 | del should_quote |
| 377 | partial_quote = "ab[]cd" |
| 378 | expected = "ab%5B%5Dcd" |
| 379 | result = urllib.quote(partial_quote) |
| 380 | self.assertEqual(expected, result, |
| 381 | "using quote(): %s != %s" % (expected, result)) |
| 382 | self.assertEqual(expected, result, |
| 383 | "using quote_plus(): %s != %s" % (expected, result)) |
| 384 | |
| 385 | def test_quoting_space(self): |
| 386 | # Make sure quote() and quote_plus() handle spaces as specified in |
| 387 | # their unique way |
| 388 | result = urllib.quote(' ') |
| 389 | self.assertEqual(result, hexescape(' '), |
| 390 | "using quote(): %s != %s" % (result, hexescape(' '))) |
| 391 | result = urllib.quote_plus(' ') |
| 392 | self.assertEqual(result, '+', |
| 393 | "using quote_plus(): %s != +" % result) |
| 394 | given = "a b cd e f" |
| 395 | expect = given.replace(' ', hexescape(' ')) |
| 396 | result = urllib.quote(given) |
| 397 | self.assertEqual(expect, result, |
| 398 | "using quote(): %s != %s" % (expect, result)) |
| 399 | expect = given.replace(' ', '+') |
| 400 | result = urllib.quote_plus(given) |
| 401 | self.assertEqual(expect, result, |
| 402 | "using quote_plus(): %s != %s" % (expect, result)) |
| 403 | |
Raymond Hettinger | 2bdec7b | 2005-09-10 14:30:09 +0000 | [diff] [blame] | 404 | def test_quoting_plus(self): |
| 405 | self.assertEqual(urllib.quote_plus('alpha+beta gamma'), |
| 406 | 'alpha%2Bbeta+gamma') |
| 407 | self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'), |
| 408 | 'alpha+beta+gamma') |
| 409 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 410 | class UnquotingTests(unittest.TestCase): |
| 411 | """Tests for unquote() and unquote_plus() |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 412 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 413 | See the doc string for quoting_Tests for details on quoting and such. |
| 414 | |
| 415 | """ |
| 416 | |
| 417 | def test_unquoting(self): |
| 418 | # Make sure unquoting of all ASCII values works |
| 419 | escape_list = [] |
| 420 | for num in range(128): |
| 421 | given = hexescape(chr(num)) |
| 422 | expect = chr(num) |
| 423 | result = urllib.unquote(given) |
| 424 | self.assertEqual(expect, result, |
| 425 | "using unquote(): %s != %s" % (expect, result)) |
| 426 | result = urllib.unquote_plus(given) |
| 427 | self.assertEqual(expect, result, |
| 428 | "using unquote_plus(): %s != %s" % |
| 429 | (expect, result)) |
| 430 | escape_list.append(given) |
| 431 | escape_string = ''.join(escape_list) |
| 432 | del escape_list |
| 433 | result = urllib.unquote(escape_string) |
| 434 | self.assertEqual(result.count('%'), 1, |
| 435 | "using quote(): not all characters escaped; %s" % |
| 436 | result) |
| 437 | result = urllib.unquote(escape_string) |
| 438 | self.assertEqual(result.count('%'), 1, |
| 439 | "using unquote(): not all characters escaped: " |
| 440 | "%s" % result) |
| 441 | |
Senthil Kumaran | f3e9b2a | 2010-03-18 12:14:15 +0000 | [diff] [blame^] | 442 | def test_unquoting_badpercent(self): |
| 443 | # Test unquoting on bad percent-escapes |
| 444 | given = '%xab' |
| 445 | expect = given |
| 446 | result = urllib.unquote(given) |
| 447 | self.assertEqual(expect, result, "using unquote(): %r != %r" |
| 448 | % (expect, result)) |
| 449 | given = '%x' |
| 450 | expect = given |
| 451 | result = urllib.unquote(given) |
| 452 | self.assertEqual(expect, result, "using unquote(): %r != %r" |
| 453 | % (expect, result)) |
| 454 | given = '%' |
| 455 | expect = given |
| 456 | result = urllib.unquote(given) |
| 457 | self.assertEqual(expect, result, "using unquote(): %r != %r" |
| 458 | % (expect, result)) |
| 459 | |
| 460 | def test_unquoting_mixed_case(self): |
| 461 | # Test unquoting on mixed-case hex digits in the percent-escapes |
| 462 | given = '%Ab%eA' |
| 463 | expect = '\xab\xea' |
| 464 | result = urllib.unquote(given) |
| 465 | self.assertEqual(expect, result, "using unquote(): %r != %r" |
| 466 | % (expect, result)) |
| 467 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 468 | def test_unquoting_parts(self): |
| 469 | # Make sure unquoting works when have non-quoted characters |
| 470 | # interspersed |
| 471 | given = 'ab%sd' % hexescape('c') |
| 472 | expect = "abcd" |
| 473 | result = urllib.unquote(given) |
| 474 | self.assertEqual(expect, result, |
| 475 | "using quote(): %s != %s" % (expect, result)) |
| 476 | result = urllib.unquote_plus(given) |
| 477 | self.assertEqual(expect, result, |
| 478 | "using unquote_plus(): %s != %s" % (expect, result)) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 479 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 480 | def test_unquoting_plus(self): |
| 481 | # Test difference between unquote() and unquote_plus() |
| 482 | given = "are+there+spaces..." |
| 483 | expect = given |
| 484 | result = urllib.unquote(given) |
| 485 | self.assertEqual(expect, result, |
| 486 | "using unquote(): %s != %s" % (expect, result)) |
| 487 | expect = given.replace('+', ' ') |
| 488 | result = urllib.unquote_plus(given) |
| 489 | self.assertEqual(expect, result, |
| 490 | "using unquote_plus(): %s != %s" % (expect, result)) |
| 491 | |
Raymond Hettinger | 4b0f20d | 2005-10-15 16:41:53 +0000 | [diff] [blame] | 492 | def test_unquote_with_unicode(self): |
| 493 | r = urllib.unquote(u'br%C3%BCckner_sapporo_20050930.doc') |
| 494 | self.assertEqual(r, u'br\xc3\xbcckner_sapporo_20050930.doc') |
| 495 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 496 | class urlencode_Tests(unittest.TestCase): |
| 497 | """Tests for urlencode()""" |
| 498 | |
| 499 | def help_inputtype(self, given, test_type): |
| 500 | """Helper method for testing different input types. |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 501 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 502 | 'given' must lead to only the pairs: |
| 503 | * 1st, 1 |
| 504 | * 2nd, 2 |
| 505 | * 3rd, 3 |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 506 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 507 | Test cannot assume anything about order. Docs make no guarantee and |
| 508 | have possible dictionary input. |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 509 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 510 | """ |
| 511 | expect_somewhere = ["1st=1", "2nd=2", "3rd=3"] |
| 512 | result = urllib.urlencode(given) |
| 513 | for expected in expect_somewhere: |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 514 | self.assertIn(expected, result, |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 515 | "testing %s: %s not found in %s" % |
| 516 | (test_type, expected, result)) |
| 517 | self.assertEqual(result.count('&'), 2, |
| 518 | "testing %s: expected 2 '&'s; got %s" % |
| 519 | (test_type, result.count('&'))) |
| 520 | amp_location = result.index('&') |
| 521 | on_amp_left = result[amp_location - 1] |
| 522 | on_amp_right = result[amp_location + 1] |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 523 | self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(), |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 524 | "testing %s: '&' not located in proper place in %s" % |
| 525 | (test_type, result)) |
| 526 | self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps |
| 527 | "testing %s: " |
| 528 | "unexpected number of characters: %s != %s" % |
| 529 | (test_type, len(result), (5 * 3) + 2)) |
| 530 | |
| 531 | def test_using_mapping(self): |
| 532 | # Test passing in a mapping object as an argument. |
| 533 | self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'}, |
| 534 | "using dict as input type") |
| 535 | |
| 536 | def test_using_sequence(self): |
| 537 | # Test passing in a sequence of two-item sequences as an argument. |
| 538 | self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')], |
| 539 | "using sequence of two-item tuples as input") |
| 540 | |
| 541 | def test_quoting(self): |
| 542 | # Make sure keys and values are quoted using quote_plus() |
| 543 | given = {"&":"="} |
| 544 | expect = "%s=%s" % (hexescape('&'), hexescape('=')) |
| 545 | result = urllib.urlencode(given) |
| 546 | self.assertEqual(expect, result) |
| 547 | given = {"key name":"A bunch of pluses"} |
| 548 | expect = "key+name=A+bunch+of+pluses" |
| 549 | result = urllib.urlencode(given) |
| 550 | self.assertEqual(expect, result) |
| 551 | |
| 552 | def test_doseq(self): |
| 553 | # Test that passing True for 'doseq' parameter works correctly |
| 554 | given = {'sequence':['1', '2', '3']} |
| 555 | expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3'])) |
| 556 | result = urllib.urlencode(given) |
| 557 | self.assertEqual(expect, result) |
| 558 | result = urllib.urlencode(given, True) |
| 559 | for value in given["sequence"]: |
| 560 | expect = "sequence=%s" % value |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 561 | self.assertIn(expect, result) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 562 | self.assertEqual(result.count('&'), 2, |
| 563 | "Expected 2 '&'s, got %s" % result.count('&')) |
| 564 | |
| 565 | class Pathname_Tests(unittest.TestCase): |
| 566 | """Test pathname2url() and url2pathname()""" |
| 567 | |
| 568 | def test_basic(self): |
| 569 | # Make sure simple tests pass |
| 570 | expected_path = os.path.join("parts", "of", "a", "path") |
| 571 | expected_url = "parts/of/a/path" |
| 572 | result = urllib.pathname2url(expected_path) |
| 573 | self.assertEqual(expected_url, result, |
| 574 | "pathname2url() failed; %s != %s" % |
| 575 | (result, expected_url)) |
| 576 | result = urllib.url2pathname(expected_url) |
| 577 | self.assertEqual(expected_path, result, |
| 578 | "url2pathame() failed; %s != %s" % |
| 579 | (result, expected_path)) |
| 580 | |
| 581 | def test_quoting(self): |
| 582 | # Test automatic quoting and unquoting works for pathnam2url() and |
| 583 | # url2pathname() respectively |
| 584 | given = os.path.join("needs", "quot=ing", "here") |
| 585 | expect = "needs/%s/here" % urllib.quote("quot=ing") |
| 586 | result = urllib.pathname2url(given) |
| 587 | self.assertEqual(expect, result, |
| 588 | "pathname2url() failed; %s != %s" % |
| 589 | (expect, result)) |
| 590 | expect = given |
| 591 | result = urllib.url2pathname(result) |
| 592 | self.assertEqual(expect, result, |
| 593 | "url2pathname() failed; %s != %s" % |
| 594 | (expect, result)) |
| 595 | given = os.path.join("make sure", "using_quote") |
| 596 | expect = "%s/using_quote" % urllib.quote("make sure") |
| 597 | result = urllib.pathname2url(given) |
| 598 | self.assertEqual(expect, result, |
| 599 | "pathname2url() failed; %s != %s" % |
| 600 | (expect, result)) |
| 601 | given = "make+sure/using_unquote" |
| 602 | expect = os.path.join("make+sure", "using_unquote") |
| 603 | result = urllib.url2pathname(given) |
| 604 | self.assertEqual(expect, result, |
| 605 | "url2pathname() failed; %s != %s" % |
| 606 | (expect, result)) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 607 | |
Senthil Kumaran | 5e95e76 | 2009-03-30 21:51:50 +0000 | [diff] [blame] | 608 | class Utility_Tests(unittest.TestCase): |
| 609 | """Testcase to test the various utility functions in the urllib.""" |
| 610 | |
| 611 | def test_splitpasswd(self): |
| 612 | """Some of the password examples are not sensible, but it is added to |
| 613 | confirming to RFC2617 and addressing issue4675. |
| 614 | """ |
| 615 | self.assertEqual(('user', 'ab'),urllib.splitpasswd('user:ab')) |
| 616 | self.assertEqual(('user', 'a\nb'),urllib.splitpasswd('user:a\nb')) |
| 617 | self.assertEqual(('user', 'a\tb'),urllib.splitpasswd('user:a\tb')) |
| 618 | self.assertEqual(('user', 'a\rb'),urllib.splitpasswd('user:a\rb')) |
| 619 | self.assertEqual(('user', 'a\fb'),urllib.splitpasswd('user:a\fb')) |
| 620 | self.assertEqual(('user', 'a\vb'),urllib.splitpasswd('user:a\vb')) |
| 621 | self.assertEqual(('user', 'a:b'),urllib.splitpasswd('user:a:b')) |
| 622 | |
| 623 | |
Senthil Kumaran | 7c2867f | 2009-04-21 03:24:19 +0000 | [diff] [blame] | 624 | class URLopener_Tests(unittest.TestCase): |
| 625 | """Testcase to test the open method of URLopener class.""" |
| 626 | |
| 627 | def test_quoted_open(self): |
| 628 | class DummyURLopener(urllib.URLopener): |
| 629 | def open_spam(self, url): |
| 630 | return url |
| 631 | |
| 632 | self.assertEqual(DummyURLopener().open( |
| 633 | 'spam://example/ /'),'//example/%20/') |
| 634 | |
Senthil Kumaran | 18d5a69 | 2010-02-20 22:05:34 +0000 | [diff] [blame] | 635 | # test the safe characters are not quoted by urlopen |
| 636 | self.assertEqual(DummyURLopener().open( |
| 637 | "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"), |
| 638 | "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/") |
| 639 | |
Senthil Kumaran | 7c2867f | 2009-04-21 03:24:19 +0000 | [diff] [blame] | 640 | |
Facundo Batista | d9880d0 | 2007-05-25 04:20:22 +0000 | [diff] [blame] | 641 | # Just commented them out. |
| 642 | # Can't really tell why keep failing in windows and sparc. |
| 643 | # Everywhere else they work ok, but on those machines, someteimes |
| 644 | # fail in one of the tests, sometimes in other. I have a linux, and |
| 645 | # the tests go ok. |
| 646 | # If anybody has one of the problematic enviroments, please help! |
| 647 | # . Facundo |
| 648 | # |
| 649 | # def server(evt): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 650 | # import socket, time |
Facundo Batista | d9880d0 | 2007-05-25 04:20:22 +0000 | [diff] [blame] | 651 | # serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 652 | # serv.settimeout(3) |
| 653 | # serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 654 | # serv.bind(("", 9093)) |
| 655 | # serv.listen(5) |
| 656 | # try: |
| 657 | # conn, addr = serv.accept() |
| 658 | # conn.send("1 Hola mundo\n") |
| 659 | # cantdata = 0 |
| 660 | # while cantdata < 13: |
| 661 | # data = conn.recv(13-cantdata) |
| 662 | # cantdata += len(data) |
| 663 | # time.sleep(.3) |
| 664 | # conn.send("2 No more lines\n") |
| 665 | # conn.close() |
| 666 | # except socket.timeout: |
| 667 | # pass |
| 668 | # finally: |
| 669 | # serv.close() |
| 670 | # evt.set() |
| 671 | # |
| 672 | # class FTPWrapperTests(unittest.TestCase): |
| 673 | # |
| 674 | # def setUp(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 675 | # import ftplib, time, threading |
Facundo Batista | d9880d0 | 2007-05-25 04:20:22 +0000 | [diff] [blame] | 676 | # ftplib.FTP.port = 9093 |
| 677 | # self.evt = threading.Event() |
| 678 | # threading.Thread(target=server, args=(self.evt,)).start() |
| 679 | # time.sleep(.1) |
| 680 | # |
| 681 | # def tearDown(self): |
| 682 | # self.evt.wait() |
| 683 | # |
| 684 | # def testBasic(self): |
| 685 | # # connects |
| 686 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 687 | # ftp.close() |
Facundo Batista | d9880d0 | 2007-05-25 04:20:22 +0000 | [diff] [blame] | 688 | # |
| 689 | # def testTimeoutNone(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 690 | # # global default timeout is ignored |
| 691 | # import socket |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 692 | # self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | d9880d0 | 2007-05-25 04:20:22 +0000 | [diff] [blame] | 693 | # socket.setdefaulttimeout(30) |
| 694 | # try: |
| 695 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
| 696 | # finally: |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 697 | # socket.setdefaulttimeout(None) |
Facundo Batista | d9880d0 | 2007-05-25 04:20:22 +0000 | [diff] [blame] | 698 | # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 699 | # ftp.close() |
Facundo Batista | d9880d0 | 2007-05-25 04:20:22 +0000 | [diff] [blame] | 700 | # |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 701 | # def testTimeoutDefault(self): |
| 702 | # # global default timeout is used |
| 703 | # import socket |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 704 | # self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 705 | # socket.setdefaulttimeout(30) |
| 706 | # try: |
| 707 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
| 708 | # finally: |
| 709 | # socket.setdefaulttimeout(None) |
| 710 | # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
| 711 | # ftp.close() |
| 712 | # |
| 713 | # def testTimeoutValue(self): |
| 714 | # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [], |
| 715 | # timeout=30) |
| 716 | # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
| 717 | # ftp.close() |
Facundo Batista | 711a54e | 2007-05-24 17:50:54 +0000 | [diff] [blame] | 718 | |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame] | 719 | |
| 720 | |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 721 | def test_main(): |
Brett Cannon | 8bb8fa5 | 2008-07-02 01:57:08 +0000 | [diff] [blame] | 722 | import warnings |
Brett Cannon | 672237d | 2008-09-09 00:49:16 +0000 | [diff] [blame] | 723 | with warnings.catch_warnings(): |
Brett Cannon | 8bb8fa5 | 2008-07-02 01:57:08 +0000 | [diff] [blame] | 724 | warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0", |
| 725 | DeprecationWarning) |
| 726 | test_support.run_unittest( |
| 727 | urlopen_FileTests, |
| 728 | urlopen_HttpTests, |
| 729 | urlretrieve_FileTests, |
Benjamin Peterson | 2c7470d | 2008-09-21 21:27:51 +0000 | [diff] [blame] | 730 | ProxyTests, |
Brett Cannon | 8bb8fa5 | 2008-07-02 01:57:08 +0000 | [diff] [blame] | 731 | QuotingTests, |
| 732 | UnquotingTests, |
| 733 | urlencode_Tests, |
| 734 | Pathname_Tests, |
Senthil Kumaran | 5e95e76 | 2009-03-30 21:51:50 +0000 | [diff] [blame] | 735 | Utility_Tests, |
Senthil Kumaran | 7c2867f | 2009-04-21 03:24:19 +0000 | [diff] [blame] | 736 | URLopener_Tests, |
Brett Cannon | 8bb8fa5 | 2008-07-02 01:57:08 +0000 | [diff] [blame] | 737 | #FTPWrapperTests, |
| 738 | ) |
Brett Cannon | 74bfd70 | 2003-04-25 09:39:47 +0000 | [diff] [blame] | 739 | |
| 740 | |
| 741 | |
| 742 | if __name__ == '__main__': |
| 743 | test_main() |