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