4
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
8
4
from bzrlib.tests import TestCase
5
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
9
6
from bzrlib.transport.http import HttpTransport, extract_auth
11
8
class FakeManager (object):
58
55
'http://bzr.ozlabs.org/.bzr/tree-version')
61
class RequestHandler(BaseHTTPRequestHandler):
63
self.send_response(200)
64
self.send_header('Content-type', 'text/plain;charset=UTF-8')
66
self.wfile.write('Path: %s\nUser-agent: %s\n' %
67
(self.path, self.headers.getheader('user-agent', '')))
68
self.close_connection = True
71
class TestHttpConnections(TestCase):
58
class TestHttpConnections(TestCaseWithWebserver):
74
"""Set up a dummy HTTP server as a thread.
76
The server will serve a single request and then quit.
78
61
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)
87
while not self.quit_server:
88
self.httpd.handle_request()
89
self.httpd.server_close()
92
# tell the server to quit, and issue a request to make sure the
94
self.quit_server = True
96
response = urllib2.urlopen(self.baseurl)
99
# ignore error, in case server has already quit
103
super(TestHttpConnections, self).tearDown()
62
self.build_tree(['xxx', 'foo/', 'foo/bar'], line_endings='binary')
105
64
def test_http_has(self):
106
t = HttpTransport(self.baseurl)
65
t = HttpTransport(self.server.get_url())
107
66
self.assertEqual(t.has('foo/bar'), True)
109
68
def test_http_get(self):
110
t = HttpTransport(self.baseurl)
69
t = HttpTransport(self.server.get_url())
111
70
fp = t.get('foo/bar')
112
71
self.assertEqualDiff(
114
'Path: /foo/bar\nUser-agent: bzr/%s\n' % bzrlib.__version__)
73
'contents of foo/bar\n')
74
self.assertEqual(len(self.server.logs), 1)
75
self.assertTrue(self.server.logs[0].endswith(
76
'"GET /foo/bar HTTP/1.1" 200 - "-" "bzr/%s"' % bzrlib.__version__))