66
77from six .moves .urllib .parse import urljoin
88
9- CERT_DIR = os .path .join (os .path .dirname (os .path .abspath (__file__ )), ' certs' )
9+ CERT_DIR = os .path .join (os .path .dirname (os .path .abspath (__file__ )), " certs" )
1010
1111
1212class ServerHandler (SimpleHandler ):
1313
14- server_software = ' Pytest-HTTPBIN/0.1.0'
15- http_version = ' 1.1'
14+ server_software = " Pytest-HTTPBIN/0.1.0"
15+ http_version = " 1.1"
1616
1717 def cleanup_headers (self ):
1818 SimpleHandler .cleanup_headers (self )
19- self .headers [' Connection' ] = ' Close'
19+ self .headers [" Connection" ] = " Close"
2020
2121 def close (self ):
2222 try :
2323 self .request_handler .log_request (
24- self .status .split (' ' , 1 )[0 ], self .bytes_sent
24+ self .status .split (" " , 1 )[0 ], self .bytes_sent
2525 )
2626 finally :
2727 SimpleHandler .close (self )
2828
2929
3030class Handler (WSGIRequestHandler ):
31-
3231 def handle (self ):
3332 """Handle a single HTTP request"""
3433
@@ -39,7 +38,7 @@ def handle(self):
3938 handler = ServerHandler (
4039 self .rfile , self .wfile , self .get_stderr (), self .get_environ ()
4140 )
42- handler .request_handler = self # backpointer for logging
41+ handler .request_handler = self # backpointer for logging
4342 handler .run (self .server .get_app ())
4443
4544 def get_environ (self ):
@@ -49,13 +48,12 @@ def get_environ(self):
4948 """
5049 # Note: Can't use super since this is an oldstyle class in python 2.x
5150 environ = WSGIRequestHandler .get_environ (self ).copy ()
52- if self .headers .get (' content-type' ) is None :
53- del environ [' CONTENT_TYPE' ]
51+ if self .headers .get (" content-type" ) is None :
52+ del environ [" CONTENT_TYPE" ]
5453 return environ
5554
5655
5756class SecureWSGIServer (WSGIServer ):
58-
5957 def finish_request (self , request , client_address ):
6058 """
6159 Negotiates SSL and then mimics BaseServer behavior.
@@ -64,12 +62,12 @@ def finish_request(self, request, client_address):
6462 try :
6563 ssock = ssl .wrap_socket (
6664 request ,
67- keyfile = os .path .join (CERT_DIR , ' key.pem' ),
68- certfile = os .path .join (CERT_DIR , ' cert.pem' ),
65+ keyfile = os .path .join (CERT_DIR , " key.pem" ),
66+ certfile = os .path .join (CERT_DIR , " cert.pem" ),
6967 server_side = True ,
7068 suppress_ragged_eofs = False ,
7169 )
72- self .base_environ [' HTTPS' ] = ' yes'
70+ self .base_environ [" HTTPS" ] = " yes"
7371 self .RequestHandlerClass (ssock , client_address , self )
7472 except Exception as e :
7573 print ("pytest-httpbin server hit an exception serving request: %s" % e )
@@ -83,30 +81,26 @@ class Server(object):
8381 HTTP server running a WSGI application in its own thread.
8482 """
8583
86- port_envvar = ' HTTPBIN_HTTP_PORT'
84+ port_envvar = " HTTPBIN_HTTP_PORT"
8785
88- def __init__ (self , host = ' 127.0.0.1' , port = 0 , application = None , ** kwargs ):
86+ def __init__ (self , host = " 127.0.0.1" , port = 0 , application = None , ** kwargs ):
8987 self .app = application
9088 if self .port_envvar in os .environ :
9189 port = int (os .environ [self .port_envvar ])
9290 self ._server = make_server (
93- host ,
94- port ,
95- self .app ,
96- handler_class = Handler ,
97- ** kwargs
91+ host , port , self .app , handler_class = Handler , ** kwargs
9892 )
9993 self .host = self ._server .server_address [0 ]
10094 self .port = self ._server .server_address [1 ]
101- self .protocol = ' http'
95+ self .protocol = " http"
10296
10397 self ._thread = threading .Thread (
10498 name = self .__class__ ,
10599 target = self ._server .serve_forever ,
106100 )
107101
108102 def __del__ (self ):
109- if hasattr (self , ' _server' ):
103+ if hasattr (self , " _server" ):
110104 self .stop ()
111105
112106 def start (self ):
@@ -120,16 +114,16 @@ def stop(self):
120114
121115 @property
122116 def url (self ):
123- return ' {0}://{1}:{2}' .format (self .protocol , self .host , self .port )
117+ return " {0}://{1}:{2}" .format (self .protocol , self .host , self .port )
124118
125119 def join (self , url , allow_fragments = True ):
126120 return urljoin (self .url , url , allow_fragments = allow_fragments )
127121
128122
129123class SecureServer (Server ):
130- port_envvar = ' HTTPBIN_HTTPS_PORT'
124+ port_envvar = " HTTPBIN_HTTPS_PORT"
131125
132- def __init__ (self , host = ' 127.0.0.1' , port = 0 , application = None , ** kwargs ):
133- kwargs [' server_class' ] = SecureWSGIServer
126+ def __init__ (self , host = " 127.0.0.1" , port = 0 , application = None , ** kwargs ):
127+ kwargs [" server_class" ] = SecureWSGIServer
134128 super (SecureServer , self ).__init__ (host , port , application , ** kwargs )
135- self .protocol = ' https'
129+ self .protocol = " https"
0 commit comments