~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

MergeĀ lp:bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
204
204
    It records the bytes sent to it, and replies with a 200.
205
205
    """
206
206
 
207
 
    def __init__(self, expect_body_tail=None):
 
207
    def __init__(self, expect_body_tail=None, scheme=''):
208
208
        """Constructor.
209
209
 
210
210
        :type expect_body_tail: str
215
215
        self.host = None
216
216
        self.port = None
217
217
        self.received_bytes = ''
 
218
        self.scheme = scheme
 
219
 
 
220
    def get_url(self):
 
221
        return '%s://%s:%s/' % (self.scheme, self.host, self.port)
218
222
 
219
223
    def setUp(self):
220
224
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
304
308
 
305
309
        server = http_server.HttpServer(BogusRequestHandler)
306
310
        try:
307
 
            self.assertRaises(httplib.UnknownProtocol,server.setUp)
 
311
            self.assertRaises(httplib.UnknownProtocol, server.setUp)
308
312
        except:
309
313
            server.tearDown()
310
314
            self.fail('HTTP Server creation did not raise UnknownProtocol')
312
316
    def test_force_invalid_protocol(self):
313
317
        server = http_server.HttpServer(protocol_version='HTTP/0.1')
314
318
        try:
315
 
            self.assertRaises(httplib.UnknownProtocol,server.setUp)
 
319
            self.assertRaises(httplib.UnknownProtocol, server.setUp)
316
320
        except:
317
321
            server.tearDown()
318
322
            self.fail('HTTP Server creation did not raise UnknownProtocol')
320
324
    def test_server_start_and_stop(self):
321
325
        server = http_server.HttpServer()
322
326
        server.setUp()
323
 
        self.assertTrue(server._http_running)
324
 
        server.tearDown()
 
327
        try:
 
328
            self.assertTrue(server._http_running)
 
329
        finally:
 
330
            server.tearDown()
325
331
        self.assertFalse(server._http_running)
326
332
 
327
333
    def test_create_http_server_one_zero(self):
330
336
            protocol_version = 'HTTP/1.0'
331
337
 
332
338
        server = http_server.HttpServer(RequestHandlerOneZero)
333
 
        server.setUp()
334
 
        self.addCleanup(server.tearDown)
 
339
        self.start_server(server)
335
340
        self.assertIsInstance(server._httpd, http_server.TestingHTTPServer)
336
341
 
337
342
    def test_create_http_server_one_one(self):
340
345
            protocol_version = 'HTTP/1.1'
341
346
 
342
347
        server = http_server.HttpServer(RequestHandlerOneOne)
343
 
        server.setUp()
344
 
        self.addCleanup(server.tearDown)
 
348
        self.start_server(server)
345
349
        self.assertIsInstance(server._httpd,
346
350
                              http_server.TestingThreadingHTTPServer)
347
351
 
352
356
 
353
357
        server = http_server.HttpServer(RequestHandlerOneZero,
354
358
                                        protocol_version='HTTP/1.1')
355
 
        server.setUp()
356
 
        self.addCleanup(server.tearDown)
 
359
        self.start_server(server)
357
360
        self.assertIsInstance(server._httpd,
358
361
                              http_server.TestingThreadingHTTPServer)
359
362
 
364
367
 
365
368
        server = http_server.HttpServer(RequestHandlerOneOne,
366
369
                                        protocol_version='HTTP/1.0')
367
 
        server.setUp()
368
 
        self.addCleanup(server.tearDown)
 
370
        self.start_server(server)
369
371
        self.assertIsInstance(server._httpd,
370
372
                              http_server.TestingHTTPServer)
371
373
 
431
433
    def test_http_impl_urls(self):
432
434
        """There are servers which ask for particular clients to connect"""
433
435
        server = self._server()
 
436
        server.setUp()
434
437
        try:
435
 
            server.setUp()
436
438
            url = server.get_url()
437
439
            self.assertTrue(url.startswith('%s://' % self._qualified_prefix))
438
440
        finally:
543
545
class TestPost(tests.TestCase):
544
546
 
545
547
    def test_post_body_is_received(self):
546
 
        server = RecordingServer(expect_body_tail='end-of-body')
547
 
        server.setUp()
548
 
        self.addCleanup(server.tearDown)
549
 
        scheme = self._qualified_prefix
550
 
        url = '%s://%s:%s/' % (scheme, server.host, server.port)
 
548
        server = RecordingServer(expect_body_tail='end-of-body',
 
549
            scheme=self._qualified_prefix)
 
550
        self.start_server(server)
 
551
        url = server.get_url()
551
552
        http_transport = self._transport(url)
552
553
        code, response = http_transport._post('abc def end-of-body')
553
554
        self.assertTrue(
779
780
        self.assertEqual(None, server.port)
780
781
 
781
782
    def test_send_receive_bytes(self):
782
 
        server = RecordingServer(expect_body_tail='c')
783
 
        server.setUp()
784
 
        self.addCleanup(server.tearDown)
 
783
        server = RecordingServer(expect_body_tail='c', scheme='http')
 
784
        self.start_server(server)
785
785
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
786
786
        sock.connect((server.host, server.port))
787
787
        sock.sendall('abc')