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
@@ -483,6 +484,7 @@ def build_test_opener(*handler_instances):
483
484
opener .add_handler (h )
484
485
return opener
485
486
487
+
486
488
class MockHTTPHandler (urllib .request .HTTPHandler ):
487
489
# Very simple mock HTTP handler with no special behavior other than using a mock HTTP connection
488
490
@@ -493,6 +495,7 @@ def __init__(self, debuglevel=None):
493
495
def http_open (self , req ):
494
496
return self .do_open (self .httpconn , req )
495
497
498
+
496
499
class MockHTTPHandlerRedirect (urllib .request .BaseHandler ):
497
500
# useful for testing redirections and auth
498
501
# sends supplied headers and code as first response
@@ -1058,34 +1061,34 @@ def test_http_body_array(self):
1058
1061
self .assertEqual (int (newreq .get_header ('Content-length' )),16 )
1059
1062
1060
1063
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 )
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 )
1067
1070
1068
1071
def test_http_handler_local_debuglevel (self ):
1069
1072
o = OpenerDirector ()
1070
- h = MockHTTPHandler (debuglevel = 1 )
1073
+ h = MockHTTPHandler (debuglevel = 5 )
1071
1074
o .add_handler (h )
1072
1075
o .open ("http://www.example.com" )
1073
- self .assertEqual (h ._debuglevel , 1 )
1076
+ self .assertEqual (h ._debuglevel , 5 )
1074
1077
1075
1078
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 )
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 )
1082
1085
1083
1086
def test_https_handler_local_debuglevel (self ):
1084
1087
o = OpenerDirector ()
1085
- h = MockHTTPSHandler (debuglevel = 1 )
1088
+ h = MockHTTPSHandler (debuglevel = 4 )
1086
1089
o .add_handler (h )
1087
1090
o .open ("https://www.example.com" )
1088
- self .assertEqual (h ._debuglevel , 1 )
1091
+ self .assertEqual (h ._debuglevel , 4 )
1089
1092
1090
1093
def test_http_doubleslash (self ):
1091
1094
# Checks the presence of any unnecessary double slash in url does not
0 commit comments