bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
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 threading |
4 |
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler |
|
5 |
import urllib2 |
|
6 |
||
7 |
import bzrlib |
|
|
1185.31.25
by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py |
8 |
from bzrlib.tests import 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 |
9 |
from bzrlib.transport.http import HttpTransport, extract_auth |
10 |
||
11 |
class FakeManager (object): |
|
12 |
def __init__(self): |
|
13 |
self.credentials = [] |
|
14 |
||
15 |
def add_password(self, realm, host, username, password): |
|
16 |
self.credentials.append([realm, host, username, password]) |
|
17 |
||
|
1553.1.2
by James Henstridge
Add a test to make sure the user-agent header is being sent correctly. |
18 |
|
|
1185.16.68
by Martin Pool
- http url fixes suggested by Robey Pointer, and tests |
19 |
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 |
20 |
def test_url_parsing(self): |
21 |
f = FakeManager() |
|
22 |
url = extract_auth('http://example.com', f) |
|
23 |
self.assertEquals('http://example.com', url) |
|
24 |
self.assertEquals(0, len(f.credentials)) |
|
25 |
url = extract_auth('http://user:pass@www.bazaar-ng.org/bzr/bzr.dev', f) |
|
26 |
self.assertEquals('http://www.bazaar-ng.org/bzr/bzr.dev', url) |
|
27 |
self.assertEquals(1, len(f.credentials)) |
|
28 |
self.assertEquals([None, 'www.bazaar-ng.org', 'user', 'pass'], f.credentials[0]) |
|
29 |
||
|
1185.16.68
by Martin Pool
- http url fixes suggested by Robey Pointer, and tests |
30 |
def test_abs_url(self): |
31 |
"""Construction of absolute http URLs""" |
|
32 |
t = HttpTransport('http://bazaar-ng.org/bzr/bzr.dev/') |
|
33 |
eq = self.assertEqualDiff |
|
34 |
eq(t.abspath('.'), |
|
35 |
'http://bazaar-ng.org/bzr/bzr.dev') |
|
36 |
eq(t.abspath('foo/bar'), |
|
37 |
'http://bazaar-ng.org/bzr/bzr.dev/foo/bar') |
|
38 |
eq(t.abspath('.bzr'), |
|
39 |
'http://bazaar-ng.org/bzr/bzr.dev/.bzr') |
|
40 |
eq(t.abspath('.bzr/1//2/./3'), |
|
41 |
'http://bazaar-ng.org/bzr/bzr.dev/.bzr/1/2/3') |
|
42 |
||
43 |
def test_invalid_http_urls(self): |
|
44 |
"""Trap invalid construction of urls""" |
|
45 |
t = HttpTransport('http://bazaar-ng.org/bzr/bzr.dev/') |
|
46 |
self.assertRaises(ValueError, |
|
47 |
t.abspath, |
|
48 |
'.bzr/') |
|
49 |
self.assertRaises(ValueError, |
|
50 |
t.abspath, |
|
51 |
'/.bzr') |
|
52 |
||
53 |
def test_http_root_urls(self): |
|
54 |
"""Construction of URLs from server root""" |
|
55 |
t = HttpTransport('http://bzr.ozlabs.org/') |
|
56 |
eq = self.assertEqualDiff |
|
57 |
eq(t.abspath('.bzr/tree-version'), |
|
58 |
'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. |
59 |
|
60 |
||
61 |
class RequestHandler(BaseHTTPRequestHandler): |
|
62 |
def do_GET(self): |
|
63 |
self.send_response(200) |
|
64 |
self.send_header('Content-type', 'text/plain;charset=UTF-8') |
|
65 |
self.end_headers() |
|
66 |
self.wfile.write('Path: %s\nUser-agent: %s\n' % |
|
67 |
(self.path, self.headers.getheader('user-agent', ''))) |
|
68 |
self.close_connection = True |
|
69 |
||
70 |
||
71 |
class TestHttpConnections(TestCase): |
|
72 |
||
73 |
def setUp(self): |
|
74 |
"""Set up a dummy HTTP server as a thread. |
|
75 |
||
76 |
The server will serve a single request and then quit.
|
|
77 |
"""
|
|
78 |
super(TestHttpConnections, self).setUp() |
|
79 |
self.httpd = HTTPServer(('127.0.0.1', 0), RequestHandler) |
|
80 |
host, port = self.httpd.socket.getsockname() |
|
81 |
self.baseurl = 'http://127.0.0.1:%d/' % port |
|
82 |
self.quit_server = False |
|
83 |
self.thread = threading.Thread(target=self._run_http) |
|
84 |
self.thread.start() |
|
85 |
||
86 |
def _run_http(self): |
|
87 |
while not self.quit_server: |
|
88 |
self.httpd.handle_request() |
|
89 |
self.httpd.server_close() |
|
90 |
||
91 |
def tearDown(self): |
|
92 |
# tell the server to quit, and issue a request to make sure the
|
|
93 |
# mainloop gets run
|
|
94 |
self.quit_server = True |
|
95 |
try: |
|
96 |
response = urllib2.urlopen(self.baseurl) |
|
97 |
response.read() |
|
98 |
except IOError: |
|
99 |
# ignore error, in case server has already quit
|
|
100 |
pass
|
|
101 |
self.thread.join() |
|
102 |
||
103 |
super(TestHttpConnections, self).tearDown() |
|
104 |
||
105 |
def test_http_has(self): |
|
106 |
t = HttpTransport(self.baseurl) |
|
107 |
self.assertEqual(t.has('foo/bar'), True) |
|
108 |
||
109 |
def test_http_get(self): |
|
110 |
t = HttpTransport(self.baseurl) |
|
111 |
fp = t.get('foo/bar') |
|
112 |
self.assertEqualDiff( |
|
113 |
fp.read(), |
|
114 |
'Path: /foo/bar\nUser-agent: bzr/%s\n' % bzrlib.__version__) |