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