1
# Copyright (C) 2005, 2006 Canonical
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.
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.
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
17
# FIXME: This test should be repeated for each available http client
18
# implementation; at the moment we have urllib and pycurl.
20
# TODO: Should be renamed to bzrlib.transport.http.tests?
23
from bzrlib.errors import DependencyNotPresent
24
from bzrlib.tests import TestCase, TestSkipped
25
from bzrlib.transport import Transport
26
from bzrlib.transport.http import extract_auth
27
from bzrlib.transport.http._urllib import HttpTransport_urllib
28
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
30
class FakeManager (object):
34
def add_password(self, realm, host, username, password):
35
self.credentials.append([realm, host, username, password])
38
class TestHttpUrls(TestCase):
39
def test_url_parsing(self):
41
url = extract_auth('http://example.com', f)
42
self.assertEquals('http://example.com', url)
43
self.assertEquals(0, len(f.credentials))
44
url = extract_auth('http://user:pass@www.bazaar-ng.org/bzr/bzr.dev', f)
45
self.assertEquals('http://www.bazaar-ng.org/bzr/bzr.dev', url)
46
self.assertEquals(1, len(f.credentials))
47
self.assertEquals([None, 'www.bazaar-ng.org', 'user', 'pass'], f.credentials[0])
49
def test_abs_url(self):
50
"""Construction of absolute http URLs"""
51
t = HttpTransport_urllib('http://bazaar-ng.org/bzr/bzr.dev/')
52
eq = self.assertEqualDiff
54
'http://bazaar-ng.org/bzr/bzr.dev')
55
eq(t.abspath('foo/bar'),
56
'http://bazaar-ng.org/bzr/bzr.dev/foo/bar')
58
'http://bazaar-ng.org/bzr/bzr.dev/.bzr')
59
eq(t.abspath('.bzr/1//2/./3'),
60
'http://bazaar-ng.org/bzr/bzr.dev/.bzr/1/2/3')
62
def test_invalid_http_urls(self):
63
"""Trap invalid construction of urls"""
64
t = HttpTransport_urllib('http://bazaar-ng.org/bzr/bzr.dev/')
65
self.assertRaises(ValueError,
68
self.assertRaises(ValueError,
72
def test_http_root_urls(self):
73
"""Construction of URLs from server root"""
74
t = HttpTransport_urllib('http://bzr.ozlabs.org/')
75
eq = self.assertEqualDiff
76
eq(t.abspath('.bzr/tree-version'),
77
'http://bzr.ozlabs.org/.bzr/tree-version')
79
def test_http_impl_urls(self):
80
"""There are servers which ask for particular clients to connect"""
82
from bzrlib.transport.http._pycurl import HttpServer_PyCurl
83
server = HttpServer_PyCurl()
86
url = server.get_url()
87
self.assertTrue(url.startswith('http+pycurl://'))
90
except DependencyNotPresent:
91
raise TestSkipped('pycurl not present')
93
class TestHttpMixins(object):
96
self.build_tree(['xxx', 'foo/', 'foo/bar'], line_endings='binary',
97
transport=self.get_transport())
99
def test_http_has(self):
100
server = self.get_readonly_server()
101
t = self._transport(server.get_url())
102
self.assertEqual(t.has('foo/bar'), True)
103
self.assertEqual(len(server.logs), 1)
104
self.assertContainsRe(server.logs[0],
105
r'"HEAD /foo/bar HTTP/1.." (200|302) - "-" "bzr/')
107
def test_http_has_not_found(self):
108
server = self.get_readonly_server()
109
t = self._transport(server.get_url())
110
self.assertEqual(t.has('not-found'), False)
111
self.assertContainsRe(server.logs[1],
112
r'"HEAD /not-found HTTP/1.." 404 - "-" "bzr/')
114
def test_http_get(self):
115
server = self.get_readonly_server()
116
t = self._transport(server.get_url())
117
fp = t.get('foo/bar')
118
self.assertEqualDiff(
120
'contents of foo/bar\n')
121
self.assertEqual(len(server.logs), 1)
122
self.assertTrue(server.logs[0].find(
123
'"GET /foo/bar HTTP/1.1" 200 - "-" "bzr/%s' % bzrlib.__version__) > -1)
126
class TestHttpConnections_urllib(TestCaseWithWebserver, TestHttpMixins):
127
_transport = HttpTransport_urllib
130
TestCaseWithWebserver.setUp(self)
135
class TestHttpConnections_pycurl(TestCaseWithWebserver, TestHttpMixins):
137
def _get_pycurl_maybe(self):
139
from bzrlib.transport.http._pycurl import PyCurlTransport
140
return PyCurlTransport
141
except DependencyNotPresent:
142
raise TestSkipped('pycurl not present')
144
_transport = property(_get_pycurl_maybe)
147
TestCaseWithWebserver.setUp(self)
152
class TestHttpTransportRegistration(TestCase):
153
"""Test registrations of various http implementations"""
155
def test_http_registered(self):
156
import bzrlib.transport.http._urllib
157
from bzrlib.transport import get_transport
158
# urlllib should always be present
159
t = get_transport('http+urllib://bzr.google.com/')
160
self.assertIsInstance(t, Transport)
161
self.assertIsInstance(t, bzrlib.transport.http._urllib.HttpTransport_urllib)