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