~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

Merge http-leaks into sftp-leaks

Show diffs side-by-side

added added

removed removed

Lines of Context:
245
245
            # The client may have already closed the socket.
246
246
            pass
247
247
 
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
252
 
            sock = None
253
 
            try:
254
 
                sock = socket.socket(af, socktype, proto)
255
 
                sock.connect(sa)
256
 
                return sock
257
 
 
258
 
            except socket.error, err:
259
 
                # err is now the most recent error
260
 
                if sock is not None:
261
 
                    sock.close()
262
 
        raise err
263
 
 
264
248
    def stop_server(self):
265
249
        try:
266
250
            # Issue a fake connection to wake up the server and allow it to
267
251
            # finish quickly
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.
324
308
 
325
309
            protocol_version = 'HTTP/0.1'
326
310
 
327
 
        server = http_server.HttpServer(BogusRequestHandler)
328
 
        try:
329
 
            self.assertRaises(httplib.UnknownProtocol, server.start_server)
330
 
        except:
331
 
            server.stop_server()
332
 
            self.fail('HTTP Server creation did not raise UnknownProtocol')
 
311
        self.assertRaises(httplib.UnknownProtocol,
 
312
                          http_server.HttpServer, BogusRequestHandler)
333
313
 
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')
338
317
 
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())
346
325
 
347
326
    def test_create_http_server_one_zero(self):
348
327
        class RequestHandlerOneZero(http_server.TestingHTTPRequestHandler):
351
330
 
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)
355
334
 
356
335
    def test_create_http_server_one_one(self):
357
336
        class RequestHandlerOneOne(http_server.TestingHTTPRequestHandler):
360
339
 
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)
365
344
 
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)
376
355
 
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)
387
366
 
388
367
 
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
1756
1736
 
1757
1737
    def makefile(self, mode='r', bufsize=None):
1758
1738
        if 'r' in mode:
1808
1788
        self.assertEqual(expected_reply_body, reply_body)
1809
1789
 
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
1812
1792
 
1813
1793
        socket = SampleSocket(
1814
1794
            'POST /.bzr/smart %s \r\n' % self._protocol_version
1854
1834
                          t.get_smart_medium().send_http_smart_request,
1855
1835
                          'whatever')
1856
1836
 
 
1837
 
1857
1838
class Test_redirected_to(tests.TestCase):
1858
1839
 
1859
1840
    def test_redirected_to_subdir(self):