Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3 | from test.test_urllib2 import sanepathname2url |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 4 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 5 | import os |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 6 | import socket |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 7 | import urllib.error |
| 8 | import urllib.request |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 9 | import sys |
Berker Peksag | b77983d | 2014-10-10 14:34:16 +0300 | [diff] [blame] | 10 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 11 | try: |
| 12 | import ssl |
| 13 | except ImportError: |
| 14 | ssl = None |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 15 | |
Berker Peksag | b77983d | 2014-10-10 14:34:16 +0300 | [diff] [blame] | 16 | requires_ssl = unittest.skipIf(ssl is None, "SSL not supported") |
| 17 | |
doko@ubuntu.com | e575148 | 2013-12-26 17:37:11 +0100 | [diff] [blame] | 18 | support.requires("network") |
| 19 | |
Senthil Kumaran | b8f7ea6 | 2010-04-20 10:35:49 +0000 | [diff] [blame] | 20 | TIMEOUT = 60 # seconds |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 21 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 22 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 23 | def _retry_thrice(func, exc, *args, **kwargs): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 24 | for i in range(3): |
| 25 | try: |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 26 | return func(*args, **kwargs) |
| 27 | except exc as e: |
Neal Norwitz | 2f14258 | 2008-01-26 19:49:41 +0000 | [diff] [blame] | 28 | last_exc = e |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 29 | continue |
| 30 | except: |
| 31 | raise |
| 32 | raise last_exc |
| 33 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 34 | def _wrap_with_retry_thrice(func, exc): |
| 35 | def wrapped(*args, **kwargs): |
| 36 | return _retry_thrice(func, exc, *args, **kwargs) |
| 37 | return wrapped |
| 38 | |
| 39 | # Connecting to remote hosts is flaky. Make it more robust by retrying |
| 40 | # the connection several times. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 41 | _urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen, |
| 42 | urllib.error.URLError) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 43 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 44 | |
| 45 | class AuthTests(unittest.TestCase): |
| 46 | """Tests urllib2 authentication features.""" |
| 47 | |
| 48 | ## Disabled at the moment since there is no page under python.org which |
| 49 | ## could be used to HTTP authentication. |
| 50 | # |
| 51 | # def test_basic_auth(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 52 | # import http.client |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 53 | # |
| 54 | # test_url = "http://www.python.org/test/test_urllib2/basic_auth" |
| 55 | # test_hostport = "www.python.org" |
| 56 | # test_realm = 'Test Realm' |
| 57 | # test_user = 'test.test_urllib2net' |
| 58 | # test_password = 'blah' |
| 59 | # |
| 60 | # # failure |
| 61 | # try: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 62 | # _urlopen_with_retry(test_url) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 63 | # except urllib2.HTTPError, exc: |
| 64 | # self.assertEqual(exc.code, 401) |
| 65 | # else: |
| 66 | # self.fail("urlopen() should have failed with 401") |
| 67 | # |
| 68 | # # success |
| 69 | # auth_handler = urllib2.HTTPBasicAuthHandler() |
| 70 | # auth_handler.add_password(test_realm, test_hostport, |
| 71 | # test_user, test_password) |
| 72 | # opener = urllib2.build_opener(auth_handler) |
| 73 | # f = opener.open('http://localhost/') |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 74 | # response = _urlopen_with_retry("http://www.python.org/") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 75 | # |
| 76 | # # The 'userinfo' URL component is deprecated by RFC 3986 for security |
| 77 | # # reasons, let's not implement it! (it's already implemented for proxy |
| 78 | # # specification strings (that is, URLs or authorities specifying a |
| 79 | # # proxy), so we must keep that) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 80 | # self.assertRaises(http.client.InvalidURL, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 81 | # urllib2.urlopen, "http://evil:thing@example.com") |
| 82 | |
| 83 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 84 | class CloseSocketTest(unittest.TestCase): |
| 85 | |
| 86 | def test_close(self): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 87 | # calling .close() on urllib2's response objects should close the |
| 88 | # underlying socket |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 89 | url = "http://www.example.com/" |
Nadeem Vawda | 61baebd | 2012-01-25 08:02:05 +0200 | [diff] [blame] | 90 | with support.transient_internet(url): |
| 91 | response = _urlopen_with_retry(url) |
| 92 | sock = response.fp |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 93 | self.assertFalse(sock.closed) |
Nadeem Vawda | 61baebd | 2012-01-25 08:02:05 +0200 | [diff] [blame] | 94 | response.close() |
| 95 | self.assertTrue(sock.closed) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 96 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 97 | class OtherNetworkTests(unittest.TestCase): |
| 98 | def setUp(self): |
| 99 | if 0: # for debugging |
| 100 | import logging |
| 101 | logger = logging.getLogger("test_urllib2net") |
| 102 | logger.addHandler(logging.StreamHandler()) |
| 103 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 104 | # XXX The rest of these tests aren't very good -- they don't check much. |
| 105 | # They do sometimes catch some major disasters, though. |
| 106 | |
| 107 | def test_ftp(self): |
| 108 | urls = [ |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 109 | 'ftp://ftp.debian.org/debian/README', |
| 110 | ('ftp://ftp.debian.org/debian/non-existent-file', |
| 111 | None, urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 112 | 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC' |
| 113 | '/research-reports/00README-Legal-Rules-Regs', |
| 114 | ] |
| 115 | self._test_urls(urls, self._extra_handlers()) |
| 116 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 117 | def test_file(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 118 | TESTFN = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 119 | f = open(TESTFN, 'w') |
| 120 | try: |
| 121 | f.write('hi there\n') |
| 122 | f.close() |
| 123 | urls = [ |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 124 | 'file:' + sanepathname2url(os.path.abspath(TESTFN)), |
| 125 | ('file:///nonsensename/etc/passwd', None, |
| 126 | urllib.error.URLError), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 127 | ] |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 128 | self._test_urls(urls, self._extra_handlers(), retry=True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | finally: |
| 130 | os.remove(TESTFN) |
| 131 | |
Senthil Kumaran | 3800ea9 | 2012-01-21 11:52:48 +0800 | [diff] [blame] | 132 | self.assertRaises(ValueError, urllib.request.urlopen,'./relative_path/to/file') |
| 133 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 134 | # XXX Following test depends on machine configurations that are internal |
| 135 | # to CNRI. Need to set up a public server with the right authentication |
| 136 | # configuration for test purposes. |
| 137 | |
| 138 | ## def test_cnri(self): |
| 139 | ## if socket.gethostname() == 'bitdiddle': |
| 140 | ## localhost = 'bitdiddle.cnri.reston.va.us' |
| 141 | ## elif socket.gethostname() == 'bitdiddle.concentric.net': |
| 142 | ## localhost = 'localhost' |
| 143 | ## else: |
| 144 | ## localhost = None |
| 145 | ## if localhost is not None: |
| 146 | ## urls = [ |
| 147 | ## 'file://%s/etc/passwd' % localhost, |
| 148 | ## 'http://%s/simple/' % localhost, |
| 149 | ## 'http://%s/digest/' % localhost, |
| 150 | ## 'http://%s/not/found.h' % localhost, |
| 151 | ## ] |
| 152 | |
| 153 | ## bauth = HTTPBasicAuthHandler() |
| 154 | ## bauth.add_password('basic_test_realm', localhost, 'jhylton', |
| 155 | ## 'password') |
| 156 | ## dauth = HTTPDigestAuthHandler() |
| 157 | ## dauth.add_password('digest_test_realm', localhost, 'jhylton', |
| 158 | ## 'password') |
| 159 | |
| 160 | ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) |
| 161 | |
Berker Peksag | b77983d | 2014-10-10 14:34:16 +0300 | [diff] [blame] | 162 | @requires_ssl |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 163 | def test_urlwithfrag(self): |
Benjamin Peterson | b4be376 | 2014-03-31 13:44:53 -0400 | [diff] [blame] | 164 | urlwith_frag = "https://docs.python.org/2/glossary.html#glossary" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 165 | with support.transient_internet(urlwith_frag): |
| 166 | req = urllib.request.Request(urlwith_frag) |
| 167 | res = urllib.request.urlopen(req) |
| 168 | self.assertEqual(res.geturl(), |
Benjamin Peterson | b4be376 | 2014-03-31 13:44:53 -0400 | [diff] [blame] | 169 | "https://docs.python.org/2/glossary.html#glossary") |
Senthil Kumaran | d95cc75 | 2010-08-08 11:27:53 +0000 | [diff] [blame] | 170 | |
Berker Peksag | b77983d | 2014-10-10 14:34:16 +0300 | [diff] [blame] | 171 | @requires_ssl |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 172 | def test_redirect_url_withfrag(self): |
Benjamin Peterson | 809ee90 | 2014-03-31 13:50:34 -0400 | [diff] [blame] | 173 | redirect_url_with_frag = "http://bit.ly/1iSHToT" |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 174 | with support.transient_internet(redirect_url_with_frag): |
| 175 | req = urllib.request.Request(redirect_url_with_frag) |
| 176 | res = urllib.request.urlopen(req) |
| 177 | self.assertEqual(res.geturl(), |
Benjamin Peterson | 809ee90 | 2014-03-31 13:50:34 -0400 | [diff] [blame] | 178 | "https://docs.python.org/3.4/glossary.html#term-global-interpreter-lock") |
Senthil Kumaran | 8307075 | 2013-05-24 09:14:12 -0700 | [diff] [blame] | 179 | |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 180 | def test_custom_headers(self): |
| 181 | url = "http://www.example.com" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 182 | with support.transient_internet(url): |
| 183 | opener = urllib.request.build_opener() |
| 184 | request = urllib.request.Request(url) |
| 185 | self.assertFalse(request.header_items()) |
| 186 | opener.open(request) |
| 187 | self.assertTrue(request.header_items()) |
| 188 | self.assertTrue(request.has_header('User-agent')) |
| 189 | request.add_header('User-Agent','Test-Agent') |
| 190 | opener.open(request) |
| 191 | self.assertEqual(request.get_header('User-agent'),'Test-Agent') |
Senthil Kumaran | 42ef4b1 | 2010-09-27 01:26:03 +0000 | [diff] [blame] | 192 | |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 193 | def test_sites_no_connection_close(self): |
| 194 | # Some sites do not send Connection: close header. |
| 195 | # Verify that those work properly. (#issue12576) |
| 196 | |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 197 | URL = 'http://www.imdb.com' # mangles Connection:close |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 198 | |
Senthil Kumaran | e324c57 | 2011-07-31 11:45:14 +0800 | [diff] [blame] | 199 | with support.transient_internet(URL): |
| 200 | try: |
| 201 | with urllib.request.urlopen(URL) as res: |
| 202 | pass |
| 203 | except ValueError as e: |
| 204 | self.fail("urlopen failed for site not sending \ |
| 205 | Connection:close") |
| 206 | else: |
| 207 | self.assertTrue(res) |
| 208 | |
| 209 | req = urllib.request.urlopen(URL) |
| 210 | res = req.read() |
| 211 | self.assertTrue(res) |
Senthil Kumaran | 1299a8f | 2011-07-27 08:05:58 +0800 | [diff] [blame] | 212 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 213 | def _test_urls(self, urls, handlers, retry=True): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 214 | import time |
| 215 | import logging |
| 216 | debug = logging.getLogger("test_urllib2").debug |
| 217 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 218 | urlopen = urllib.request.build_opener(*handlers).open |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 219 | if retry: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 220 | urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 221 | |
| 222 | for url in urls: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 223 | with self.subTest(url=url): |
| 224 | if isinstance(url, tuple): |
| 225 | url, req, expected_err = url |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 226 | else: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 227 | req = expected_err = None |
| 228 | |
| 229 | with support.transient_internet(url): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 230 | try: |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 231 | f = urlopen(url, req, TIMEOUT) |
Berker Peksag | 8b63d3a | 2014-10-25 05:42:30 +0300 | [diff] [blame] | 232 | # urllib.error.URLError is a subclass of OSError |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 233 | except OSError as err: |
| 234 | if expected_err: |
| 235 | msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
| 236 | (expected_err, url, req, type(err), err)) |
| 237 | self.assertIsInstance(err, expected_err, msg) |
| 238 | else: |
| 239 | raise |
Antoine Pitrou | bc2c4c9 | 2014-09-17 00:39:21 +0200 | [diff] [blame] | 240 | else: |
| 241 | try: |
| 242 | with support.time_out, \ |
| 243 | support.socket_peer_reset, \ |
| 244 | support.ioerror_peer_reset: |
| 245 | buf = f.read() |
| 246 | debug("read %d bytes" % len(buf)) |
| 247 | except socket.timeout: |
| 248 | print("<timeout: %s>" % url, file=sys.stderr) |
| 249 | f.close() |
| 250 | time.sleep(0.1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 251 | |
| 252 | def _extra_handlers(self): |
| 253 | handlers = [] |
| 254 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 255 | cfh = urllib.request.CacheFTPHandler() |
Nadeem Vawda | 08f5f7a | 2011-07-23 14:03:00 +0200 | [diff] [blame] | 256 | self.addCleanup(cfh.clear_cache) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 257 | cfh.setTimeout(1) |
| 258 | handlers.append(cfh) |
| 259 | |
| 260 | return handlers |
| 261 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 263 | class TimeoutTest(unittest.TestCase): |
| 264 | def test_http_basic(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 265 | self.assertIsNone(socket.getdefaulttimeout()) |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 266 | url = "http://www.example.com" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 267 | with support.transient_internet(url, timeout=None): |
| 268 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 269 | self.addCleanup(u.close) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 270 | self.assertIsNone(u.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 271 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 272 | def test_http_default_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 273 | self.assertIsNone(socket.getdefaulttimeout()) |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 274 | url = "http://www.example.com" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 275 | with support.transient_internet(url): |
| 276 | socket.setdefaulttimeout(60) |
| 277 | try: |
| 278 | u = _urlopen_with_retry(url) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 279 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 280 | finally: |
| 281 | socket.setdefaulttimeout(None) |
| 282 | self.assertEqual(u.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 283 | |
| 284 | def test_http_no_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 285 | self.assertIsNone(socket.getdefaulttimeout()) |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 286 | url = "http://www.example.com" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 287 | with support.transient_internet(url): |
| 288 | socket.setdefaulttimeout(60) |
| 289 | try: |
| 290 | u = _urlopen_with_retry(url, timeout=None) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 291 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 292 | finally: |
| 293 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 294 | self.assertIsNone(u.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 295 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 296 | def test_http_timeout(self): |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 297 | url = "http://www.example.com" |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 298 | with support.transient_internet(url): |
| 299 | u = _urlopen_with_retry(url, timeout=120) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 300 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 301 | self.assertEqual(u.fp.raw._sock.gettimeout(), 120) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 302 | |
Benjamin Peterson | 87cb787 | 2010-04-11 21:59:57 +0000 | [diff] [blame] | 303 | FTP_HOST = "ftp://ftp.mirror.nl/pub/gnu/" |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 305 | def test_ftp_basic(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 306 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 307 | with support.transient_internet(self.FTP_HOST, timeout=None): |
| 308 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 309 | self.addCleanup(u.close) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 310 | self.assertIsNone(u.fp.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 311 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 312 | def test_ftp_default_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 313 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 314 | with support.transient_internet(self.FTP_HOST): |
| 315 | socket.setdefaulttimeout(60) |
| 316 | try: |
| 317 | u = _urlopen_with_retry(self.FTP_HOST) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 318 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 319 | finally: |
| 320 | socket.setdefaulttimeout(None) |
| 321 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 322 | |
| 323 | def test_ftp_no_timeout(self): |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 324 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 325 | with support.transient_internet(self.FTP_HOST): |
| 326 | socket.setdefaulttimeout(60) |
| 327 | try: |
| 328 | u = _urlopen_with_retry(self.FTP_HOST, timeout=None) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 329 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 330 | finally: |
| 331 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 332 | self.assertIsNone(u.fp.fp.raw._sock.gettimeout()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 333 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 334 | def test_ftp_timeout(self): |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 335 | with support.transient_internet(self.FTP_HOST): |
| 336 | u = _urlopen_with_retry(self.FTP_HOST, timeout=60) |
Victor Stinner | eaca5c8 | 2011-06-17 14:53:02 +0200 | [diff] [blame] | 337 | self.addCleanup(u.close) |
Georg Brandl | 5be365f | 2010-10-28 14:55:02 +0000 | [diff] [blame] | 338 | self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 339 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 340 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 341 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 342 | unittest.main() |