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