/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
1
# Copyright (C) 2005, 2006 Canonical
1540.3.24 by Martin Pool
Add new protocol 'http+pycurl' that always uses PyCurl.
2
#
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1540.3.24 by Martin Pool
Add new protocol 'http+pycurl' that always uses PyCurl.
7
#
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1540.3.24 by Martin Pool
Add new protocol 'http+pycurl' that always uses PyCurl.
12
#
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
16
1540.3.3 by Martin Pool
Review updates of pycurl transport
17
# FIXME: This test should be repeated for each available http client
18
# implementation; at the moment we have urllib and pycurl.
19
1540.3.22 by Martin Pool
[patch] Add TestCase.assertIsInstance
20
# TODO: Should be renamed to bzrlib.transport.http.tests?
21
2000.2.2 by John Arbash Meinel
Update the urllib.has test.
22
import socket
23
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
24
import bzrlib
2004.1.1 by vila
Connection sharing, with redirection. without authentification.
25
from bzrlib.errors import (DependencyNotPresent,
26
                           ConnectionError,
27
                           )
1540.3.30 by Martin Pool
Fix up bogus-url tests for broken dns servers, and error imports
28
from bzrlib.tests import TestCase, TestSkipped
1540.3.23 by Martin Pool
Allow urls like http+pycurl://host/ to use a particular impl
29
from bzrlib.transport import Transport
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
30
from bzrlib.transport.http import extract_auth, HttpTransportBase
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
31
from bzrlib.transport.http._urllib import HttpTransport_urllib
2004.3.1 by vila
Test ConnectionError exceptions.
32
from bzrlib.tests.HTTPTestUtil import (
33
    TestCaseWithWebserver,
34
    TestCaseWithWallserver,
35
    )
1185.40.20 by Robey Pointer
allow user:pass@ info in http urls to be used for auth; this should be easily expandable later to use auth config files
36
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
37
1185.40.20 by Robey Pointer
allow user:pass@ info in http urls to be used for auth; this should be easily expandable later to use auth config files
38
class FakeManager (object):
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
39
1185.40.20 by Robey Pointer
allow user:pass@ info in http urls to be used for auth; this should be easily expandable later to use auth config files
40
    def __init__(self):
41
        self.credentials = []
2004.3.1 by vila
Test ConnectionError exceptions.
42
1185.40.20 by Robey Pointer
allow user:pass@ info in http urls to be used for auth; this should be easily expandable later to use auth config files
43
    def add_password(self, realm, host, username, password):
44
        self.credentials.append([realm, host, username, password])
45
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
46
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
47
class TestHttpUrls(TestCase):
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
48
1185.40.20 by Robey Pointer
allow user:pass@ info in http urls to be used for auth; this should be easily expandable later to use auth config files
49
    def test_url_parsing(self):
50
        f = FakeManager()
51
        url = extract_auth('http://example.com', f)
52
        self.assertEquals('http://example.com', url)
53
        self.assertEquals(0, len(f.credentials))
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
54
        url = extract_auth('http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
55
        self.assertEquals('http://www.bazaar-vcs.org/bzr/bzr.dev', url)
1185.40.20 by Robey Pointer
allow user:pass@ info in http urls to be used for auth; this should be easily expandable later to use auth config files
56
        self.assertEquals(1, len(f.credentials))
2004.3.1 by vila
Test ConnectionError exceptions.
57
        self.assertEquals([None, 'www.bazaar-vcs.org', 'user', 'pass'],
58
                          f.credentials[0])
59
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
60
    def test_abs_url(self):
61
        """Construction of absolute http URLs"""
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
62
        t = HttpTransport_urllib('http://bazaar-vcs.org/bzr/bzr.dev/')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
63
        eq = self.assertEqualDiff
64
        eq(t.abspath('.'),
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
65
           'http://bazaar-vcs.org/bzr/bzr.dev')
2004.3.1 by vila
Test ConnectionError exceptions.
66
        eq(t.abspath('foo/bar'),
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
67
           'http://bazaar-vcs.org/bzr/bzr.dev/foo/bar')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
68
        eq(t.abspath('.bzr'),
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
69
           'http://bazaar-vcs.org/bzr/bzr.dev/.bzr')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
70
        eq(t.abspath('.bzr/1//2/./3'),
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
71
           'http://bazaar-vcs.org/bzr/bzr.dev/.bzr/1/2/3')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
72
73
    def test_invalid_http_urls(self):
74
        """Trap invalid construction of urls"""
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
75
        t = HttpTransport_urllib('http://bazaar-vcs.org/bzr/bzr.dev/')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
76
        self.assertRaises(ValueError,
77
            t.abspath,
78
            '.bzr/')
79
80
    def test_http_root_urls(self):
81
        """Construction of URLs from server root"""
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
82
        t = HttpTransport_urllib('http://bzr.ozlabs.org/')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
83
        eq = self.assertEqualDiff
84
        eq(t.abspath('.bzr/tree-version'),
85
           'http://bzr.ozlabs.org/.bzr/tree-version')
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
86
1540.3.24 by Martin Pool
Add new protocol 'http+pycurl' that always uses PyCurl.
87
    def test_http_impl_urls(self):
88
        """There are servers which ask for particular clients to connect"""
89
        try:
90
            from bzrlib.transport.http._pycurl import HttpServer_PyCurl
91
            server = HttpServer_PyCurl()
92
            try:
93
                server.setUp()
94
                url = server.get_url()
95
                self.assertTrue(url.startswith('http+pycurl://'))
96
            finally:
97
                server.tearDown()
1540.3.30 by Martin Pool
Fix up bogus-url tests for broken dns servers, and error imports
98
        except DependencyNotPresent:
1540.3.24 by Martin Pool
Add new protocol 'http+pycurl' that always uses PyCurl.
99
            raise TestSkipped('pycurl not present')
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
100
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
101
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
102
class TestHttpMixins(object):
103
104
    def _prep_tree(self):
105
        self.build_tree(['xxx', 'foo/', 'foo/bar'], line_endings='binary',
106
                        transport=self.get_transport())
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
107
108
    def test_http_has(self):
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
109
        server = self.get_readonly_server()
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
110
        t = self._transport(server.get_url())
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
111
        self.assertEqual(t.has('foo/bar'), True)
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
112
        self.assertEqual(len(server.logs), 1)
2004.3.1 by vila
Test ConnectionError exceptions.
113
        self.assertContainsRe(server.logs[0],
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
114
            r'"HEAD /foo/bar HTTP/1.." (200|302) - "-" "bzr/')
1553.1.5 by James Henstridge
Make HTTP transport has() method do HEAD requests, and update test to
115
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
116
    def test_http_has_not_found(self):
117
        server = self.get_readonly_server()
118
        t = self._transport(server.get_url())
1553.1.5 by James Henstridge
Make HTTP transport has() method do HEAD requests, and update test to
119
        self.assertEqual(t.has('not-found'), False)
2004.3.1 by vila
Test ConnectionError exceptions.
120
        self.assertContainsRe(server.logs[1],
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
121
            r'"HEAD /not-found HTTP/1.." 404 - "-" "bzr/')
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
122
123
    def test_http_get(self):
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
124
        server = self.get_readonly_server()
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
125
        t = self._transport(server.get_url())
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
126
        fp = t.get('foo/bar')
127
        self.assertEqualDiff(
128
            fp.read(),
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
129
            'contents of foo/bar\n')
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
130
        self.assertEqual(len(server.logs), 1)
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
131
        self.assertTrue(server.logs[0].find(
132
            '"GET /foo/bar HTTP/1.1" 200 - "-" "bzr/%s' % bzrlib.__version__) > -1)
133
134
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
135
class TestHttpConnections_urllib(TestCaseWithWebserver, TestHttpMixins):
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
136
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
137
    _transport = HttpTransport_urllib
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
138
139
    def setUp(self):
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
140
        TestCaseWithWebserver.setUp(self)
141
        self._prep_tree()
142
2000.2.2 by John Arbash Meinel
Update the urllib.has test.
143
    def test_has_on_bogus_host(self):
2004.1.1 by vila
Connection sharing, with redirection. without authentification.
144
        # Get a free address and don't 'accept' on it, so that we
145
        # can be sure there is no http handler there, but set a
146
        # reasonable timeout to not slow down tests too much.
147
        default_timeout = socket.getdefaulttimeout()
148
        try:
149
            socket.setdefaulttimeout(2)
150
            s = socket.socket()
151
            s.bind(('localhost', 0))
152
            t = self._transport('http://%s:%s/' % s.getsockname())
153
            self.assertRaises(ConnectionError, t.has, 'foo/bar')
154
        finally:
155
            socket.setdefaulttimeout(default_timeout)
156
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
157
158
class TestHttpConnections_pycurl(TestCaseWithWebserver, TestHttpMixins):
159
160
    def _get_pycurl_maybe(self):
1540.3.29 by Martin Pool
Prevent selftest failure when pycurl is not installed
161
        try:
162
            from bzrlib.transport.http._pycurl import PyCurlTransport
1612.1.1 by Martin Pool
Raise errors correctly on pycurl connection failure
163
            return PyCurlTransport
1540.3.30 by Martin Pool
Fix up bogus-url tests for broken dns servers, and error imports
164
        except DependencyNotPresent:
1540.3.29 by Martin Pool
Prevent selftest failure when pycurl is not installed
165
            raise TestSkipped('pycurl not present')
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
166
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
167
    _transport = property(_get_pycurl_maybe)
168
169
    def setUp(self):
170
        TestCaseWithWebserver.setUp(self)
171
        self._prep_tree()
172
173
1540.3.23 by Martin Pool
Allow urls like http+pycurl://host/ to use a particular impl
174
class TestHttpTransportRegistration(TestCase):
175
    """Test registrations of various http implementations"""
176
177
    def test_http_registered(self):
178
        import bzrlib.transport.http._urllib
179
        from bzrlib.transport import get_transport
180
        # urlllib should always be present
181
        t = get_transport('http+urllib://bzr.google.com/')
182
        self.assertIsInstance(t, Transport)
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
183
        self.assertIsInstance(t, bzrlib.transport.http._urllib.HttpTransport_urllib)
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
184
185
186
class TestOffsets(TestCase):
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
187
    """Test offsets_to_ranges method"""
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
188
189
    def test_offsets_to_ranges_simple(self):
190
        to_range = HttpTransportBase.offsets_to_ranges
1786.1.39 by John Arbash Meinel
Remove the ability to read negative offsets from readv()
191
        ranges = to_range([(10, 1)])
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
192
        self.assertEqual([[10, 10]], ranges)
1786.1.39 by John Arbash Meinel
Remove the ability to read negative offsets from readv()
193
194
        ranges = to_range([(0, 1), (1, 1)])
195
        self.assertEqual([[0, 1]], ranges)
196
197
        ranges = to_range([(1, 1), (0, 1)])
198
        self.assertEqual([[0, 1]], ranges)
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
199
200
    def test_offset_to_ranges_overlapped(self):
201
        to_range = HttpTransportBase.offsets_to_ranges
202
1786.1.39 by John Arbash Meinel
Remove the ability to read negative offsets from readv()
203
        ranges = to_range([(10, 1), (20, 2), (22, 5)])
204
        self.assertEqual([[10, 10], [20, 26]], ranges)
205
206
        ranges = to_range([(10, 1), (11, 2), (22, 5)])
207
        self.assertEqual([[10, 12], [22, 26]], ranges)
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
208
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
209
210
class TestRangeHeader(TestCase):
211
    """Test range_header method"""
212
213
    def check_header(self, value, ranges=[], tail=0):
214
        range_header = HttpTransportBase.range_header
215
        self.assertEqual(value, range_header(ranges, tail))
216
217
    def test_range_header_single(self):
1786.1.36 by John Arbash Meinel
pycurl expects us to just set the range of bytes, not including bytes=
218
        self.check_header('0-9', ranges=[[0,9]])
219
        self.check_header('100-109', ranges=[[100,109]])
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
220
221
    def test_range_header_tail(self):
1786.1.36 by John Arbash Meinel
pycurl expects us to just set the range of bytes, not including bytes=
222
        self.check_header('-10', tail=10)
223
        self.check_header('-50', tail=50)
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
224
225
    def test_range_header_multi(self):
1786.1.36 by John Arbash Meinel
pycurl expects us to just set the range of bytes, not including bytes=
226
        self.check_header('0-9,100-200,300-5000',
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
227
                          ranges=[(0,9), (100, 200), (300,5000)])
228
229
    def test_range_header_mixed(self):
1786.1.36 by John Arbash Meinel
pycurl expects us to just set the range of bytes, not including bytes=
230
        self.check_header('0-9,300-5000,-50',
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
231
                          ranges=[(0,9), (300,5000)],
232
                          tail=50)
2004.3.1 by vila
Test ConnectionError exceptions.
233
234
# TODO: We need to generalize the following tests to all
235
# transports which connect to a server via a socket. Is there a
236
# way to add an accessor or an attribute to this transports so
237
# that we can filter them from the list of all existing
238
# transports ?
239
class TestWallServer(TestCaseWithWallserver):
240
    """Tests that we get the right exceptions during the connection phase"""
241
242
    from bzrlib.transport.http._pycurl import PyCurlTransport
243
#    _transport = PyCurlTransport
244
    _transport = HttpTransport_urllib
245
246
    def test_has(self):
247
        server = self.get_readonly_server()
248
        t = self._transport(server.get_url())
249
        self.assertRaises(ConnectionError, t.has, 'foo/bar')