@@ -483,8 +483,17 @@ def build_test_opener(*handler_instances):
483
483
opener .add_handler (h )
484
484
return opener
485
485
486
+ class MockHTTPHandler (urllib .request .HTTPHandler ):
487
+ # Very simple mock HTTP handler with no special behavior other than using a mock HTTP connection
486
488
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 ):
488
497
# useful for testing redirections and auth
489
498
# sends supplied headers and code as first response
490
499
# sends 200 OK as second response
@@ -512,12 +521,12 @@ def http_open(self, req):
512
521
return MockResponse (200 , "OK" , msg , "" , req .get_full_url ())
513
522
514
523
515
- class MockHTTPSHandler (urllib .request .AbstractHTTPHandler ):
524
+ class MockHTTPSHandler (urllib .request .HTTPSHandler ):
516
525
# Useful for testing the Proxy-Authorization request by verifying the
517
526
# properties of httpcon
518
527
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 )
521
530
self .httpconn = MockHTTPClass ()
522
531
523
532
def https_open (self , req ):
@@ -1048,7 +1057,30 @@ def test_http_body_array(self):
1048
1057
newreq = h .do_request_ (req )
1049
1058
self .assertEqual (int (newreq .get_header ('Content-length' )),16 )
1050
1059
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 ):
1052
1084
o = OpenerDirector ()
1053
1085
h = MockHTTPSHandler (debuglevel = 1 )
1054
1086
o .add_handler (h )
@@ -1289,7 +1321,7 @@ def test_cookie_redirect(self):
1289
1321
1290
1322
cj = CookieJar ()
1291
1323
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 " )
1293
1325
hdeh = urllib .request .HTTPDefaultErrorHandler ()
1294
1326
hrh = urllib .request .HTTPRedirectHandler ()
1295
1327
cp = urllib .request .HTTPCookieProcessor (cj )
@@ -1299,7 +1331,7 @@ def test_cookie_redirect(self):
1299
1331
1300
1332
def test_redirect_fragment (self ):
1301
1333
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 )
1303
1335
hdeh = urllib .request .HTTPDefaultErrorHandler ()
1304
1336
hrh = urllib .request .HTTPRedirectHandler ()
1305
1337
o = build_test_opener (hh , hdeh , hrh )
@@ -1484,7 +1516,7 @@ def check_basic_auth(self, headers, realm):
1484
1516
password_manager = MockPasswordManager ()
1485
1517
auth_handler = urllib .request .HTTPBasicAuthHandler (password_manager )
1486
1518
body = '\r \n ' .join (headers ) + '\r \n \r \n '
1487
- http_handler = MockHTTPHandler (401 , body )
1519
+ http_handler = MockHTTPHandlerRedirect (401 , body )
1488
1520
opener .add_handler (auth_handler )
1489
1521
opener .add_handler (http_handler )
1490
1522
self ._test_basic_auth (opener , auth_handler , "Authorization" ,
@@ -1544,7 +1576,7 @@ def test_proxy_basic_auth(self):
1544
1576
password_manager = MockPasswordManager ()
1545
1577
auth_handler = urllib .request .ProxyBasicAuthHandler (password_manager )
1546
1578
realm = "ACME Networks"
1547
- http_handler = MockHTTPHandler (
1579
+ http_handler = MockHTTPHandlerRedirect (
1548
1580
407 , 'Proxy-Authenticate: Basic realm="%s"\r \n \r \n ' % realm )
1549
1581
opener .add_handler (auth_handler )
1550
1582
opener .add_handler (http_handler )
@@ -1588,7 +1620,7 @@ def http_error_401(self, *args, **kwds):
1588
1620
digest_handler = TestDigestAuthHandler (password_manager )
1589
1621
basic_handler = TestBasicAuthHandler (password_manager )
1590
1622
realm = "ACME Networks"
1591
- http_handler = MockHTTPHandler (
1623
+ http_handler = MockHTTPHandlerRedirect (
1592
1624
401 , 'WWW-Authenticate: Basic realm="%s"\r \n \r \n ' % realm )
1593
1625
opener .add_handler (basic_handler )
1594
1626
opener .add_handler (digest_handler )
@@ -1608,7 +1640,7 @@ def test_unsupported_auth_digest_handler(self):
1608
1640
opener = OpenerDirector ()
1609
1641
# While using DigestAuthHandler
1610
1642
digest_auth_handler = urllib .request .HTTPDigestAuthHandler (None )
1611
- http_handler = MockHTTPHandler (
1643
+ http_handler = MockHTTPHandlerRedirect (
1612
1644
401 , 'WWW-Authenticate: Kerberos\r \n \r \n ' )
1613
1645
opener .add_handler (digest_auth_handler )
1614
1646
opener .add_handler (http_handler )
@@ -1618,7 +1650,7 @@ def test_unsupported_auth_basic_handler(self):
1618
1650
# While using BasicAuthHandler
1619
1651
opener = OpenerDirector ()
1620
1652
basic_auth_handler = urllib .request .HTTPBasicAuthHandler (None )
1621
- http_handler = MockHTTPHandler (
1653
+ http_handler = MockHTTPHandlerRedirect (
1622
1654
401 , 'WWW-Authenticate: NTLM\r \n \r \n ' )
1623
1655
opener .add_handler (basic_auth_handler )
1624
1656
opener .add_handler (http_handler )
@@ -1705,7 +1737,7 @@ def test_basic_prior_auth_send_after_first_success(self):
1705
1737
opener = OpenerDirector ()
1706
1738
opener .add_handler (auth_prior_handler )
1707
1739
1708
- http_handler = MockHTTPHandler (
1740
+ http_handler = MockHTTPHandlerRedirect (
1709
1741
401 , 'WWW-Authenticate: Basic realm="%s"\r \n \r \n ' % None )
1710
1742
opener .add_handler (http_handler )
1711
1743
0 commit comments