]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_urllib2net.py
4 from test
import test_support
5 from test
.test_urllib2
import sanepathname2url
12 TIMEOUT
= 60 # seconds
15 def _retry_thrice(func
, exc
, *args
, **kwargs
):
18 return func(*args
, **kwargs
)
25 def _wrap_with_retry_thrice(func
, exc
):
26 def wrapped(*args
, **kwargs
):
27 return _retry_thrice(func
, exc
, *args
, **kwargs
)
30 # Connecting to remote hosts is flaky. Make it more robust by retrying
31 # the connection several times.
32 _urlopen_with_retry
= _wrap_with_retry_thrice(urllib2
.urlopen
, urllib2
.URLError
)
35 class AuthTests(unittest
.TestCase
):
36 """Tests urllib2 authentication features."""
38 ## Disabled at the moment since there is no page under python.org which
39 ## could be used to HTTP authentication.
41 # def test_basic_auth(self):
44 # test_url = "http://www.python.org/test/test_urllib2/basic_auth"
45 # test_hostport = "www.python.org"
46 # test_realm = 'Test Realm'
47 # test_user = 'test.test_urllib2net'
48 # test_password = 'blah'
52 # _urlopen_with_retry(test_url)
53 # except urllib2.HTTPError, exc:
54 # self.assertEqual(exc.code, 401)
56 # self.fail("urlopen() should have failed with 401")
59 # auth_handler = urllib2.HTTPBasicAuthHandler()
60 # auth_handler.add_password(test_realm, test_hostport,
61 # test_user, test_password)
62 # opener = urllib2.build_opener(auth_handler)
63 # f = opener.open('http://localhost/')
64 # response = _urlopen_with_retry("http://www.python.org/")
66 # # The 'userinfo' URL component is deprecated by RFC 3986 for security
67 # # reasons, let's not implement it! (it's already implemented for proxy
68 # # specification strings (that is, URLs or authorities specifying a
69 # # proxy), so we must keep that)
70 # self.assertRaises(httplib.InvalidURL,
71 # urllib2.urlopen, "http://evil:thing@example.com")
74 class CloseSocketTest(unittest
.TestCase
):
79 # calling .close() on urllib2's response objects should close the
82 # delve deep into response to fetch socket._socketobject
83 response
= _urlopen_with_retry("http://www.python.org/")
84 abused_fileobject
= response
.fp
85 self
.assertTrue(abused_fileobject
.__class
__ is socket
._fileobject
)
86 httpresponse
= abused_fileobject
._sock
87 self
.assertTrue(httpresponse
.__class
__ is httplib
.HTTPResponse
)
88 fileobject
= httpresponse
.fp
89 self
.assertTrue(fileobject
.__class
__ is socket
._fileobject
)
91 self
.assertTrue(not fileobject
.closed
)
93 self
.assertTrue(fileobject
.closed
)
95 class OtherNetworkTests(unittest
.TestCase
):
99 logger
= logging
.getLogger("test_urllib2net")
100 logger
.addHandler(logging
.StreamHandler())
102 # XXX The rest of these tests aren't very good -- they don't check much.
103 # They do sometimes catch some major disasters, though.
107 'ftp://ftp.kernel.org/pub/linux/kernel/README',
108 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file',
109 #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
110 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
111 '/research-reports/00README-Legal-Rules-Regs',
113 self
._test
_urls
(urls
, self
._extra
_handlers
())
116 TESTFN
= test_support
.TESTFN
117 f
= open(TESTFN
, 'w')
119 f
.write('hi there\n')
122 'file:'+sanepathname2url(os
.path
.abspath(TESTFN
)),
123 ('file:///nonsensename/etc/passwd', None, urllib2
.URLError
),
125 self
._test
_urls
(urls
, self
._extra
_handlers
(), retry
=True)
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.
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'
140 ## if localhost is not None:
142 ## 'file://%s/etc/passwd' % localhost,
143 ## 'http://%s/simple/' % localhost,
144 ## 'http://%s/digest/' % localhost,
145 ## 'http://%s/not/found.h' % localhost,
148 ## bauth = HTTPBasicAuthHandler()
149 ## bauth.add_password('basic_test_realm', localhost, 'jhylton',
151 ## dauth = HTTPDigestAuthHandler()
152 ## dauth.add_password('digest_test_realm', localhost, 'jhylton',
155 ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
157 def test_urlwithfrag(self
):
158 urlwith_frag
= "http://docs.python.org/glossary.html#glossary"
159 with test_support
.transient_internet(urlwith_frag
):
160 req
= urllib2
.Request(urlwith_frag
)
161 res
= urllib2
.urlopen(req
)
162 self
.assertEqual(res
.geturl(),
163 "http://docs.python.org/glossary.html#glossary")
165 def test_fileno(self
):
166 req
= urllib2
.Request("http://www.python.org")
167 opener
= urllib2
.build_opener()
168 res
= opener
.open(req
)
171 except AttributeError:
172 self
.fail("HTTPResponse object should return a valid fileno")
176 def test_custom_headers(self
):
177 url
= "http://www.example.com"
178 with test_support
.transient_internet(url
):
179 opener
= urllib2
.build_opener()
180 request
= urllib2
.Request(url
)
181 self
.assertFalse(request
.header_items())
183 self
.assertTrue(request
.header_items())
184 self
.assertTrue(request
.has_header('User-agent'))
185 request
.add_header('User-Agent','Test-Agent')
187 self
.assertEqual(request
.get_header('User-agent'),'Test-Agent')
189 def _test_urls(self
, urls
, handlers
, retry
=True):
192 debug
= logging
.getLogger("test_urllib2").debug
194 urlopen
= urllib2
.build_opener(*handlers
).open
196 urlopen
= _wrap_with_retry_thrice(urlopen
, urllib2
.URLError
)
199 if isinstance(url
, tuple):
200 url
, req
, expected_err
= url
202 req
= expected_err
= None
203 with test_support
.transient_internet(url
):
206 f
= urlopen(url
, req
, TIMEOUT
)
207 except EnvironmentError as err
:
210 msg
= ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
211 (expected_err
, url
, req
, type(err
), err
))
212 self
.assertIsInstance(err
, expected_err
, msg
)
213 except urllib2
.URLError
as err
:
214 if isinstance(err
[0], socket
.timeout
):
215 print >>sys
.stderr
, "<timeout: %s>" % url
221 with test_support
.transient_internet(url
):
223 debug("read %d bytes" % len(buf
))
224 except socket
.timeout
:
225 print >>sys
.stderr
, "<timeout: %s>" % url
227 debug("******** next url coming up...")
230 def _extra_handlers(self
):
233 cfh
= urllib2
.CacheFTPHandler()
240 class TimeoutTest(unittest
.TestCase
):
241 def test_http_basic(self
):
242 self
.assertTrue(socket
.getdefaulttimeout() is None)
243 url
= "http://www.python.org"
244 with test_support
.transient_internet(url
, timeout
=None):
245 u
= _urlopen_with_retry(url
)
246 self
.assertTrue(u
.fp
._sock
.fp
._sock
.gettimeout() is None)
248 def test_http_default_timeout(self
):
249 self
.assertTrue(socket
.getdefaulttimeout() is None)
250 url
= "http://www.python.org"
251 with test_support
.transient_internet(url
):
252 socket
.setdefaulttimeout(60)
254 u
= _urlopen_with_retry(url
)
256 socket
.setdefaulttimeout(None)
257 self
.assertEqual(u
.fp
._sock
.fp
._sock
.gettimeout(), 60)
259 def test_http_no_timeout(self
):
260 self
.assertTrue(socket
.getdefaulttimeout() is None)
261 url
= "http://www.python.org"
262 with test_support
.transient_internet(url
):
263 socket
.setdefaulttimeout(60)
265 u
= _urlopen_with_retry(url
, timeout
=None)
267 socket
.setdefaulttimeout(None)
268 self
.assertTrue(u
.fp
._sock
.fp
._sock
.gettimeout() is None)
270 def test_http_timeout(self
):
271 url
= "http://www.python.org"
272 with test_support
.transient_internet(url
):
273 u
= _urlopen_with_retry(url
, timeout
=120)
274 self
.assertEqual(u
.fp
._sock
.fp
._sock
.gettimeout(), 120)
276 FTP_HOST
= "ftp://ftp.mirror.nl/pub/gnu/"
278 def test_ftp_basic(self
):
279 self
.assertTrue(socket
.getdefaulttimeout() is None)
280 with test_support
.transient_internet(self
.FTP_HOST
, timeout
=None):
281 u
= _urlopen_with_retry(self
.FTP_HOST
)
282 self
.assertTrue(u
.fp
.fp
._sock
.gettimeout() is None)
284 def test_ftp_default_timeout(self
):
285 self
.assertTrue(socket
.getdefaulttimeout() is None)
286 with test_support
.transient_internet(self
.FTP_HOST
):
287 socket
.setdefaulttimeout(60)
289 u
= _urlopen_with_retry(self
.FTP_HOST
)
291 socket
.setdefaulttimeout(None)
292 self
.assertEqual(u
.fp
.fp
._sock
.gettimeout(), 60)
294 def test_ftp_no_timeout(self
):
295 self
.assertTrue(socket
.getdefaulttimeout() is None)
296 with test_support
.transient_internet(self
.FTP_HOST
):
297 socket
.setdefaulttimeout(60)
299 u
= _urlopen_with_retry(self
.FTP_HOST
, timeout
=None)
301 socket
.setdefaulttimeout(None)
302 self
.assertTrue(u
.fp
.fp
._sock
.gettimeout() is None)
304 def test_ftp_timeout(self
):
305 with test_support
.transient_internet(self
.FTP_HOST
):
306 u
= _urlopen_with_retry(self
.FTP_HOST
, timeout
=60)
307 self
.assertEqual(u
.fp
.fp
._sock
.gettimeout(), 60)
311 test_support
.requires("network")
312 test_support
.run_unittest(AuthTests
,
318 if __name__
== "__main__":