@@ -2568,6 +2568,29 @@ def _create_connection(address, timeout=None, source_address=None):
25682568 self .assertIsNotNone (exc )
25692569 self .assertTrue (sock .file_closed )
25702570
2571+ class HTTPConnectionStateTests (TestCase ):
2572+ def test_connect_timeout_resets_state (self ):
2573+ with mock .patch ('socket.create_connection' , side_effect = TimeoutError ("timed out" )):
2574+ conn = client .HTTPConnection ('10.255.255.1' , 80 , timeout = 0.01 )
2575+
2576+ with self .assertRaises (TimeoutError ):
2577+ conn .request ('GET' , '/' )
2578+
2579+ self .assertEqual (conn ._HTTPConnection__state , client ._CS_IDLE )
2580+ self .assertIsNone (conn .sock )
2581+
2582+ with mock .patch ('socket.create_connection' ) as mock_cc :
2583+ fake_sock = mock .Mock ()
2584+ mock_cc .return_value = fake_sock
2585+ # Provide a working socket for the second request.
2586+ conn .request ("GET" , "/second" )
2587+
2588+ # Ensure the connection is in a usable state (either idle or
2589+ # request-sent depending on response handling).
2590+ self .assertIn (conn ._HTTPConnection__state ,
2591+ (client ._CS_REQ_SENT , client ._CS_IDLE ))
2592+
25712593
25722594if __name__ == '__main__' :
2573- unittest .main (verbosity = 2 )
2595+ unittest .main ()
2596+
0 commit comments