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