@@ -576,12 +576,12 @@ def setUp(self):
576576 def tearDown (self ):
577577 try :
578578 os .chdir (self .cwd )
579- try :
580- shutil .rmtree (self .tempdir )
581- except :
582- pass
583579 finally :
584580 super ().tearDown ()
581+ try :
582+ shutil .rmtree (self .tempdir )
583+ except :
584+ pass
585585
586586 def check_status_and_reason (self , response , status , data = None ):
587587 def close_conn ():
@@ -608,6 +608,15 @@ def close_conn():
608608 reader .close ()
609609 return body
610610
611+ def check_status_and_headers (self , response , status , headers = None ):
612+ # Drain the body so the server-side handler can close the file
613+ # before tearDown removes the tempdir (matters on Windows).
614+ response .read ()
615+ self .assertEqual (response .status , status )
616+ if headers :
617+ for name , value in headers .items ():
618+ self .assertEqual (response .getheader (name ), value )
619+
611620 def check_list_dir_dirname (self , dirname , quotedname = None ):
612621 fullpath = os .path .join (self .tempdir , dirname )
613622 try :
@@ -912,9 +921,10 @@ def test_extra_response_headers_list_dir(self):
912921 ('X-Test2' , 'test2' ),
913922 ]):
914923 response = self .request (self .base_url + '/' )
915- self .assertEqual (response .status , 200 )
916- self .assertEqual (response .getheader ("X-Test1" ), 'test1' )
917- self .assertEqual (response .getheader ("X-Test2" ), 'test2' )
924+ self .check_status_and_headers (response , HTTPStatus .OK , {
925+ "X-Test1" : "test1" ,
926+ "X-Test2" : "test2" ,
927+ })
918928
919929 def test_extra_response_headers_get_file (self ):
920930 with mock .patch .object (self .request_handler , 'extra_response_headers' , [
@@ -926,18 +936,19 @@ def test_extra_response_headers_get_file(self):
926936 with open (os .path .join (self .tempdir_name , 'index.html' ), 'wb' ) as f :
927937 f .write (data )
928938 response = self .request (self .base_url + '/' )
929- self .assertEqual (response . status , 200 )
930- self . assertEqual ( response . getheader ( "Set-Cookie" ) ,
931- 'test1=value1, test2=value2' )
932- self . assertEqual ( response . getheader ( "X-Test1" ), 'value3' )
939+ self .check_status_and_headers (response , HTTPStatus . OK , {
940+ "Set-Cookie" : "test1=value1, test2=value2" ,
941+ "X-Test1" : "value3" ,
942+ } )
933943
934944 def test_extra_response_headers_missing_on_404 (self ):
935945 with mock .patch .object (self .request_handler , 'extra_response_headers' , [
936946 ('X-Test1' , 'value' ),
937947 ]):
938948 response = self .request (self .base_url + '/missing.html' )
939- self .assertEqual (response .status , 404 )
940- self .assertEqual (response .getheader ("X-Test1" ), None )
949+ self .check_status_and_headers (response , HTTPStatus .NOT_FOUND , {
950+ "X-Test1" : None ,
951+ })
941952
942953 def test_extra_response_headers_dont_overwrite_default_headers (self ):
943954 with mock .patch .object (self .request_handler , 'extra_response_headers' , [
@@ -949,21 +960,21 @@ def test_extra_response_headers_dont_overwrite_default_headers(self):
949960 # But cookies in the extra_allowed_duplicate_headers are allowed,
950961 # including Set-Cookie
951962 response = self .request (self .base_url + '/' )
952- self .assertEqual (response .status , 200 )
963+ self .check_status_and_headers (response , HTTPStatus .OK , {
964+ "Set-Cookie" : "test=allowed" ,
965+ })
953966 self .assertNotEqual (response .getheader ("Content-Type" ), 'test/not_allowed' )
954967 self .assertNotEqual (response .getheader ("Server" ), 'not_allowed' )
955- self .assertEqual (response .getheader ("Set-Cookie" ), 'test=allowed' )
956968
957969 def test_multiple_requests_dont_duplicate_extra_response_headers (self ):
958970 with mock .patch .object (self .request_handler , 'extra_response_headers' , [
959971 ('x-test' , 'test-value' ),
960972 ]):
961- response = self .request (self .base_url + '/' )
962- self .assertEqual (response .status , 200 )
963- self .assertEqual (response .getheader ("x-test" ), 'test-value' )
964- response = self .request (self .base_url + '/' )
965- self .assertEqual (response .status , 200 )
966- self .assertEqual (response .getheader ("x-test" ), 'test-value' )
973+ for _ in range (2 ):
974+ response = self .request (self .base_url + '/' )
975+ self .check_status_and_headers (response , HTTPStatus .OK , {
976+ "x-test" : "test-value" ,
977+ })
967978
968979
969980class SocketlessRequestHandler (SimpleHTTPRequestHandler ):
0 commit comments