blob: 8eec3ad09204dd4b7ab3925ceaaabd1ee737b587 [file] [log] [blame]
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00001import unittest
2from test import test_support
Georg Brandl1b06a1d2006-05-03 05:15:10 +00003from test.test_urllib2 import sanepathname2url
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00004
5import socket
6import urllib2
Jeremy Hylton5d9c3032004-08-07 17:40:50 +00007import os
Senthil Kumaran281b5512010-04-20 06:54:59 +00008import sys
9
10TIMEOUT = 60 # seconds
Jeremy Hylton5d9c3032004-08-07 17:40:50 +000011
Neal Norwitz769d0ee2008-01-25 06:37:23 +000012
Facundo Batista6a5a1772008-06-07 13:36:36 +000013def _retry_thrice(func, exc, *args, **kwargs):
Neal Norwitz769d0ee2008-01-25 06:37:23 +000014 for i in range(3):
15 try:
Facundo Batista6a5a1772008-06-07 13:36:36 +000016 return func(*args, **kwargs)
17 except exc, last_exc:
Neal Norwitz769d0ee2008-01-25 06:37:23 +000018 continue
19 except:
20 raise
21 raise last_exc
22
Facundo Batista6a5a1772008-06-07 13:36:36 +000023def _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 Norwitz769d0ee2008-01-25 06:37:23 +000031
Georg Brandlfa42bd72006-04-30 07:06:11 +000032
33class 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 Norwitz769d0ee2008-01-25 06:37:23 +000050# _urlopen_with_retry(test_url)
Georg Brandlfa42bd72006-04-30 07:06:11 +000051# 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 Norwitz769d0ee2008-01-25 06:37:23 +000062# response = _urlopen_with_retry("http://www.python.org/")
Georg Brandlfa42bd72006-04-30 07:06:11 +000063#
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 Brandldd7b0522007-01-21 10:35:10 +000072class CloseSocketTest(unittest.TestCase):
73
74 def test_close(self):
Georg Brandla4f46e12010-02-07 17:03:15 +000075 import httplib
Georg Brandldd7b0522007-01-21 10:35:10 +000076
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 Deilyc7275332014-03-26 23:25:02 -070081 response = _urlopen_with_retry("http://www.example.com/")
Georg Brandldd7b0522007-01-21 10:35:10 +000082 abused_fileobject = response.fp
Serhiy Storchaka528bed82014-02-08 14:49:55 +020083 self.assertIs(abused_fileobject.__class__, socket._fileobject)
Georg Brandldd7b0522007-01-21 10:35:10 +000084 httpresponse = abused_fileobject._sock
Serhiy Storchaka528bed82014-02-08 14:49:55 +020085 self.assertIs(httpresponse.__class__, httplib.HTTPResponse)
Georg Brandldd7b0522007-01-21 10:35:10 +000086 fileobject = httpresponse.fp
Serhiy Storchaka528bed82014-02-08 14:49:55 +020087 self.assertIs(fileobject.__class__, socket._fileobject)
Georg Brandldd7b0522007-01-21 10:35:10 +000088
Benjamin Peterson5c8da862009-06-30 22:57:08 +000089 self.assertTrue(not fileobject.closed)
Georg Brandldd7b0522007-01-21 10:35:10 +000090 response.close()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000091 self.assertTrue(fileobject.closed)
Georg Brandldd7b0522007-01-21 10:35:10 +000092
Georg Brandl1b06a1d2006-05-03 05:15:10 +000093class 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 Brandl1b06a1d2006-05-03 05:15:10 +0000100 # 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 Stinner9a46eb72015-04-07 12:59:14 +0200105 'ftp://ftp.debian.org/debian/README',
106 ('ftp://ftp.debian.org/debian/non-existent-file',
107 None, urllib2.URLError),
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000108 ]
109 self._test_urls(urls, self._extra_handlers())
110
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000111 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. Smithe9fef692007-09-09 23:36:46 +0000119 ('file:///nonsensename/etc/passwd', None, urllib2.URLError),
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000120 ]
Facundo Batista6a5a1772008-06-07 13:36:36 +0000121 self._test_urls(urls, self._extra_handlers(), retry=True)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000122 finally:
123 os.remove(TESTFN)
124
Senthil Kumaran58c60622012-01-21 11:43:02 +0800125 self.assertRaises(ValueError, urllib2.urlopen,'./relative_path/to/file')
126
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000127 # 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 Kumaranb4ec7ee2010-08-08 11:43:45 +0000155 def test_urlwithfrag(self):
Benjamin Peterson95805102014-11-05 11:27:14 -0500156 urlwith_frag = "http://www.pythontest.net/index.html#frag"
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000157 with test_support.transient_internet(urlwith_frag):
158 req = urllib2.Request(urlwith_frag)
159 res = urllib2.urlopen(req)
160 self.assertEqual(res.geturl(),
Benjamin Peterson95805102014-11-05 11:27:14 -0500161 "http://www.pythontest.net/index.html#frag")
Senthil Kumaranb4ec7ee2010-08-08 11:43:45 +0000162
Senthil Kumarand389cb52010-09-21 01:38:15 +0000163 def test_fileno(self):
Ned Deilyc7275332014-03-26 23:25:02 -0700164 req = urllib2.Request("http://www.example.com")
Senthil Kumarand389cb52010-09-21 01:38:15 +0000165 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 Kumaran176c73d2010-09-27 01:40:59 +0000174 def test_custom_headers(self):
175 url = "http://www.example.com"
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000176 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 Kumaran176c73d2010-09-27 01:40:59 +0000186
Senthil Kumaran7d7702b2011-07-27 09:37:17 +0800187 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 Kumaran23c21042011-07-31 11:48:54 +0800191 URL = 'http://www.imdb.com' # No Connection:close
Benjamin Peterson3facb8c2011-07-30 23:39:39 -0500192 with test_support.transient_internet(URL):
Senthil Kumaran23c21042011-07-31 11:48:54 +0800193 req = urllib2.urlopen(URL)
194 res = req.read()
195 self.assertTrue(res)
Senthil Kumaran7d7702b2011-07-27 09:37:17 +0800196
Facundo Batista6a5a1772008-06-07 13:36:36 +0000197 def _test_urls(self, urls, handlers, retry=True):
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000198 import time
199 import logging
200 debug = logging.getLogger("test_urllib2").debug
201
Facundo Batista6a5a1772008-06-07 13:36:36 +0000202 urlopen = urllib2.build_opener(*handlers).open
203 if retry:
204 urlopen = _wrap_with_retry_thrice(urlopen, urllib2.URLError)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000205
206 for url in urls:
207 if isinstance(url, tuple):
208 url, req, expected_err = url
209 else:
210 req = expected_err = None
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000211 with test_support.transient_internet(url):
212 debug(url)
Senthil Kumaran281b5512010-04-20 06:54:59 +0000213 try:
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000214 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 Brandl1b06a1d2006-05-03 05:15:10 +0000235 debug("******** next url coming up...")
236 time.sleep(0.1)
237
238 def _extra_handlers(self):
239 handlers = []
240
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000241 cfh = urllib2.CacheFTPHandler()
Nadeem Vawdab42c53e2011-07-23 15:51:16 +0200242 self.addCleanup(cfh.clear_cache)
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000243 cfh.setTimeout(1)
244 handlers.append(cfh)
245
246 return handlers
247
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000248
Facundo Batista10951d52007-06-06 17:15:23 +0000249class TimeoutTest(unittest.TestCase):
250 def test_http_basic(self):
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200251 self.assertIsNone(socket.getdefaulttimeout())
Ned Deilyc7275332014-03-26 23:25:02 -0700252 url = "http://www.example.com"
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000253 with test_support.transient_internet(url, timeout=None):
254 u = _urlopen_with_retry(url)
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200255 self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
Facundo Batista10951d52007-06-06 17:15:23 +0000256
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000257 def test_http_default_timeout(self):
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200258 self.assertIsNone(socket.getdefaulttimeout())
Ned Deilyc7275332014-03-26 23:25:02 -0700259 url = "http://www.example.com"
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000260 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 Batista4f1b1ed2008-05-29 16:39:26 +0000267
268 def test_http_no_timeout(self):
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200269 self.assertIsNone(socket.getdefaulttimeout())
Ned Deilyc7275332014-03-26 23:25:02 -0700270 url = "http://www.example.com"
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000271 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 Storchaka528bed82014-02-08 14:49:55 +0200277 self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
Facundo Batista10951d52007-06-06 17:15:23 +0000278
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000279 def test_http_timeout(self):
Ned Deilyc7275332014-03-26 23:25:02 -0700280 url = "http://www.example.com"
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000281 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 Batista10951d52007-06-06 17:15:23 +0000284
Victor Stinner9a46eb72015-04-07 12:59:14 +0200285 FTP_HOST = 'ftp://ftp.debian.org/debian/'
Neal Norwitz769d0ee2008-01-25 06:37:23 +0000286
Facundo Batista10951d52007-06-06 17:15:23 +0000287 def test_ftp_basic(self):
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200288 self.assertIsNone(socket.getdefaulttimeout())
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000289 with test_support.transient_internet(self.FTP_HOST, timeout=None):
290 u = _urlopen_with_retry(self.FTP_HOST)
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200291 self.assertIsNone(u.fp.fp._sock.gettimeout())
Facundo Batista10951d52007-06-06 17:15:23 +0000292
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000293 def test_ftp_default_timeout(self):
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200294 self.assertIsNone(socket.getdefaulttimeout())
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000295 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 Batista4f1b1ed2008-05-29 16:39:26 +0000302
303 def test_ftp_no_timeout(self):
Serhiy Storchaka528bed82014-02-08 14:49:55 +0200304 self.assertIsNone(socket.getdefaulttimeout(),)
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000305 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 Storchaka528bed82014-02-08 14:49:55 +0200311 self.assertIsNone(u.fp.fp._sock.gettimeout())
Facundo Batista10951d52007-06-06 17:15:23 +0000312
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000313 def test_ftp_timeout(self):
Antoine Pitrou9f3f9c52010-10-31 13:58:00 +0000314 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 Batista10951d52007-06-06 17:15:23 +0000317
Georg Brandl1b06a1d2006-05-03 05:15:10 +0000318
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000319def test_main():
320 test_support.requires("network")
Gregory P. Smith0001c2e2008-03-28 08:00:44 +0000321 test_support.run_unittest(AuthTests,
Georg Brandldd7b0522007-01-21 10:35:10 +0000322 OtherNetworkTests,
323 CloseSocketTest,
Facundo Batista10951d52007-06-06 17:15:23 +0000324 TimeoutTest,
Georg Brandldd7b0522007-01-21 10:35:10 +0000325 )
Jeremy Hylton5d9c3032004-08-07 17:40:50 +0000326
327if __name__ == "__main__":
328 test_main()