271
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
273
def __init__(self, server_address, request_handler_class,
275
BaseHTTPServer.HTTPServer.__init__(self, server_address,
276
request_handler_class)
272
class TestingHTTPServerWrapper(object):
273
"""Isolate the wrapper itself to make the server use transparent.
275
Daughter classes can override any method and/or directly call the _server
279
def __init__(self, server_class, test_case_server,
280
server_address, request_handler_class):
281
self._server = server_class(server_address, request_handler_class)
277
282
# test_case_server can be used to communicate between the
278
283
# tests and the server (or the request handler and the
279
284
# server), allowing dynamic behaviors to be defined from
280
285
# the tests cases.
281
self.test_case_server = test_case_server
286
self._server.test_case_server = test_case_server
288
def __getattr__(self, name):
289
return getattr(self._server, name)
291
def server_bind(self):
292
"""Override server_bind to store the server name."""
293
self._server.server_bind()
294
host, port = self._server.socket.getsockname()[:2]
295
self._server.server_name = socket.getfqdn(host)
296
self._server.server_port = port
283
298
def server_close(self):
284
"""Called to clean-up the server.
286
Since the server may be in a blocking read, we shutdown the socket
289
self.socket.shutdown(socket.SHUT_RDWR)
290
BaseHTTPServer.HTTPServer.server_close(self)
299
"""Called to clean-up the server.
301
Since the server may be (surely is, even) in a blocking listen, we
302
shutdown its socket before closing it.
304
# Note that is this executed as part of the implicit tear down in the
305
# main thread while the server runs in its own thread. The clean way
306
# to tear down the server will be to instruct him to stop accepting
307
# connections and wait for the current connection to end naturally. To
308
# end the connection naturally, the http transports should close their
309
# socket when they do not need to talk to the server anymore. We
310
# don't want to impose such a constraint on the http transports (and
311
# we can't anyway ;). So we must tear down here, from the main thread,
312
# when the test have ended. Note that since the server is in a
313
# blocking operation and since python use select internally, shutting
314
# down the socket is reliable and relatively clean.
315
self._server.socket.shutdown(socket.SHUT_RDWR)
316
# Let the server properly close the socket
317
self._server.server_close()
319
class TestingHTTPServer(TestingHTTPServerWrapper):
321
def __init__(self, server_address, request_handler_class, test_case_server):
322
super(TestingHTTPServer, self).__init__(
323
SocketServer.TCPServer, test_case_server,
324
server_address, request_handler_class)
327
class TestingThreadingHTTPServer(TestingHTTPServerWrapper):
328
"""A threading HTTP test server for HTTP 1.1.
330
Since tests can initiate several concurrent connections to the same http
331
server, we need an independent connection for each of them. We achieve that
332
by spawning a new thread for each connection.
335
def __init__(self, server_address, request_handler_class, test_case_server):
336
super(TestingThreadingHTTPServer, self).__init__(
337
SocketServer.ThreadingTCPServer, test_case_server,
338
server_address, request_handler_class)
339
# Decides how threads will act upon termination of the main
340
# process. This is prophylactic as we should not leave the threads
342
self._server.daemon_threads = True
293
345
class HttpServer(transport.Server):