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