~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Gordon Tyler
  • Date: 2010-01-14 15:24:04 UTC
  • mto: (5037.3.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5046.
  • Revision ID: gordon@doxxx.net-20100114152404-d64ik2afl96lcml0
Reverted changes to test_rules since the original should work now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
218
218
    def get_url(self):
219
219
        return '%s://%s:%s/' % (self.scheme, self.host, self.port)
220
220
 
221
 
    def start_server(self):
 
221
    def setUp(self):
222
222
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
223
223
        self._sock.bind(('127.0.0.1', 0))
224
224
        self.host, self.port = self._sock.getsockname()
247
247
            # The client may have already closed the socket.
248
248
            pass
249
249
 
250
 
    def stop_server(self):
 
250
    def tearDown(self):
251
251
        try:
252
252
            self._sock.close()
253
253
        except socket.error:
306
306
 
307
307
        server = http_server.HttpServer(BogusRequestHandler)
308
308
        try:
309
 
            self.assertRaises(httplib.UnknownProtocol, server.start_server)
 
309
            self.assertRaises(httplib.UnknownProtocol, server.setUp)
310
310
        except:
311
 
            server.stop_server()
 
311
            server.tearDown()
312
312
            self.fail('HTTP Server creation did not raise UnknownProtocol')
313
313
 
314
314
    def test_force_invalid_protocol(self):
315
315
        server = http_server.HttpServer(protocol_version='HTTP/0.1')
316
316
        try:
317
 
            self.assertRaises(httplib.UnknownProtocol, server.start_server)
 
317
            self.assertRaises(httplib.UnknownProtocol, server.setUp)
318
318
        except:
319
 
            server.stop_server()
 
319
            server.tearDown()
320
320
            self.fail('HTTP Server creation did not raise UnknownProtocol')
321
321
 
322
322
    def test_server_start_and_stop(self):
323
323
        server = http_server.HttpServer()
324
 
        server.start_server()
 
324
        server.setUp()
325
325
        try:
326
326
            self.assertTrue(server._http_running)
327
327
        finally:
328
 
            server.stop_server()
 
328
            server.tearDown()
329
329
        self.assertFalse(server._http_running)
330
330
 
331
331
    def test_create_http_server_one_zero(self):
391
391
        self.assertEqual('http://example.com', url)
392
392
        self.assertEqual(0, len(f.credentials))
393
393
        url = http.extract_auth(
394
 
            'http://user:pass@example.com/bzr/bzr.dev', f)
395
 
        self.assertEqual('http://example.com/bzr/bzr.dev', url)
 
394
            'http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
 
395
        self.assertEqual('http://www.bazaar-vcs.org/bzr/bzr.dev', url)
396
396
        self.assertEqual(1, len(f.credentials))
397
 
        self.assertEqual([None, 'example.com', 'user', 'pass'],
 
397
        self.assertEqual([None, 'www.bazaar-vcs.org', 'user', 'pass'],
398
398
                         f.credentials[0])
399
399
 
400
400
 
428
428
    def test_http_impl_urls(self):
429
429
        """There are servers which ask for particular clients to connect"""
430
430
        server = self._server()
431
 
        server.start_server()
 
431
        server.setUp()
432
432
        try:
433
433
            url = server.get_url()
434
434
            self.assertTrue(url.startswith('%s://' % self._qualified_prefix))
435
435
        finally:
436
 
            server.stop_server()
 
436
            server.tearDown()
437
437
 
438
438
 
439
439
class TestHttps_pycurl(TestWithTransport_pycurl, tests.TestCase):
449
449
        support it.
450
450
        """
451
451
        self.requireFeature(features.pycurl)
452
 
        # Import the module locally now that we now it's available.
453
 
        pycurl = features.pycurl.module
454
452
 
455
 
        self.overrideAttr(pycurl, 'version_info',
456
 
                          # Fake the pycurl version_info This was taken from
457
 
                          # a windows pycurl without SSL (thanks to bialix)
458
 
                          lambda : (2,
459
 
                                    '7.13.2',
460
 
                                    462082,
461
 
                                    'i386-pc-win32',
462
 
                                    2576,
463
 
                                    None,
464
 
                                    0,
465
 
                                    None,
466
 
                                    ('ftp', 'gopher', 'telnet',
467
 
                                     'dict', 'ldap', 'http', 'file'),
468
 
                                    None,
469
 
                                    0,
470
 
                                    None))
471
 
        self.assertRaises(errors.DependencyNotPresent, self._transport,
472
 
                          'https://launchpad.net')
 
453
        version_info_orig = pycurl.version_info
 
454
        try:
 
455
            # Now that we have pycurl imported, we can fake its version_info
 
456
            # This was taken from a windows pycurl without SSL
 
457
            # (thanks to bialix)
 
458
            pycurl.version_info = lambda : (2,
 
459
                                            '7.13.2',
 
460
                                            462082,
 
461
                                            'i386-pc-win32',
 
462
                                            2576,
 
463
                                            None,
 
464
                                            0,
 
465
                                            None,
 
466
                                            ('ftp', 'gopher', 'telnet',
 
467
                                             'dict', 'ldap', 'http', 'file'),
 
468
                                            None,
 
469
                                            0,
 
470
                                            None)
 
471
            self.assertRaises(errors.DependencyNotPresent, self._transport,
 
472
                              'https://launchpad.net')
 
473
        finally:
 
474
            # Restore the right function
 
475
            pycurl.version_info = version_info_orig
473
476
 
474
477
 
475
478
class TestHTTPConnections(http_utils.TestCaseWithWebserver):
759
762
        self.assertEqual(None, server.host)
760
763
        self.assertEqual(None, server.port)
761
764
 
762
 
    def test_setUp_and_stop(self):
 
765
    def test_setUp_and_tearDown(self):
763
766
        server = RecordingServer(expect_body_tail=None)
764
 
        server.start_server()
 
767
        server.setUp()
765
768
        try:
766
769
            self.assertNotEqual(None, server.host)
767
770
            self.assertNotEqual(None, server.port)
768
771
        finally:
769
 
            server.stop_server()
 
772
            server.tearDown()
770
773
        self.assertEqual(None, server.host)
771
774
        self.assertEqual(None, server.port)
772
775
 
1360
1363
 
1361
1364
 
1362
1365
def install_redirected_request(test):
1363
 
    test.overrideAttr(_urllib2_wrappers, 'Request', RedirectedRequest)
 
1366
    test.original_class = _urllib2_wrappers.Request
 
1367
    def restore():
 
1368
        _urllib2_wrappers.Request = test.original_class
 
1369
    _urllib2_wrappers.Request = RedirectedRequest
 
1370
    test.addCleanup(restore)
1364
1371
 
1365
1372
 
1366
1373
class TestHTTPSilentRedirections(http_utils.TestCaseWithRedirectedWebserver):
1956
1963
    def setUp(self):
1957
1964
        tests.TestCase.setUp(self)
1958
1965
        self.server = self._activity_server(self._protocol_version)
1959
 
        self.server.start_server()
 
1966
        self.server.setUp()
1960
1967
        self.activities = {}
1961
1968
        def report_activity(t, bytes, direction):
1962
1969
            count = self.activities.get(direction, 0)
1971
1978
 
1972
1979
    def tearDown(self):
1973
1980
        self._transport._report_activity = self.orig_report_activity
1974
 
        self.server.stop_server()
 
1981
        self.server.tearDown()
1975
1982
        tests.TestCase.tearDown(self)
1976
1983
 
1977
1984
    def get_transport(self):
2097
2104
    def setUp(self):
2098
2105
        tests.TestCase.setUp(self)
2099
2106
        self.server = self._activity_server(self._protocol_version)
2100
 
        self.server.start_server()
 
2107
        self.server.setUp()
2101
2108
        self.activities = {}
2102
2109
        def report_activity(t, bytes, direction):
2103
2110
            count = self.activities.get(direction, 0)
2112
2119
 
2113
2120
    def tearDown(self):
2114
2121
        self._transport._report_activity = self.orig_report_activity
2115
 
        self.server.stop_server()
 
2122
        self.server.tearDown()
2116
2123
        tests.TestCase.tearDown(self)
2117
2124
 
2118
2125
 
2128
2135
        self.server = ActivityHTTPServer('HTTP/1.1')
2129
2136
        self._transport=_urllib.HttpTransport_urllib
2130
2137
 
2131
 
        self.server.start_server()
 
2138
        self.server.setUp()
2132
2139
 
2133
2140
        # We override at class level because constructors may propagate the
2134
2141
        # bound method and render instance overriding ineffective (an
2138
2145
 
2139
2146
    def tearDown(self):
2140
2147
        self._transport._report_activity = self.orig_report_activity
2141
 
        self.server.stop_server()
 
2148
        self.server.tearDown()
2142
2149
        tests.TestCase.tearDown(self)
2143
2150
 
2144
2151
    def assertActivitiesMatch(self):