~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Vincent Ladeuil
  • Date: 2009-02-05 10:20:51 UTC
  • mfrom: (3945.1.8 pb-http)
  • mto: This revision was merged to the branch mainline in revision 3983.
  • Revision ID: v.ladeuil+lp@free.fr-20090205102051-gyivrlud1hk1c12g
Progress bar at socket level for http

Show diffs side-by-side

added added

removed removed

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