/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Jelmer Vernooij
  • Date: 2009-02-25 15:36:48 UTC
  • mfrom: (4048 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4050.
  • Revision ID: jelmer@samba.org-20090225153648-7r5mk20nr9dttqbf
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 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
44
44
    ui,
45
45
    urlutils,
46
46
    )
 
47
from bzrlib.symbol_versioning import (
 
48
    deprecated_in,
 
49
    )
47
50
from bzrlib.tests import (
48
51
    http_server,
49
52
    http_utils,
65
68
    pycurl_present = False
66
69
 
67
70
 
68
 
class TransportAdapter(tests.TestScenarioApplier):
69
 
    """Generate the same test for each transport implementation."""
70
 
 
71
 
    def __init__(self):
72
 
        transport_scenarios = [
73
 
            ('urllib', dict(_transport=_urllib.HttpTransport_urllib,
74
 
                            _server=http_server.HttpServer_urllib,
75
 
                            _qualified_prefix='http+urllib',)),
76
 
            ]
77
 
        if pycurl_present:
78
 
            transport_scenarios.append(
79
 
                ('pycurl', dict(_transport=PyCurlTransport,
80
 
                                _server=http_server.HttpServer_PyCurl,
81
 
                                _qualified_prefix='http+pycurl',)))
82
 
        self.scenarios = transport_scenarios
83
 
 
84
 
 
85
 
class TransportProtocolAdapter(TransportAdapter):
86
 
    """Generate the same test for each protocol implementation.
87
 
 
88
 
    In addition to the transport adaptatation that we inherit from.
89
 
    """
90
 
 
91
 
    def __init__(self):
92
 
        super(TransportProtocolAdapter, self).__init__()
93
 
        protocol_scenarios = [
94
 
            ('HTTP/1.0',  dict(_protocol_version='HTTP/1.0')),
95
 
            ('HTTP/1.1',  dict(_protocol_version='HTTP/1.1')),
96
 
            ]
97
 
        self.scenarios = tests.multiply_scenarios(self.scenarios,
98
 
                                                  protocol_scenarios)
99
 
 
100
 
 
101
 
class TransportProtocolAuthenticationAdapter(TransportProtocolAdapter):
102
 
    """Generate the same test for each authentication scheme implementation.
103
 
 
104
 
    In addition to the protocol adaptatation that we inherit from.
105
 
    """
106
 
 
107
 
    def __init__(self):
108
 
        super(TransportProtocolAuthenticationAdapter, self).__init__()
109
 
        auth_scheme_scenarios = [
110
 
            ('basic', dict(_auth_scheme='basic')),
111
 
            ('digest', dict(_auth_scheme='digest')),
112
 
            ]
113
 
 
114
 
        self.scenarios = tests.multiply_scenarios(self.scenarios,
115
 
                                                  auth_scheme_scenarios)
116
 
 
117
71
def load_tests(standard_tests, module, loader):
118
72
    """Multiply tests for http clients and protocol versions."""
 
73
    result = loader.suiteClass()
 
74
    adapter = tests.TestScenarioApplier()
 
75
    remaining_tests = standard_tests
 
76
 
119
77
    # one for each transport
120
 
    t_adapter = TransportAdapter()
121
 
    t_classes= (TestHttpTransportRegistration,
 
78
    t_tests, remaining_tests = tests.split_suite_by_condition(
 
79
        remaining_tests, tests.condition_isinstance((
 
80
                TestHttpTransportRegistration,
122
81
                TestHttpTransportUrls,
123
82
                Test_redirected_to,
124
 
                )
125
 
    is_testing_for_transports = tests.condition_isinstance(t_classes)
 
83
                )))
 
84
    transport_scenarios = [
 
85
        ('urllib', dict(_transport=_urllib.HttpTransport_urllib,
 
86
                        _server=http_server.HttpServer_urllib,
 
87
                        _qualified_prefix='http+urllib',)),
 
88
        ]
 
89
    if pycurl_present:
 
90
        transport_scenarios.append(
 
91
            ('pycurl', dict(_transport=PyCurlTransport,
 
92
                            _server=http_server.HttpServer_PyCurl,
 
93
                            _qualified_prefix='http+pycurl',)))
 
94
    adapter.scenarios = transport_scenarios
 
95
    tests.adapt_tests(t_tests, adapter, result)
126
96
 
127
97
    # multiplied by one for each protocol version
128
 
    tp_adapter = TransportProtocolAdapter()
129
 
    tp_classes= (SmartHTTPTunnellingTest,
130
 
                 TestDoCatchRedirections,
131
 
                 TestHTTPConnections,
132
 
                 TestHTTPRedirections,
133
 
                 TestHTTPSilentRedirections,
134
 
                 TestLimitedRangeRequestServer,
135
 
                 TestPost,
136
 
                 TestProxyHttpServer,
137
 
                 TestRanges,
138
 
                 TestSpecificRequestHandler,
139
 
                 )
140
 
    is_also_testing_for_protocols = tests.condition_isinstance(tp_classes)
 
98
    tp_tests, remaining_tests = tests.split_suite_by_condition(
 
99
        remaining_tests, tests.condition_isinstance((
 
100
                SmartHTTPTunnellingTest,
 
101
                TestDoCatchRedirections,
 
102
                TestHTTPConnections,
 
103
                TestHTTPRedirections,
 
104
                TestHTTPSilentRedirections,
 
105
                TestLimitedRangeRequestServer,
 
106
                TestPost,
 
107
                TestProxyHttpServer,
 
108
                TestRanges,
 
109
                TestSpecificRequestHandler,
 
110
                )))
 
111
    protocol_scenarios = [
 
112
            ('HTTP/1.0',  dict(_protocol_version='HTTP/1.0')),
 
113
            ('HTTP/1.1',  dict(_protocol_version='HTTP/1.1')),
 
114
            ]
 
115
    tp_scenarios = tests.multiply_scenarios(adapter.scenarios,
 
116
                                            protocol_scenarios)
 
117
    adapter.scenarios = tp_scenarios
 
118
    tests.adapt_tests(tp_tests, adapter, result)
141
119
 
142
120
    # multiplied by one for each authentication scheme
143
 
    tpa_adapter = TransportProtocolAuthenticationAdapter()
144
 
    tpa_classes = (TestAuth,
145
 
                   )
146
 
    is_also_testing_for_authentication = tests.condition_isinstance(
147
 
        tpa_classes)
148
 
 
149
 
    result = loader.suiteClass()
150
 
    for test_class in tests.iter_suite_tests(standard_tests):
151
 
        # Each test class is either standalone or testing for some combination
152
 
        # of transport, protocol version, authentication scheme. Use the right
153
 
        # adpater (or none) depending on the class.
154
 
        if is_testing_for_transports(test_class):
155
 
            result.addTests(t_adapter.adapt(test_class))
156
 
        elif is_also_testing_for_protocols(test_class):
157
 
            result.addTests(tp_adapter.adapt(test_class))
158
 
        elif is_also_testing_for_authentication(test_class):
159
 
            result.addTests(tpa_adapter.adapt(test_class))
160
 
        else:
161
 
            result.addTest(test_class)
 
121
    tpa_tests, remaining_tests = tests.split_suite_by_condition(
 
122
        remaining_tests, tests.condition_isinstance((
 
123
                TestAuth,
 
124
                )))
 
125
    auth_scheme_scenarios = [
 
126
        ('basic', dict(_auth_scheme='basic')),
 
127
        ('digest', dict(_auth_scheme='digest')),
 
128
        ]
 
129
    adapter.scenarios = tests.multiply_scenarios(adapter.scenarios,
 
130
                                                 auth_scheme_scenarios)
 
131
    tests.adapt_tests(tpa_tests, adapter, result)
 
132
 
 
133
    tpact_tests, remaining_tests = tests.split_suite_by_condition(
 
134
        remaining_tests, tests.condition_isinstance((
 
135
                TestActivity,
 
136
                )))
 
137
    activity_scenarios = [
 
138
        ('http', dict(_activity_server=ActivityHTTPServer)),
 
139
        ]
 
140
    if tests.HTTPSServerFeature.available():
 
141
        activity_scenarios.append(
 
142
            ('https', dict(_activity_server=ActivityHTTPSServer,)))
 
143
    adapter.scenarios = tests.multiply_scenarios(tp_scenarios,
 
144
                                                 activity_scenarios)
 
145
    tests.adapt_tests(tpact_tests, adapter, result)
 
146
 
 
147
    # No parametrization for the remaining tests
 
148
    result.addTests(remaining_tests)
 
149
 
162
150
    return result
163
151
 
164
152
 
173
161
 
174
162
class RecordingServer(object):
175
163
    """A fake HTTP server.
176
 
    
 
164
 
177
165
    It records the bytes sent to it, and replies with a 200.
178
166
    """
179
167
 
812
800
        # bytes on the socket
813
801
        ireadv = iter(t.readv('a', ((0, 1), (1, 1), (2, 4), (6, 4))))
814
802
        self.assertEqual((0, '0'), ireadv.next())
815
 
        # The server should have issued one request so far 
 
803
        # The server should have issued one request so far
816
804
        self.assertEqual(1, server.GET_request_nb)
817
805
        self.assertEqual('0123456789', t.get_bytes('a'))
818
806
        # get_bytes issued an additional request, the readv pending ones are
1275
1263
    def test_read_redirected_bundle_from_url(self):
1276
1264
        from bzrlib.bundle import read_bundle_from_url
1277
1265
        url = self.old_transport.abspath('bundle')
1278
 
        bundle = read_bundle_from_url(url)
 
1266
        bundle = self.applyDeprecated(deprecated_in((1, 12, 0)),
 
1267
                read_bundle_from_url, url)
1279
1268
        # If read_bundle_from_url was successful we get an empty bundle
1280
1269
        self.assertEqual([], bundle.revisions)
1281
1270
 
1807
1796
                             'https://foo.example.com/foo')
1808
1797
        self.assertIsInstance(r, type(t))
1809
1798
        self.assertEquals(t._user, r._user)
 
1799
 
 
1800
 
 
1801
class PredefinedRequestHandler(http_server.TestingHTTPRequestHandler):
 
1802
    """Request handler for a unique and pre-defined request.
 
1803
 
 
1804
    The only thing we care about here is how many bytes travel on the wire. But
 
1805
    since we want to measure it for a real http client, we have to send it
 
1806
    correct responses.
 
1807
 
 
1808
    We expect to receive a *single* request nothing more (and we won't even
 
1809
    check what request it is, we just measure the bytes read until an empty
 
1810
    line.
 
1811
    """
 
1812
 
 
1813
    def handle_one_request(self):
 
1814
        tcs = self.server.test_case_server
 
1815
        requestline = self.rfile.readline()
 
1816
        headers = self.MessageClass(self.rfile, 0)
 
1817
        # We just read: the request, the headers, an empty line indicating the
 
1818
        # end of the headers.
 
1819
        bytes_read = len(requestline)
 
1820
        for line in headers.headers:
 
1821
            bytes_read += len(line)
 
1822
        bytes_read += len('\r\n')
 
1823
        if requestline.startswith('POST'):
 
1824
            # The body should be a single line (or we don't know where it ends
 
1825
            # and we don't want to issue a blocking read)
 
1826
            body = self.rfile.readline()
 
1827
            bytes_read += len(body)
 
1828
        tcs.bytes_read = bytes_read
 
1829
 
 
1830
        # We set the bytes written *before* issuing the write, the client is
 
1831
        # supposed to consume every produced byte *before* checking that value.
 
1832
 
 
1833
        # Doing the oppposite may lead to test failure: we may be interrupted
 
1834
        # after the write but before updating the value. The client can then
 
1835
        # continue and read the value *before* we can update it. And yes,
 
1836
        # this has been observed -- vila 20090129
 
1837
        tcs.bytes_written = len(tcs.canned_response)
 
1838
        self.wfile.write(tcs.canned_response)
 
1839
 
 
1840
 
 
1841
class ActivityServerMixin(object):
 
1842
 
 
1843
    def __init__(self, protocol_version):
 
1844
        super(ActivityServerMixin, self).__init__(
 
1845
            request_handler=PredefinedRequestHandler,
 
1846
            protocol_version=protocol_version)
 
1847
        # Bytes read and written by the server
 
1848
        self.bytes_read = 0
 
1849
        self.bytes_written = 0
 
1850
        self.canned_response = None
 
1851
 
 
1852
 
 
1853
class ActivityHTTPServer(ActivityServerMixin, http_server.HttpServer):
 
1854
    pass
 
1855
 
 
1856
 
 
1857
if tests.HTTPSServerFeature.available():
 
1858
    from bzrlib.tests import https_server
 
1859
    class ActivityHTTPSServer(ActivityServerMixin, https_server.HTTPSServer):
 
1860
        pass
 
1861
 
 
1862
 
 
1863
class TestActivity(tests.TestCase):
 
1864
    """Test socket activity reporting.
 
1865
 
 
1866
    We use a special purpose server to control the bytes sent and received and
 
1867
    be able to predict the activity on the client socket.
 
1868
    """
 
1869
 
 
1870
    def setUp(self):
 
1871
        tests.TestCase.setUp(self)
 
1872
        self.server = self._activity_server(self._protocol_version)
 
1873
        self.server.setUp()
 
1874
        self.activities = {}
 
1875
        def report_activity(t, bytes, direction):
 
1876
            count = self.activities.get(direction, 0)
 
1877
            count += bytes
 
1878
            self.activities[direction] = count
 
1879
 
 
1880
        # We override at class level because constructors may propagate the
 
1881
        # bound method and render instance overriding ineffective (an
 
1882
        # alternative would be be to define a specific ui factory instead...)
 
1883
        self.orig_report_activity = self._transport._report_activity
 
1884
        self._transport._report_activity = report_activity
 
1885
 
 
1886
    def tearDown(self):
 
1887
        self._transport._report_activity = self.orig_report_activity
 
1888
        self.server.tearDown()
 
1889
        tests.TestCase.tearDown(self)
 
1890
 
 
1891
    def get_transport(self):
 
1892
        return self._transport(self.server.get_url())
 
1893
 
 
1894
    def assertActivitiesMatch(self):
 
1895
        self.assertEqual(self.server.bytes_read,
 
1896
                         self.activities.get('write', 0), 'written bytes')
 
1897
        self.assertEqual(self.server.bytes_written,
 
1898
                         self.activities.get('read', 0), 'read bytes')
 
1899
 
 
1900
    def test_get(self):
 
1901
        self.server.canned_response = '''HTTP/1.1 200 OK\r
 
1902
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
 
1903
Server: Apache/2.0.54 (Fedora)\r
 
1904
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
 
1905
ETag: "56691-23-38e9ae00"\r
 
1906
Accept-Ranges: bytes\r
 
1907
Content-Length: 35\r
 
1908
Connection: close\r
 
1909
Content-Type: text/plain; charset=UTF-8\r
 
1910
\r
 
1911
Bazaar-NG meta directory, format 1
 
1912
'''
 
1913
        t = self.get_transport()
 
1914
        self.assertEqual('Bazaar-NG meta directory, format 1\n',
 
1915
                         t.get('foo/bar').read())
 
1916
        self.assertActivitiesMatch()
 
1917
 
 
1918
    def test_has(self):
 
1919
        self.server.canned_response = '''HTTP/1.1 200 OK\r
 
1920
Server: SimpleHTTP/0.6 Python/2.5.2\r
 
1921
Date: Thu, 29 Jan 2009 20:21:47 GMT\r
 
1922
Content-type: application/octet-stream\r
 
1923
Content-Length: 20\r
 
1924
Last-Modified: Thu, 29 Jan 2009 20:21:47 GMT\r
 
1925
\r
 
1926
'''
 
1927
        t = self.get_transport()
 
1928
        self.assertTrue(t.has('foo/bar'))
 
1929
        self.assertActivitiesMatch()
 
1930
 
 
1931
    def test_readv(self):
 
1932
        self.server.canned_response = '''HTTP/1.1 206 Partial Content\r
 
1933
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
 
1934
Server: Apache/2.0.54 (Fedora)\r
 
1935
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
 
1936
ETag: "238a3c-16ec2-805c5540"\r
 
1937
Accept-Ranges: bytes\r
 
1938
Content-Length: 1534\r
 
1939
Connection: close\r
 
1940
Content-Type: multipart/byteranges; boundary=418470f848b63279b\r
 
1941
\r
 
1942
\r
 
1943
--418470f848b63279b\r
 
1944
Content-type: text/plain; charset=UTF-8\r
 
1945
Content-range: bytes 0-254/93890\r
 
1946
\r
 
1947
mbp@sourcefrog.net-20050309040815-13242001617e4a06
 
1948
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e7627
 
1949
mbp@sourcefrog.net-20050309040957-6cad07f466bb0bb8
 
1950
mbp@sourcefrog.net-20050309041501-c840e09071de3b67
 
1951
mbp@sourcefrog.net-20050309044615-c24a3250be83220a
 
1952
\r
 
1953
--418470f848b63279b\r
 
1954
Content-type: text/plain; charset=UTF-8\r
 
1955
Content-range: bytes 1000-2049/93890\r
 
1956
\r
 
1957
40-fd4ec249b6b139ab
 
1958
mbp@sourcefrog.net-20050311063625-07858525021f270b
 
1959
mbp@sourcefrog.net-20050311231934-aa3776aff5200bb9
 
1960
mbp@sourcefrog.net-20050311231953-73aeb3a131c3699a
 
1961
mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
 
1962
mbp@sourcefrog.net-20050312071639-0a8f59a34a024ff0
 
1963
mbp@sourcefrog.net-20050312073432-b2c16a55e0d6e9fb
 
1964
mbp@sourcefrog.net-20050312073831-a47c3335ece1920f
 
1965
mbp@sourcefrog.net-20050312085412-13373aa129ccbad3
 
1966
mbp@sourcefrog.net-20050313052251-2bf004cb96b39933
 
1967
mbp@sourcefrog.net-20050313052856-3edd84094687cb11
 
1968
mbp@sourcefrog.net-20050313053233-e30a4f28aef48f9d
 
1969
mbp@sourcefrog.net-20050313053853-7c64085594ff3072
 
1970
mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
 
1971
mbp@sourcefrog.net-20050313061422-418f1f73b94879b9
 
1972
mbp@sourcefrog.net-20050313120651-497bd231b19df600
 
1973
mbp@sourcefrog.net-20050314024931-eae0170ef25a5d1a
 
1974
mbp@sourcefrog.net-20050314025438-d52099f915fe65fc
 
1975
mbp@sourcefrog.net-20050314025539-637a636692c055cf
 
1976
mbp@sourcefrog.net-20050314025737-55eb441f430ab4ba
 
1977
mbp@sourcefrog.net-20050314025901-d74aa93bb7ee8f62
 
1978
mbp@source\r
 
1979
--418470f848b63279b--\r
 
1980
'''
 
1981
        t = self.get_transport()
 
1982
        # Remember that the request is ignored and that the ranges below
 
1983
        # doesn't have to match the canned response.
 
1984
        l = list(t.readv('/foo/bar', ((0, 255), (1000, 1050))))
 
1985
        self.assertEqual(2, len(l))
 
1986
        self.assertActivitiesMatch()
 
1987
 
 
1988
    def test_post(self):
 
1989
        self.server.canned_response = '''HTTP/1.1 200 OK\r
 
1990
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
 
1991
Server: Apache/2.0.54 (Fedora)\r
 
1992
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
 
1993
ETag: "56691-23-38e9ae00"\r
 
1994
Accept-Ranges: bytes\r
 
1995
Content-Length: 35\r
 
1996
Connection: close\r
 
1997
Content-Type: text/plain; charset=UTF-8\r
 
1998
\r
 
1999
lalala whatever as long as itsssss
 
2000
'''
 
2001
        t = self.get_transport()
 
2002
        # We must send a single line of body bytes, see
 
2003
        # PredefinedRequestHandler.handle_one_request
 
2004
        code, f = t._post('abc def end-of-body\n')
 
2005
        self.assertEqual('lalala whatever as long as itsssss\n', f.read())
 
2006
        self.assertActivitiesMatch()