Skip to content

Commit 518d0e2

Browse files
committed
add tests
1 parent 932fe4c commit 518d0e2

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

Lib/test/test_urllib2.py

+45-13
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,17 @@ def build_test_opener(*handler_instances):
483483
opener.add_handler(h)
484484
return opener
485485

486+
class MockHTTPHandler(urllib.request.HTTPHandler):
487+
# Very simple mock HTTP handler with no special behavior other than using a mock HTTP connection
486488

487-
class MockHTTPHandler(urllib.request.BaseHandler):
489+
def __init__(self, debuglevel=None):
490+
super(MockHTTPHandler, self).__init__(debuglevel=debuglevel)
491+
self.httpconn = MockHTTPClass()
492+
493+
def http_open(self, req):
494+
return self.do_open(self.httpconn, req)
495+
496+
class MockHTTPHandlerRedirect(urllib.request.BaseHandler):
488497
# useful for testing redirections and auth
489498
# sends supplied headers and code as first response
490499
# sends 200 OK as second response
@@ -512,12 +521,12 @@ def http_open(self, req):
512521
return MockResponse(200, "OK", msg, "", req.get_full_url())
513522

514523

515-
class MockHTTPSHandler(urllib.request.AbstractHTTPHandler):
524+
class MockHTTPSHandler(urllib.request.HTTPSHandler):
516525
# Useful for testing the Proxy-Authorization request by verifying the
517526
# properties of httpcon
518527

519-
def __init__(self, debuglevel=0):
520-
urllib.request.AbstractHTTPHandler.__init__(self, debuglevel=debuglevel)
528+
def __init__(self, debuglevel=None, context=None, check_hostname=None):
529+
super(MockHTTPSHandler, self).__init__(debuglevel, context, check_hostname)
521530
self.httpconn = MockHTTPClass()
522531

523532
def https_open(self, req):
@@ -1048,7 +1057,30 @@ def test_http_body_array(self):
10481057
newreq = h.do_request_(req)
10491058
self.assertEqual(int(newreq.get_header('Content-length')),16)
10501059

1051-
def test_http_handler_debuglevel(self):
1060+
def test_http_handler_global_debuglevel(self):
1061+
http.client.HTTPConnection.debuglevel = 1
1062+
o = OpenerDirector()
1063+
h = MockHTTPHandler()
1064+
o.add_handler(h)
1065+
o.open("http://www.example.com")
1066+
self.assertEqual(h._debuglevel, 1)
1067+
1068+
def test_http_handler_local_debuglevel(self):
1069+
o = OpenerDirector()
1070+
h = MockHTTPHandler(debuglevel=1)
1071+
o.add_handler(h)
1072+
o.open("http://www.example.com")
1073+
self.assertEqual(h._debuglevel, 1)
1074+
1075+
def test_https_handler_global_debuglevel(self):
1076+
http.client.HTTPSConnection.debuglevel = 1
1077+
o = OpenerDirector()
1078+
h = MockHTTPSHandler()
1079+
o.add_handler(h)
1080+
o.open("https://www.example.com")
1081+
self.assertEqual(h._debuglevel, 1)
1082+
1083+
def test_https_handler_local_debuglevel(self):
10521084
o = OpenerDirector()
10531085
h = MockHTTPSHandler(debuglevel=1)
10541086
o.add_handler(h)
@@ -1289,7 +1321,7 @@ def test_cookie_redirect(self):
12891321

12901322
cj = CookieJar()
12911323
interact_netscape(cj, "http://www.example.com/", "spam=eggs")
1292-
hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
1324+
hh = MockHTTPHandlerRedirect(302, "Location: http://www.cracker.com/\r\n\r\n")
12931325
hdeh = urllib.request.HTTPDefaultErrorHandler()
12941326
hrh = urllib.request.HTTPRedirectHandler()
12951327
cp = urllib.request.HTTPCookieProcessor(cj)
@@ -1299,7 +1331,7 @@ def test_cookie_redirect(self):
12991331

13001332
def test_redirect_fragment(self):
13011333
redirected_url = 'http://www.example.com/index.html#OK\r\n\r\n'
1302-
hh = MockHTTPHandler(302, 'Location: ' + redirected_url)
1334+
hh = MockHTTPHandlerRedirect(302, 'Location: ' + redirected_url)
13031335
hdeh = urllib.request.HTTPDefaultErrorHandler()
13041336
hrh = urllib.request.HTTPRedirectHandler()
13051337
o = build_test_opener(hh, hdeh, hrh)
@@ -1484,7 +1516,7 @@ def check_basic_auth(self, headers, realm):
14841516
password_manager = MockPasswordManager()
14851517
auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
14861518
body = '\r\n'.join(headers) + '\r\n\r\n'
1487-
http_handler = MockHTTPHandler(401, body)
1519+
http_handler = MockHTTPHandlerRedirect(401, body)
14881520
opener.add_handler(auth_handler)
14891521
opener.add_handler(http_handler)
14901522
self._test_basic_auth(opener, auth_handler, "Authorization",
@@ -1544,7 +1576,7 @@ def test_proxy_basic_auth(self):
15441576
password_manager = MockPasswordManager()
15451577
auth_handler = urllib.request.ProxyBasicAuthHandler(password_manager)
15461578
realm = "ACME Networks"
1547-
http_handler = MockHTTPHandler(
1579+
http_handler = MockHTTPHandlerRedirect(
15481580
407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
15491581
opener.add_handler(auth_handler)
15501582
opener.add_handler(http_handler)
@@ -1588,7 +1620,7 @@ def http_error_401(self, *args, **kwds):
15881620
digest_handler = TestDigestAuthHandler(password_manager)
15891621
basic_handler = TestBasicAuthHandler(password_manager)
15901622
realm = "ACME Networks"
1591-
http_handler = MockHTTPHandler(
1623+
http_handler = MockHTTPHandlerRedirect(
15921624
401, 'WWW-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
15931625
opener.add_handler(basic_handler)
15941626
opener.add_handler(digest_handler)
@@ -1608,7 +1640,7 @@ def test_unsupported_auth_digest_handler(self):
16081640
opener = OpenerDirector()
16091641
# While using DigestAuthHandler
16101642
digest_auth_handler = urllib.request.HTTPDigestAuthHandler(None)
1611-
http_handler = MockHTTPHandler(
1643+
http_handler = MockHTTPHandlerRedirect(
16121644
401, 'WWW-Authenticate: Kerberos\r\n\r\n')
16131645
opener.add_handler(digest_auth_handler)
16141646
opener.add_handler(http_handler)
@@ -1618,7 +1650,7 @@ def test_unsupported_auth_basic_handler(self):
16181650
# While using BasicAuthHandler
16191651
opener = OpenerDirector()
16201652
basic_auth_handler = urllib.request.HTTPBasicAuthHandler(None)
1621-
http_handler = MockHTTPHandler(
1653+
http_handler = MockHTTPHandlerRedirect(
16221654
401, 'WWW-Authenticate: NTLM\r\n\r\n')
16231655
opener.add_handler(basic_auth_handler)
16241656
opener.add_handler(http_handler)
@@ -1705,7 +1737,7 @@ def test_basic_prior_auth_send_after_first_success(self):
17051737
opener = OpenerDirector()
17061738
opener.add_handler(auth_prior_handler)
17071739

1708-
http_handler = MockHTTPHandler(
1740+
http_handler = MockHTTPHandlerRedirect(
17091741
401, 'WWW-Authenticate: Basic realm="%s"\r\n\r\n' % None)
17101742
opener.add_handler(http_handler)
17111743

0 commit comments

Comments
 (0)