~bzr-pqm/bzr/bzr.dev

1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
1
# (C) 2005 Canonical
2
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
3
import bzrlib
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
4
from bzrlib.tests import TestCase
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
5
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
6
from bzrlib.transport.http import HttpTransport, extract_auth
7
8
class FakeManager (object):
9
    def __init__(self):
10
        self.credentials = []
11
        
12
    def add_password(self, realm, host, username, password):
13
        self.credentials.append([realm, host, username, password])
14
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
15
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
16
class TestHttpUrls(TestCase):
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
17
    def test_url_parsing(self):
18
        f = FakeManager()
19
        url = extract_auth('http://example.com', f)
20
        self.assertEquals('http://example.com', url)
21
        self.assertEquals(0, len(f.credentials))
22
        url = extract_auth('http://user:pass@www.bazaar-ng.org/bzr/bzr.dev', f)
23
        self.assertEquals('http://www.bazaar-ng.org/bzr/bzr.dev', url)
24
        self.assertEquals(1, len(f.credentials))
25
        self.assertEquals([None, 'www.bazaar-ng.org', 'user', 'pass'], f.credentials[0])
26
        
1185.16.68 by Martin Pool
- http url fixes suggested by Robey Pointer, and tests
27
    def test_abs_url(self):
28
        """Construction of absolute http URLs"""
29
        t = HttpTransport('http://bazaar-ng.org/bzr/bzr.dev/')
30
        eq = self.assertEqualDiff
31
        eq(t.abspath('.'),
32
           'http://bazaar-ng.org/bzr/bzr.dev')
33
        eq(t.abspath('foo/bar'), 
34
           'http://bazaar-ng.org/bzr/bzr.dev/foo/bar')
35
        eq(t.abspath('.bzr'),
36
           'http://bazaar-ng.org/bzr/bzr.dev/.bzr')
37
        eq(t.abspath('.bzr/1//2/./3'),
38
           'http://bazaar-ng.org/bzr/bzr.dev/.bzr/1/2/3')
39
40
    def test_invalid_http_urls(self):
41
        """Trap invalid construction of urls"""
42
        t = HttpTransport('http://bazaar-ng.org/bzr/bzr.dev/')
43
        self.assertRaises(ValueError,
44
            t.abspath,
45
            '.bzr/')
46
        self.assertRaises(ValueError,
47
            t.abspath,
48
            '/.bzr')
49
50
    def test_http_root_urls(self):
51
        """Construction of URLs from server root"""
52
        t = HttpTransport('http://bzr.ozlabs.org/')
53
        eq = self.assertEqualDiff
54
        eq(t.abspath('.bzr/tree-version'),
55
           '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.
56
57
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
58
class TestHttpConnections(TestCaseWithWebserver):
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
59
60
    def setUp(self):
61
        super(TestHttpConnections, self).setUp()
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
62
        self.build_tree(['xxx', 'foo/', 'foo/bar'], line_endings='binary')
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
63
64
    def test_http_has(self):
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
65
        server = self.get_readonly_server()
66
        t = HttpTransport(server.get_url())
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
67
        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.
68
        self.assertEqual(len(server.logs), 1)
69
        self.assertTrue(server.logs[0].endswith(
1553.1.5 by James Henstridge
Make HTTP transport has() method do HEAD requests, and update test to
70
            '"HEAD /foo/bar HTTP/1.1" 200 - "-" "bzr/%s"'
71
            % bzrlib.__version__))
72
73
        self.assertEqual(t.has('not-found'), False)
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
74
        self.assertTrue(server.logs[-1].endswith(
1553.1.5 by James Henstridge
Make HTTP transport has() method do HEAD requests, and update test to
75
            '"HEAD /not-found HTTP/1.1" 404 - "-" "bzr/%s"'
76
            % bzrlib.__version__))
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
77
78
    def test_http_get(self):
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
79
        server = self.get_readonly_server()
80
        t = HttpTransport(server.get_url())
1553.1.2 by James Henstridge
Add a test to make sure the user-agent header is being sent correctly.
81
        fp = t.get('foo/bar')
82
        self.assertEqualDiff(
83
            fp.read(),
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
84
            'contents of foo/bar\n')
1185.50.84 by John Arbash Meinel
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.
85
        self.assertEqual(len(server.logs), 1)
86
        self.assertTrue(server.logs[0].endswith(
1553.1.3 by James Henstridge
Make bzrlib.transport.http.HttpServer output referer and user agent as in
87
            '"GET /foo/bar HTTP/1.1" 200 - "-" "bzr/%s"' % bzrlib.__version__))