33from test .support import os_helper
44from test .support import warnings_helper
55from test import test_urllib
6+ from unittest import mock
67
78import os
89import io
@@ -483,6 +484,7 @@ def build_test_opener(*handler_instances):
483484 opener .add_handler (h )
484485 return opener
485486
487+
486488class MockHTTPHandler (urllib .request .HTTPHandler ):
487489 # Very simple mock HTTP handler with no special behavior other than using a mock HTTP connection
488490
@@ -493,6 +495,7 @@ def __init__(self, debuglevel=None):
493495 def http_open (self , req ):
494496 return self .do_open (self .httpconn , req )
495497
498+
496499class MockHTTPHandlerRedirect (urllib .request .BaseHandler ):
497500 # useful for testing redirections and auth
498501 # sends supplied headers and code as first response
@@ -1058,34 +1061,34 @@ def test_http_body_array(self):
10581061 self .assertEqual (int (newreq .get_header ('Content-length' )),16 )
10591062
10601063 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 )
10671070
10681071 def test_http_handler_local_debuglevel (self ):
10691072 o = OpenerDirector ()
1070- h = MockHTTPHandler (debuglevel = 1 )
1073+ h = MockHTTPHandler (debuglevel = 5 )
10711074 o .add_handler (h )
10721075 o .open ("http://www.example.com" )
1073- self .assertEqual (h ._debuglevel , 1 )
1076+ self .assertEqual (h ._debuglevel , 5 )
10741077
10751078 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 )
10821085
10831086 def test_https_handler_local_debuglevel (self ):
10841087 o = OpenerDirector ()
1085- h = MockHTTPSHandler (debuglevel = 1 )
1088+ h = MockHTTPSHandler (debuglevel = 4 )
10861089 o .add_handler (h )
10871090 o .open ("https://www.example.com" )
1088- self .assertEqual (h ._debuglevel , 1 )
1091+ self .assertEqual (h ._debuglevel , 4 )
10891092
10901093 def test_http_doubleslash (self ):
10911094 # Checks the presence of any unnecessary double slash in url does not
0 commit comments