/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
1540.3.30 by Martin Pool
Fix up bogus-url tests for broken dns servers, and error imports
25
from bzrlib.errors import DependencyNotPresent
26
from bzrlib.tests import TestCase, TestSkipped
1540.3.23 by Martin Pool
Allow urls like http+pycurl://host/ to use a particular impl
27
from bzrlib.transport import Transport
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
28
from bzrlib.transport.http import extract_auth, HttpTransportBase
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
29
from bzrlib.transport.http._urllib import HttpTransport_urllib
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
30
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
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
31
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
32
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
33
class FakeManager (object):
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
34
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
35
    def __init__(self):
36
        self.credentials = []
37
        
38
    def add_password(self, realm, host, username, password):
39
        self.credentials.append([realm, host, username, password])
40
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
41
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
42
class TestHttpUrls(TestCase):
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
43
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
44
    def test_url_parsing(self):
45
        f = FakeManager()
46
        url = extract_auth('http://example.com', f)
47
        self.assertEquals('http://example.com', url)
48
        self.assertEquals(0, len(f.credentials))
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
49
        url = extract_auth('http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
50
        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
51
        self.assertEquals(1, len(f.credentials))
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
52
        self.assertEquals([None, 'www.bazaar-vcs.org', 'user', 'pass'], f.credentials[0])
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
53
        
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
54
    def test_abs_url(self):
55
        """Construction of absolute http URLs"""
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
56
        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
57
        eq = self.assertEqualDiff
58
        eq(t.abspath('.'),
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
59
           'http://bazaar-vcs.org/bzr/bzr.dev')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
60
        eq(t.abspath('foo/bar'), 
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
61
           'http://bazaar-vcs.org/bzr/bzr.dev/foo/bar')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
62
        eq(t.abspath('.bzr'),
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
63
           'http://bazaar-vcs.org/bzr/bzr.dev/.bzr')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
64
        eq(t.abspath('.bzr/1//2/./3'),
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
65
           '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
66
67
    def test_invalid_http_urls(self):
68
        """Trap invalid construction of urls"""
1185.50.94 by John Arbash Meinel
Updated web page url to http://bazaar-vcs.org
69
        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
70
        self.assertRaises(ValueError,
71
            t.abspath,
72
            '.bzr/')
73
        self.assertRaises(ValueError,
74
            t.abspath,
75
            '/.bzr')
76
77
    def test_http_root_urls(self):
78
        """Construction of URLs from server root"""
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
79
        t = HttpTransport_urllib('http://bzr.ozlabs.org/')
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
80
        eq = self.assertEqualDiff
81
        eq(t.abspath('.bzr/tree-version'),
82
           '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.
83
1540.3.24 by Martin Pool
Add new protocol 'http+pycurl' that always uses PyCurl.
84
    def test_http_impl_urls(self):
85
        """There are servers which ask for particular clients to connect"""
86
        try:
87
            from bzrlib.transport.http._pycurl import HttpServer_PyCurl
88
            server = HttpServer_PyCurl()
89
            try:
90
                server.setUp()
91
                url = server.get_url()
92
                self.assertTrue(url.startswith('http+pycurl://'))
93
            finally:
94
                server.tearDown()
1540.3.30 by Martin Pool
Fix up bogus-url tests for broken dns servers, and error imports
95
        except DependencyNotPresent:
1540.3.24 by Martin Pool
Add new protocol 'http+pycurl' that always uses PyCurl.
96
            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.
97
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
98
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
99
class TestHttpMixins(object):
100
101
    def _prep_tree(self):
102
        self.build_tree(['xxx', 'foo/', 'foo/bar'], line_endings='binary',
103
                        transport=self.get_transport())
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
104
105
    def test_http_has(self):
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
106
        server = self.get_readonly_server()
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
107
        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.
108
        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.
109
        self.assertEqual(len(server.logs), 1)
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
110
        self.assertContainsRe(server.logs[0], 
111
            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
112
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
113
    def test_http_has_not_found(self):
114
        server = self.get_readonly_server()
115
        t = self._transport(server.get_url())
1553.1.5 by James Henstridge
Make HTTP transport has() method do HEAD requests, and update test to
116
        self.assertEqual(t.has('not-found'), False)
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
117
        self.assertContainsRe(server.logs[1], 
118
            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.
119
120
    def test_http_get(self):
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
121
        server = self.get_readonly_server()
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
122
        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.
123
        fp = t.get('foo/bar')
124
        self.assertEqualDiff(
125
            fp.read(),
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
126
            'contents of foo/bar\n')
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
127
        self.assertEqual(len(server.logs), 1)
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
128
        self.assertTrue(server.logs[0].find(
129
            '"GET /foo/bar HTTP/1.1" 200 - "-" "bzr/%s' % bzrlib.__version__) > -1)
130
131
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
132
class TestHttpConnections_urllib(TestCaseWithWebserver, TestHttpMixins):
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
133
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
134
    _transport = HttpTransport_urllib
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
135
136
    def setUp(self):
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
137
        TestCaseWithWebserver.setUp(self)
138
        self._prep_tree()
139
2000.2.2 by John Arbash Meinel
Update the urllib.has test.
140
    def test_has_on_bogus_host(self):
141
        import urllib2
142
        # Get a random address, so that we can be sure there is no
143
        # http handler there.
144
        s = socket.socket()
145
        s.bind(('localhost', 0))
146
        t = self._transport('http://%s:%s/' % s.getsockname())
147
        self.assertRaises(urllib2.URLError, t.has, 'foo/bar')
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
148
149
150
class TestHttpConnections_pycurl(TestCaseWithWebserver, TestHttpMixins):
151
152
    def _get_pycurl_maybe(self):
1540.3.29 by Martin Pool
Prevent selftest failure when pycurl is not installed
153
        try:
154
            from bzrlib.transport.http._pycurl import PyCurlTransport
1612.1.1 by Martin Pool
Raise errors correctly on pycurl connection failure
155
            return PyCurlTransport
1540.3.30 by Martin Pool
Fix up bogus-url tests for broken dns servers, and error imports
156
        except DependencyNotPresent:
1540.3.29 by Martin Pool
Prevent selftest failure when pycurl is not installed
157
            raise TestSkipped('pycurl not present')
1540.3.15 by Martin Pool
[merge] large merge to sync with bzr.dev
158
1540.3.33 by Martin Pool
Fix http tests that were failing to run tearDown when setup got a missing dependency
159
    _transport = property(_get_pycurl_maybe)
160
161
    def setUp(self):
162
        TestCaseWithWebserver.setUp(self)
163
        self._prep_tree()
164
165
1540.3.23 by Martin Pool
Allow urls like http+pycurl://host/ to use a particular impl
166
167
class TestHttpTransportRegistration(TestCase):
168
    """Test registrations of various http implementations"""
169
170
    def test_http_registered(self):
171
        import bzrlib.transport.http._urllib
172
        from bzrlib.transport import get_transport
173
        # urlllib should always be present
174
        t = get_transport('http+urllib://bzr.google.com/')
175
        self.assertIsInstance(t, Transport)
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
176
        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.
177
178
179
class TestOffsets(TestCase):
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
180
    """Test offsets_to_ranges method"""
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
181
182
    def test_offsets_to_ranges_simple(self):
183
        to_range = HttpTransportBase.offsets_to_ranges
1786.1.39 by John Arbash Meinel
Remove the ability to read negative offsets from readv()
184
        ranges = to_range([(10, 1)])
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
185
        self.assertEqual([[10, 10]], ranges)
1786.1.39 by John Arbash Meinel
Remove the ability to read negative offsets from readv()
186
187
        ranges = to_range([(0, 1), (1, 1)])
188
        self.assertEqual([[0, 1]], ranges)
189
190
        ranges = to_range([(1, 1), (0, 1)])
191
        self.assertEqual([[0, 1]], ranges)
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
192
193
    def test_offset_to_ranges_overlapped(self):
194
        to_range = HttpTransportBase.offsets_to_ranges
195
1786.1.39 by John Arbash Meinel
Remove the ability to read negative offsets from readv()
196
        ranges = to_range([(10, 1), (20, 2), (22, 5)])
197
        self.assertEqual([[10, 10], [20, 26]], ranges)
198
199
        ranges = to_range([(10, 1), (11, 2), (22, 5)])
200
        self.assertEqual([[10, 12], [22, 26]], ranges)
1786.1.23 by John Arbash Meinel
Move offset_to_http_ranges back onto HttpTransportBase, clarify tests.
201
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
202
203
class TestRangeHeader(TestCase):
204
    """Test range_header method"""
205
206
    def check_header(self, value, ranges=[], tail=0):
207
        range_header = HttpTransportBase.range_header
208
        self.assertEqual(value, range_header(ranges, tail))
209
210
    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=
211
        self.check_header('0-9', ranges=[[0,9]])
212
        self.check_header('100-109', ranges=[[100,109]])
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
213
214
    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=
215
        self.check_header('-10', tail=10)
216
        self.check_header('-50', tail=50)
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
217
218
    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=
219
        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
220
                          ranges=[(0,9), (100, 200), (300,5000)])
221
222
    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=
223
        self.check_header('0-9,300-5000,-50',
1786.1.28 by John Arbash Meinel
Update and add tests for the HttpTransportBase.range_header
224
                          ranges=[(0,9), (300,5000)],
225
                          tail=50)