~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Martin Pool
  • Date: 2006-03-10 06:29:53 UTC
  • mfrom: (1608 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060310062953-bc1c7ade75c89a7a
[merge] bzr.dev; pycurl not updated for readv yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
# FIXME: This test should be repeated for each available http client
 
18
# implementation; at the moment we have urllib and pycurl.
 
19
 
 
20
# TODO: Should be renamed to bzrlib.transport.http.tests?
 
21
 
 
22
import bzrlib
 
23
import bzrlib.errors as errors
 
24
from bzrlib.tests import TestCase
 
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.transport.http._pycurl import PyCurlTransport
 
29
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
 
30
 
 
31
class FakeManager (object):
 
32
    def __init__(self):
 
33
        self.credentials = []
 
34
        
 
35
    def add_password(self, realm, host, username, password):
 
36
        self.credentials.append([realm, host, username, password])
 
37
 
 
38
 
 
39
class TestHttpUrls(TestCase):
 
40
    def test_url_parsing(self):
 
41
        f = FakeManager()
 
42
        url = extract_auth('http://example.com', f)
 
43
        self.assertEquals('http://example.com', url)
 
44
        self.assertEquals(0, len(f.credentials))
 
45
        url = extract_auth('http://user:pass@www.bazaar-ng.org/bzr/bzr.dev', f)
 
46
        self.assertEquals('http://www.bazaar-ng.org/bzr/bzr.dev', url)
 
47
        self.assertEquals(1, len(f.credentials))
 
48
        self.assertEquals([None, 'www.bazaar-ng.org', 'user', 'pass'], f.credentials[0])
 
49
        
 
50
    def test_abs_url(self):
 
51
        """Construction of absolute http URLs"""
 
52
        t = HttpTransport_urllib('http://bazaar-ng.org/bzr/bzr.dev/')
 
53
        eq = self.assertEqualDiff
 
54
        eq(t.abspath('.'),
 
55
           'http://bazaar-ng.org/bzr/bzr.dev')
 
56
        eq(t.abspath('foo/bar'), 
 
57
           'http://bazaar-ng.org/bzr/bzr.dev/foo/bar')
 
58
        eq(t.abspath('.bzr'),
 
59
           'http://bazaar-ng.org/bzr/bzr.dev/.bzr')
 
60
        eq(t.abspath('.bzr/1//2/./3'),
 
61
           'http://bazaar-ng.org/bzr/bzr.dev/.bzr/1/2/3')
 
62
 
 
63
    def test_invalid_http_urls(self):
 
64
        """Trap invalid construction of urls"""
 
65
        t = HttpTransport_urllib('http://bazaar-ng.org/bzr/bzr.dev/')
 
66
        self.assertRaises(ValueError,
 
67
            t.abspath,
 
68
            '.bzr/')
 
69
        self.assertRaises(ValueError,
 
70
            t.abspath,
 
71
            '/.bzr')
 
72
 
 
73
    def test_http_root_urls(self):
 
74
        """Construction of URLs from server root"""
 
75
        t = HttpTransport_urllib('http://bzr.ozlabs.org/')
 
76
        eq = self.assertEqualDiff
 
77
        eq(t.abspath('.bzr/tree-version'),
 
78
           'http://bzr.ozlabs.org/.bzr/tree-version')
 
79
 
 
80
    def test_http_impl_urls(self):
 
81
        """There are servers which ask for particular clients to connect"""
 
82
        try:
 
83
            from bzrlib.transport.http._pycurl import HttpServer_PyCurl
 
84
            server = HttpServer_PyCurl()
 
85
            try:
 
86
                server.setUp()
 
87
                url = server.get_url()
 
88
                self.assertTrue(url.startswith('http+pycurl://'))
 
89
            finally:
 
90
                server.tearDown()
 
91
        except errors.DependencyNotPresent:
 
92
            raise TestSkipped('pycurl not present')
 
93
 
 
94
class TestHttpConnections(TestCaseWithWebserver):
 
95
 
 
96
    _transport = HttpTransport_urllib
 
97
 
 
98
    def setUp(self):
 
99
        super(TestHttpConnections, self).setUp()
 
100
        self.build_tree(['xxx', 'foo/', 'foo/bar'], line_endings='binary')
 
101
 
 
102
    def test_http_has(self):
 
103
        server = self.get_readonly_server()
 
104
        t = self._transport(server.get_url())
 
105
        self.assertEqual(t.has('foo/bar'), True)
 
106
        self.assertEqual(len(server.logs), 1)
 
107
        self.assertContainsRe(server.logs[0], 
 
108
            r'"HEAD /foo/bar HTTP/1.." (200|302) - "-" "bzr/')
 
109
 
 
110
    def test_http_has_not_found(self):
 
111
        server = self.get_readonly_server()
 
112
        t = self._transport(server.get_url())
 
113
        self.assertEqual(t.has('not-found'), False)
 
114
        self.assertContainsRe(server.logs[1], 
 
115
            r'"HEAD /not-found HTTP/1.." 404 - "-" "bzr/')
 
116
 
 
117
    def test_http_get(self):
 
118
        server = self.get_readonly_server()
 
119
        t = self._transport(server.get_url())
 
120
        fp = t.get('foo/bar')
 
121
        self.assertEqualDiff(
 
122
            fp.read(),
 
123
            'contents of foo/bar\n')
 
124
        self.assertEqual(len(server.logs), 1)
 
125
        self.assertTrue(server.logs[0].find(
 
126
            '"GET /foo/bar HTTP/1.1" 200 - "-" "bzr/%s' % bzrlib.__version__) > -1)
 
127
 
 
128
 
 
129
class TestHttpConnections_pycurl(TestHttpConnections):
 
130
    _transport = PyCurlTransport
 
131
 
 
132
    def setUp(self):
 
133
        super(TestHttpConnections_pycurl, self).setUp()
 
134
 
 
135
 
 
136
class TestHttpTransportRegistration(TestCase):
 
137
    """Test registrations of various http implementations"""
 
138
 
 
139
    def test_http_registered(self):
 
140
        import bzrlib.transport.http._urllib
 
141
        from bzrlib.transport import get_transport
 
142
        # urlllib should always be present
 
143
        t = get_transport('http+urllib://bzr.google.com/')
 
144
        self.assertIsInstance(t, Transport)
 
145
        self.assertIsInstance(t, bzrlib.transport.http._urllib.HttpTransport_urllib)