3
3
from test .support import os_helper
4
4
from test .support import warnings_helper
5
5
from test import test_urllib
6
+ from unittest import mock
6
7
7
8
import os
8
9
import io
@@ -484,7 +485,18 @@ def build_test_opener(*handler_instances):
484
485
return opener
485
486
486
487
487
- class MockHTTPHandler (urllib .request .BaseHandler ):
488
+ class MockHTTPHandler (urllib .request .HTTPHandler ):
489
+ # Very simple mock HTTP handler with no special behavior other than using a mock HTTP connection
490
+
491
+ def __init__ (self , debuglevel = None ):
492
+ super (MockHTTPHandler , self ).__init__ (debuglevel = debuglevel )
493
+ self .httpconn = MockHTTPClass ()
494
+
495
+ def http_open (self , req ):
496
+ return self .do_open (self .httpconn , req )
497
+
498
+
499
+ class MockHTTPHandlerRedirect (urllib .request .BaseHandler ):
488
500
# useful for testing redirections and auth
489
501
# sends supplied headers and code as first response
490
502
# sends 200 OK as second response
@@ -512,12 +524,12 @@ def http_open(self, req):
512
524
return MockResponse (200 , "OK" , msg , "" , req .get_full_url ())
513
525
514
526
515
- class MockHTTPSHandler (urllib .request .AbstractHTTPHandler ):
527
+ class MockHTTPSHandler (urllib .request .HTTPSHandler ):
516
528
# Useful for testing the Proxy-Authorization request by verifying the
517
529
# properties of httpcon
518
530
519
- def __init__ (self , debuglevel = 0 ):
520
- urllib . request . AbstractHTTPHandler . __init__ (self , debuglevel = debuglevel )
531
+ def __init__ (self , debuglevel = None , context = None , check_hostname = None ):
532
+ super ( MockHTTPSHandler , self ). __init__ (debuglevel , context , check_hostname )
521
533
self .httpconn = MockHTTPClass ()
522
534
523
535
def https_open (self , req ):
@@ -1048,12 +1060,35 @@ def test_http_body_array(self):
1048
1060
newreq = h .do_request_ (req )
1049
1061
self .assertEqual (int (newreq .get_header ('Content-length' )),16 )
1050
1062
1051
- def test_http_handler_debuglevel (self ):
1063
+ def test_http_handler_global_debuglevel (self ):
1064
+ with mock .patch .object (http .client .HTTPConnection , 'debuglevel' , 6 ):
1065
+ o = OpenerDirector ()
1066
+ h = MockHTTPHandler ()
1067
+ o .add_handler (h )
1068
+ o .open ("http://www.example.com" )
1069
+ self .assertEqual (h ._debuglevel , 6 )
1070
+
1071
+ def test_http_handler_local_debuglevel (self ):
1072
+ o = OpenerDirector ()
1073
+ h = MockHTTPHandler (debuglevel = 5 )
1074
+ o .add_handler (h )
1075
+ o .open ("http://www.example.com" )
1076
+ self .assertEqual (h ._debuglevel , 5 )
1077
+
1078
+ def test_https_handler_global_debuglevel (self ):
1079
+ with mock .patch .object (http .client .HTTPSConnection , 'debuglevel' , 7 ):
1080
+ o = OpenerDirector ()
1081
+ h = MockHTTPSHandler ()
1082
+ o .add_handler (h )
1083
+ o .open ("https://www.example.com" )
1084
+ self .assertEqual (h ._debuglevel , 7 )
1085
+
1086
+ def test_https_handler_local_debuglevel (self ):
1052
1087
o = OpenerDirector ()
1053
- h = MockHTTPSHandler (debuglevel = 1 )
1088
+ h = MockHTTPSHandler (debuglevel = 4 )
1054
1089
o .add_handler (h )
1055
1090
o .open ("https://www.example.com" )
1056
- self .assertEqual (h ._debuglevel , 1 )
1091
+ self .assertEqual (h ._debuglevel , 4 )
1057
1092
1058
1093
def test_http_doubleslash (self ):
1059
1094
# Checks the presence of any unnecessary double slash in url does not
@@ -1289,7 +1324,7 @@ def test_cookie_redirect(self):
1289
1324
1290
1325
cj = CookieJar ()
1291
1326
interact_netscape (cj , "http://www.example.com/" , "spam=eggs" )
1292
- hh = MockHTTPHandler (302 , "Location: http://www.cracker.com/\r \n \r \n " )
1327
+ hh = MockHTTPHandlerRedirect (302 , "Location: http://www.cracker.com/\r \n \r \n " )
1293
1328
hdeh = urllib .request .HTTPDefaultErrorHandler ()
1294
1329
hrh = urllib .request .HTTPRedirectHandler ()
1295
1330
cp = urllib .request .HTTPCookieProcessor (cj )
@@ -1299,7 +1334,7 @@ def test_cookie_redirect(self):
1299
1334
1300
1335
def test_redirect_fragment (self ):
1301
1336
redirected_url = 'http://www.example.com/index.html#OK\r \n \r \n '
1302
- hh = MockHTTPHandler (302 , 'Location: ' + redirected_url )
1337
+ hh = MockHTTPHandlerRedirect (302 , 'Location: ' + redirected_url )
1303
1338
hdeh = urllib .request .HTTPDefaultErrorHandler ()
1304
1339
hrh = urllib .request .HTTPRedirectHandler ()
1305
1340
o = build_test_opener (hh , hdeh , hrh )
@@ -1484,7 +1519,7 @@ def check_basic_auth(self, headers, realm):
1484
1519
password_manager = MockPasswordManager ()
1485
1520
auth_handler = urllib .request .HTTPBasicAuthHandler (password_manager )
1486
1521
body = '\r \n ' .join (headers ) + '\r \n \r \n '
1487
- http_handler = MockHTTPHandler (401 , body )
1522
+ http_handler = MockHTTPHandlerRedirect (401 , body )
1488
1523
opener .add_handler (auth_handler )
1489
1524
opener .add_handler (http_handler )
1490
1525
self ._test_basic_auth (opener , auth_handler , "Authorization" ,
@@ -1544,7 +1579,7 @@ def test_proxy_basic_auth(self):
1544
1579
password_manager = MockPasswordManager ()
1545
1580
auth_handler = urllib .request .ProxyBasicAuthHandler (password_manager )
1546
1581
realm = "ACME Networks"
1547
- http_handler = MockHTTPHandler (
1582
+ http_handler = MockHTTPHandlerRedirect (
1548
1583
407 , 'Proxy-Authenticate: Basic realm="%s"\r \n \r \n ' % realm )
1549
1584
opener .add_handler (auth_handler )
1550
1585
opener .add_handler (http_handler )
@@ -1588,7 +1623,7 @@ def http_error_401(self, *args, **kwds):
1588
1623
digest_handler = TestDigestAuthHandler (password_manager )
1589
1624
basic_handler = TestBasicAuthHandler (password_manager )
1590
1625
realm = "ACME Networks"
1591
- http_handler = MockHTTPHandler (
1626
+ http_handler = MockHTTPHandlerRedirect (
1592
1627
401 , 'WWW-Authenticate: Basic realm="%s"\r \n \r \n ' % realm )
1593
1628
opener .add_handler (basic_handler )
1594
1629
opener .add_handler (digest_handler )
@@ -1608,7 +1643,7 @@ def test_unsupported_auth_digest_handler(self):
1608
1643
opener = OpenerDirector ()
1609
1644
# While using DigestAuthHandler
1610
1645
digest_auth_handler = urllib .request .HTTPDigestAuthHandler (None )
1611
- http_handler = MockHTTPHandler (
1646
+ http_handler = MockHTTPHandlerRedirect (
1612
1647
401 , 'WWW-Authenticate: Kerberos\r \n \r \n ' )
1613
1648
opener .add_handler (digest_auth_handler )
1614
1649
opener .add_handler (http_handler )
@@ -1618,7 +1653,7 @@ def test_unsupported_auth_basic_handler(self):
1618
1653
# While using BasicAuthHandler
1619
1654
opener = OpenerDirector ()
1620
1655
basic_auth_handler = urllib .request .HTTPBasicAuthHandler (None )
1621
- http_handler = MockHTTPHandler (
1656
+ http_handler = MockHTTPHandlerRedirect (
1622
1657
401 , 'WWW-Authenticate: NTLM\r \n \r \n ' )
1623
1658
opener .add_handler (basic_auth_handler )
1624
1659
opener .add_handler (http_handler )
@@ -1705,7 +1740,7 @@ def test_basic_prior_auth_send_after_first_success(self):
1705
1740
opener = OpenerDirector ()
1706
1741
opener .add_handler (auth_prior_handler )
1707
1742
1708
- http_handler = MockHTTPHandler (
1743
+ http_handler = MockHTTPHandlerRedirect (
1709
1744
401 , 'WWW-Authenticate: Basic realm="%s"\r \n \r \n ' % None )
1710
1745
opener .add_handler (http_handler )
1711
1746
0 commit comments