@@ -60,15 +60,19 @@ def finish_request(self, request, client_address):
6060 """
6161 request .settimeout (1.0 )
6262 try :
63- ssock = ssl .wrap_socket (
64- request ,
65- keyfile = os .path .join (CERT_DIR , "key.pem" ),
66- certfile = os .path .join (CERT_DIR , "cert.pem" ),
67- server_side = True ,
68- suppress_ragged_eofs = False ,
63+ context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
64+ context .load_cert_chain (
65+ os .path .join (CERT_DIR , "cert.pem" ),
66+ os .path .join (CERT_DIR , "key.pem" ),
6967 )
70- self .base_environ ["HTTPS" ] = "yes"
71- self .RequestHandlerClass (ssock , client_address , self )
68+ ssock = context .wrap_socket (
69+ request , server_side = True , suppress_ragged_eofs = False
70+ )
71+ try :
72+ self .base_environ ["HTTPS" ] = "yes"
73+ self .RequestHandlerClass (ssock , client_address , self )
74+ finally :
75+ ssock .close ()
7276 except Exception as e :
7377 print ("pytest-httpbin server hit an exception serving request: %s" % e )
7478 print ("attempting to ignore so the rest of the tests can run" )
@@ -106,6 +110,16 @@ def __del__(self):
106110 def start (self ):
107111 self ._thread .start ()
108112
113+ def __enter__ (self ):
114+ self .start ()
115+ return self
116+
117+ def __exit__ (self , * args , ** kwargs ):
118+ self .stop ()
119+ suppress_exc = self ._server .__exit__ (* args , ** kwargs )
120+ self ._thread .join ()
121+ return suppress_exc
122+
109123 def __add__ (self , other ):
110124 return self .url + other
111125
0 commit comments