@@ -1623,35 +1623,29 @@ def test_unknown_flag(self, _):
16231623 self .assertEqual (stdout .getvalue (), '' )
16241624 self .assertIn ('error' , stderr .getvalue ())
16251625
1626- @mock .patch ('http.server._make_server' , wraps = server . _make_server )
1627- @ mock . patch . object ( HTTPServer , 'serve_forever' )
1628- def test_extra_response_headers_arg ( self , _ , mock_make_server ):
1626+ @mock .patch ('http.server.test' )
1627+ def test_extra_response_headers_arg ( self , mock_test ):
1628+ # Call the main function with extra response headers cli args
16291629 server ._main (
16301630 ['-H' , 'Set-Cookie' , 'k=v' , '-H' , 'Set-Cookie' , 'k2=v2:v3 v4' , '8080' ]
16311631 )
1632- # Get an instance of the server / RequestHandler by using
1633- # the spied call args, then calling _make_server with them.
1634- args , kwargs = mock_make_server .call_args
1635- httpd = server ._make_server (* args , ** kwargs )
1636- self .addCleanup (httpd .server_close )
1637-
1638- # Ensure the RequestHandler class is passed the correct response
1639- # headers
1640- request_handler_class = httpd .RequestHandlerClass
1641- with mock .patch .object (
1642- request_handler_class , '__init__'
1643- ) as mock_handler_init :
1644- mock_handler_init .return_value = None
1645- # finish_request instantiates a request handler class,
1646- # ensure extra_response_headers are passed to it
1647- httpd .finish_request (mock .Mock (), '127.0.0.1' )
1648- mock_handler_init .assert_called_once_with (
1649- mock .ANY , mock .ANY , mock .ANY ,
1650- directory = mock .ANY ,
1651- extra_response_headers = [
1652- ['Set-Cookie' , 'k=v' ], ['Set-Cookie' , 'k2=v2:v3 v4' ]
1653- ]
1654- )
1632+ # Get the ServerClass (DualStackServerMixin subclass) that _main()
1633+ # passed to test(), and verify its finish_request passes
1634+ # extra_response_headers to the handler.
1635+ _ , kwargs = mock_test .call_args
1636+ server_class = kwargs ['ServerClass' ]
1637+
1638+ mock_handler_class = mock .MagicMock ()
1639+ mock_server = mock .Mock ()
1640+ mock_server .RequestHandlerClass = mock_handler_class
1641+ server_class .finish_request (mock_server , mock .Mock (), '127.0.0.1' )
1642+ mock_handler_class .assert_called_once_with (
1643+ mock .ANY , mock .ANY , mock_server ,
1644+ directory = mock .ANY ,
1645+ extra_response_headers = [
1646+ ['Set-Cookie' , 'k=v' ], ['Set-Cookie' , 'k2=v2:v3 v4' ]
1647+ ]
1648+ )
16551649
16561650
16571651class CommandLineRunTimeTestCase (unittest .TestCase ):
0 commit comments