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 = [ |
Gregory P. Smith | e9fef69 | 2007-09-09 23:36:46 +0000 | [diff] [blame] | 105 | 'ftp://ftp.kernel.org/pub/linux/kernel/README', |
Mark Dickinson | 3e4caeb | 2009-02-21 20:27:01 +0000 | [diff] [blame] | 106 | 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file', |
Gregory P. Smith | e9fef69 | 2007-09-09 23:36:46 +0000 | [diff] [blame] | 107 | #'ftp://ftp.kernel.org/pub/leenox/kernel/test', |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 108 | 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC' |
| 109 | '/research-reports/00README-Legal-Rules-Regs', |
| 110 | ] |
| 111 | self._test_urls(urls, self._extra_handlers()) |
| 112 | |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 113 | def test_file(self): |
| 114 | TESTFN = test_support.TESTFN |
| 115 | f = open(TESTFN, 'w') |
| 116 | try: |
| 117 | f.write('hi there\n') |
| 118 | f.close() |
| 119 | urls = [ |
| 120 | 'file:'+sanepathname2url(os.path.abspath(TESTFN)), |
Gregory P. Smith | e9fef69 | 2007-09-09 23:36:46 +0000 | [diff] [blame] | 121 | ('file:///nonsensename/etc/passwd', None, urllib2.URLError), |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 122 | ] |
Facundo Batista | 6a5a177 | 2008-06-07 13:36:36 +0000 | [diff] [blame] | 123 | self._test_urls(urls, self._extra_handlers(), retry=True) |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 124 | finally: |
| 125 | os.remove(TESTFN) |
| 126 | |
Senthil Kumaran | 58c6062 | 2012-01-21 11:43:02 +0800 | [diff] [blame] | 127 | self.assertRaises(ValueError, urllib2.urlopen,'./relative_path/to/file') |
| 128 | |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 129 | # XXX Following test depends on machine configurations that are internal |
| 130 | # to CNRI. Need to set up a public server with the right authentication |
| 131 | # configuration for test purposes. |
| 132 | |
| 133 | ## def test_cnri(self): |
| 134 | ## if socket.gethostname() == 'bitdiddle': |
| 135 | ## localhost = 'bitdiddle.cnri.reston.va.us' |
| 136 | ## elif socket.gethostname() == 'bitdiddle.concentric.net': |
| 137 | ## localhost = 'localhost' |
| 138 | ## else: |
| 139 | ## localhost = None |
| 140 | ## if localhost is not None: |
| 141 | ## urls = [ |
| 142 | ## 'file://%s/etc/passwd' % localhost, |
| 143 | ## 'http://%s/simple/' % localhost, |
| 144 | ## 'http://%s/digest/' % localhost, |
| 145 | ## 'http://%s/not/found.h' % localhost, |
| 146 | ## ] |
| 147 | |
| 148 | ## bauth = HTTPBasicAuthHandler() |
| 149 | ## bauth.add_password('basic_test_realm', localhost, 'jhylton', |
| 150 | ## 'password') |
| 151 | ## dauth = HTTPDigestAuthHandler() |
| 152 | ## dauth.add_password('digest_test_realm', localhost, 'jhylton', |
| 153 | ## 'password') |
| 154 | |
| 155 | ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) |
| 156 | |
Senthil Kumaran | b4ec7ee | 2010-08-08 11:43:45 +0000 | [diff] [blame] | 157 | def test_urlwithfrag(self): |
Benjamin Peterson | 3853438 | 2014-03-31 13:44:53 -0400 | [diff] [blame] | 158 | urlwith_frag = "https://docs.python.org/2/glossary.html#glossary" |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 159 | with test_support.transient_internet(urlwith_frag): |
| 160 | req = urllib2.Request(urlwith_frag) |
| 161 | res = urllib2.urlopen(req) |
| 162 | self.assertEqual(res.geturl(), |
Benjamin Peterson | 3853438 | 2014-03-31 13:44:53 -0400 | [diff] [blame] | 163 | "https://docs.python.org/2/glossary.html#glossary") |
Senthil Kumaran | b4ec7ee | 2010-08-08 11:43:45 +0000 | [diff] [blame] | 164 | |
Senthil Kumaran | d389cb5 | 2010-09-21 01:38:15 +0000 | [diff] [blame] | 165 | def test_fileno(self): |
Ned Deily | c727533 | 2014-03-26 23:25:02 -0700 | [diff] [blame] | 166 | req = urllib2.Request("http://www.example.com") |
Senthil Kumaran | d389cb5 | 2010-09-21 01:38:15 +0000 | [diff] [blame] | 167 | opener = urllib2.build_opener() |
| 168 | res = opener.open(req) |
| 169 | try: |
| 170 | res.fileno() |
| 171 | except AttributeError: |
| 172 | self.fail("HTTPResponse object should return a valid fileno") |
| 173 | finally: |
| 174 | res.close() |
| 175 | |
Senthil Kumaran | 176c73d | 2010-09-27 01:40:59 +0000 | [diff] [blame] | 176 | def test_custom_headers(self): |
| 177 | url = "http://www.example.com" |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 178 | with test_support.transient_internet(url): |
| 179 | opener = urllib2.build_opener() |
| 180 | request = urllib2.Request(url) |
| 181 | self.assertFalse(request.header_items()) |
| 182 | opener.open(request) |
| 183 | self.assertTrue(request.header_items()) |
| 184 | self.assertTrue(request.has_header('User-agent')) |
| 185 | request.add_header('User-Agent','Test-Agent') |
| 186 | opener.open(request) |
| 187 | self.assertEqual(request.get_header('User-agent'),'Test-Agent') |
Senthil Kumaran | 176c73d | 2010-09-27 01:40:59 +0000 | [diff] [blame] | 188 | |
Senthil Kumaran | 7d7702b | 2011-07-27 09:37:17 +0800 | [diff] [blame] | 189 | def test_sites_no_connection_close(self): |
| 190 | # Some sites do not send Connection: close header. |
| 191 | # Verify that those work properly. (#issue12576) |
| 192 | |
Senthil Kumaran | 23c2104 | 2011-07-31 11:48:54 +0800 | [diff] [blame] | 193 | URL = 'http://www.imdb.com' # No Connection:close |
Benjamin Peterson | 3facb8c | 2011-07-30 23:39:39 -0500 | [diff] [blame] | 194 | with test_support.transient_internet(URL): |
Senthil Kumaran | 23c2104 | 2011-07-31 11:48:54 +0800 | [diff] [blame] | 195 | req = urllib2.urlopen(URL) |
| 196 | res = req.read() |
| 197 | self.assertTrue(res) |
Senthil Kumaran | 7d7702b | 2011-07-27 09:37:17 +0800 | [diff] [blame] | 198 | |
Facundo Batista | 6a5a177 | 2008-06-07 13:36:36 +0000 | [diff] [blame] | 199 | def _test_urls(self, urls, handlers, retry=True): |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 200 | import time |
| 201 | import logging |
| 202 | debug = logging.getLogger("test_urllib2").debug |
| 203 | |
Facundo Batista | 6a5a177 | 2008-06-07 13:36:36 +0000 | [diff] [blame] | 204 | urlopen = urllib2.build_opener(*handlers).open |
| 205 | if retry: |
| 206 | urlopen = _wrap_with_retry_thrice(urlopen, urllib2.URLError) |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 207 | |
| 208 | for url in urls: |
| 209 | if isinstance(url, tuple): |
| 210 | url, req, expected_err = url |
| 211 | else: |
| 212 | req = expected_err = None |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 213 | with test_support.transient_internet(url): |
| 214 | debug(url) |
Senthil Kumaran | 281b551 | 2010-04-20 06:54:59 +0000 | [diff] [blame] | 215 | try: |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 216 | f = urlopen(url, req, TIMEOUT) |
| 217 | except EnvironmentError as err: |
| 218 | debug(err) |
| 219 | if expected_err: |
| 220 | msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % |
| 221 | (expected_err, url, req, type(err), err)) |
| 222 | self.assertIsInstance(err, expected_err, msg) |
| 223 | except urllib2.URLError as err: |
| 224 | if isinstance(err[0], socket.timeout): |
| 225 | print >>sys.stderr, "<timeout: %s>" % url |
| 226 | continue |
| 227 | else: |
| 228 | raise |
| 229 | else: |
| 230 | try: |
| 231 | with test_support.transient_internet(url): |
| 232 | buf = f.read() |
| 233 | debug("read %d bytes" % len(buf)) |
| 234 | except socket.timeout: |
| 235 | print >>sys.stderr, "<timeout: %s>" % url |
| 236 | f.close() |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 237 | debug("******** next url coming up...") |
| 238 | time.sleep(0.1) |
| 239 | |
| 240 | def _extra_handlers(self): |
| 241 | handlers = [] |
| 242 | |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 243 | cfh = urllib2.CacheFTPHandler() |
Nadeem Vawda | b42c53e | 2011-07-23 15:51:16 +0200 | [diff] [blame] | 244 | self.addCleanup(cfh.clear_cache) |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 245 | cfh.setTimeout(1) |
| 246 | handlers.append(cfh) |
| 247 | |
| 248 | return handlers |
| 249 | |
Gregory P. Smith | 0001c2e | 2008-03-28 08:00:44 +0000 | [diff] [blame] | 250 | |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 251 | class TimeoutTest(unittest.TestCase): |
| 252 | def test_http_basic(self): |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 253 | self.assertIsNone(socket.getdefaulttimeout()) |
Ned Deily | c727533 | 2014-03-26 23:25:02 -0700 | [diff] [blame] | 254 | url = "http://www.example.com" |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 255 | with test_support.transient_internet(url, timeout=None): |
| 256 | u = _urlopen_with_retry(url) |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 257 | self.assertIsNone(u.fp._sock.fp._sock.gettimeout()) |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 258 | |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 259 | def test_http_default_timeout(self): |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 260 | self.assertIsNone(socket.getdefaulttimeout()) |
Ned Deily | c727533 | 2014-03-26 23:25:02 -0700 | [diff] [blame] | 261 | url = "http://www.example.com" |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 262 | with test_support.transient_internet(url): |
| 263 | socket.setdefaulttimeout(60) |
| 264 | try: |
| 265 | u = _urlopen_with_retry(url) |
| 266 | finally: |
| 267 | socket.setdefaulttimeout(None) |
| 268 | self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 60) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 269 | |
| 270 | def test_http_no_timeout(self): |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 271 | self.assertIsNone(socket.getdefaulttimeout()) |
Ned Deily | c727533 | 2014-03-26 23:25:02 -0700 | [diff] [blame] | 272 | url = "http://www.example.com" |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 273 | with test_support.transient_internet(url): |
| 274 | socket.setdefaulttimeout(60) |
| 275 | try: |
| 276 | u = _urlopen_with_retry(url, timeout=None) |
| 277 | finally: |
| 278 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 279 | self.assertIsNone(u.fp._sock.fp._sock.gettimeout()) |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 280 | |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 281 | def test_http_timeout(self): |
Ned Deily | c727533 | 2014-03-26 23:25:02 -0700 | [diff] [blame] | 282 | url = "http://www.example.com" |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 283 | with test_support.transient_internet(url): |
| 284 | u = _urlopen_with_retry(url, timeout=120) |
| 285 | self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120) |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 286 | |
Martin v. Löwis | 7bc26b9 | 2010-04-08 17:40:54 +0000 | [diff] [blame] | 287 | FTP_HOST = "ftp://ftp.mirror.nl/pub/gnu/" |
Neal Norwitz | 769d0ee | 2008-01-25 06:37:23 +0000 | [diff] [blame] | 288 | |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 289 | def test_ftp_basic(self): |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 290 | self.assertIsNone(socket.getdefaulttimeout()) |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 291 | with test_support.transient_internet(self.FTP_HOST, timeout=None): |
| 292 | u = _urlopen_with_retry(self.FTP_HOST) |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 293 | self.assertIsNone(u.fp.fp._sock.gettimeout()) |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 294 | |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 295 | def test_ftp_default_timeout(self): |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 296 | self.assertIsNone(socket.getdefaulttimeout()) |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 297 | with test_support.transient_internet(self.FTP_HOST): |
| 298 | socket.setdefaulttimeout(60) |
| 299 | try: |
| 300 | u = _urlopen_with_retry(self.FTP_HOST) |
| 301 | finally: |
| 302 | socket.setdefaulttimeout(None) |
| 303 | self.assertEqual(u.fp.fp._sock.gettimeout(), 60) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 304 | |
| 305 | def test_ftp_no_timeout(self): |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 306 | self.assertIsNone(socket.getdefaulttimeout(),) |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 307 | with test_support.transient_internet(self.FTP_HOST): |
| 308 | socket.setdefaulttimeout(60) |
| 309 | try: |
| 310 | u = _urlopen_with_retry(self.FTP_HOST, timeout=None) |
| 311 | finally: |
| 312 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 313 | self.assertIsNone(u.fp.fp._sock.gettimeout()) |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 314 | |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 315 | def test_ftp_timeout(self): |
Antoine Pitrou | 9f3f9c5 | 2010-10-31 13:58:00 +0000 | [diff] [blame] | 316 | with test_support.transient_internet(self.FTP_HOST): |
| 317 | u = _urlopen_with_retry(self.FTP_HOST, timeout=60) |
| 318 | self.assertEqual(u.fp.fp._sock.gettimeout(), 60) |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 319 | |
Georg Brandl | 1b06a1d | 2006-05-03 05:15:10 +0000 | [diff] [blame] | 320 | |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 321 | def test_main(): |
| 322 | test_support.requires("network") |
Gregory P. Smith | 0001c2e | 2008-03-28 08:00:44 +0000 | [diff] [blame] | 323 | test_support.run_unittest(AuthTests, |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 324 | OtherNetworkTests, |
| 325 | CloseSocketTest, |
Facundo Batista | 10951d5 | 2007-06-06 17:15:23 +0000 | [diff] [blame] | 326 | TimeoutTest, |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 327 | ) |
Jeremy Hylton | 5d9c303 | 2004-08-07 17:40:50 +0000 | [diff] [blame] | 328 | |
| 329 | if __name__ == "__main__": |
| 330 | test_main() |