245
245
# The client may have already closed the socket.
248
def connect_socket(self):
249
err = socket.error('getaddrinfo returns an empty list')
250
for res in socket.getaddrinfo(self.host, self.port):
251
af, socktype, proto, canonname, sa = res
254
sock = socket.socket(af, socktype, proto)
258
except socket.error, err:
259
# err is now the most recent error
264
248
def stop_server(self):
266
250
# Issue a fake connection to wake up the server and allow it to
268
fake_conn = self.connect_socket()
252
fake_conn = osutils.connect_socket((self.host, self.port))
269
253
fake_conn.close()
270
254
except socket.error:
271
255
# We might have already closed it. We don't care.
325
309
protocol_version = 'HTTP/0.1'
327
server = http_server.HttpServer(BogusRequestHandler)
329
self.assertRaises(httplib.UnknownProtocol, server.start_server)
332
self.fail('HTTP Server creation did not raise UnknownProtocol')
311
self.assertRaises(httplib.UnknownProtocol,
312
http_server.HttpServer, BogusRequestHandler)
334
314
def test_force_invalid_protocol(self):
335
server = http_server.HttpServer(protocol_version='HTTP/0.1')
336
self.addCleanup(server.stop_server)
337
self.assertRaises(httplib.UnknownProtocol, server.start_server)
315
self.assertRaises(httplib.UnknownProtocol,
316
http_server.HttpServer, protocol_version='HTTP/0.1')
339
318
def test_server_start_and_stop(self):
340
319
server = http_server.HttpServer()
341
320
self.addCleanup(server.stop_server)
342
321
server.start_server()
343
self.assertTrue(server._httpd is not None)
344
self.assertTrue(server._httpd.serving is not None)
345
self.assertTrue(server._httpd.serving.isSet())
322
self.assertTrue(server.server is not None)
323
self.assertTrue(server.server.serving is not None)
324
self.assertTrue(server.server.serving.isSet())
347
326
def test_create_http_server_one_zero(self):
348
327
class RequestHandlerOneZero(http_server.TestingHTTPRequestHandler):
352
331
server = http_server.HttpServer(RequestHandlerOneZero)
353
332
self.start_server(server)
354
self.assertIsInstance(server._httpd, http_server.TestingHTTPServer)
333
self.assertIsInstance(server.server, http_server.TestingHTTPServer)
356
335
def test_create_http_server_one_one(self):
357
336
class RequestHandlerOneOne(http_server.TestingHTTPRequestHandler):
361
340
server = http_server.HttpServer(RequestHandlerOneOne)
362
341
self.start_server(server)
363
self.assertIsInstance(server._httpd,
342
self.assertIsInstance(server.server,
364
343
http_server.TestingThreadingHTTPServer)
366
345
def test_create_http_server_force_one_one(self):
371
350
server = http_server.HttpServer(RequestHandlerOneZero,
372
351
protocol_version='HTTP/1.1')
373
352
self.start_server(server)
374
self.assertIsInstance(server._httpd,
353
self.assertIsInstance(server.server,
375
354
http_server.TestingThreadingHTTPServer)
377
356
def test_create_http_server_force_one_zero(self):
382
361
server = http_server.HttpServer(RequestHandlerOneOne,
383
362
protocol_version='HTTP/1.0')
384
363
self.start_server(server)
385
self.assertIsInstance(server._httpd,
364
self.assertIsInstance(server.server,
386
365
http_server.TestingHTTPServer)
1753
1732
self.readfile = StringIO(socket_read_content)
1754
1733
self.writefile = StringIO()
1755
1734
self.writefile.close = lambda: None
1735
self.close = lambda: None
1757
1737
def makefile(self, mode='r', bufsize=None):
1758
1738
if 'r' in mode:
1808
1788
self.assertEqual(expected_reply_body, reply_body)
1810
1790
def test_smart_http_server_post_request_handler(self):
1811
httpd = self.get_readonly_server()._get_httpd()
1791
httpd = self.get_readonly_server().server
1813
1793
socket = SampleSocket(
1814
1794
'POST /.bzr/smart %s \r\n' % self._protocol_version