29
33
from bzrlib.smart import protocol
30
from bzrlib.tests import TestCaseWithTransport
31
from bzrlib.tests.http_server import (
33
TestingHTTPRequestHandler,
35
from bzrlib.transport import (
40
class HTTPServerWithSmarts(HttpServer):
34
from bzrlib.tests import http_server
37
class HTTPServerWithSmarts(http_server.HttpServer):
41
38
"""HTTPServerWithSmarts extends the HttpServer with POST methods that will
42
39
trigger a smart server to execute with a transport rooted at the rootdir of
46
43
def __init__(self):
47
HttpServer.__init__(self, SmartRequestHandler)
50
class SmartRequestHandler(TestingHTTPRequestHandler):
44
http_server.HttpServer.__init__(self, SmartRequestHandler)
47
class SmartRequestHandler(http_server.TestingHTTPRequestHandler):
51
48
"""Extend TestingHTTPRequestHandler to support smart client POSTs."""
54
51
"""Hand the request off to a smart server instance."""
55
52
self.send_response(200)
56
53
self.send_header("Content-type", "application/octet-stream")
57
transport = get_transport(self.server.test_case_server._home_dir)
54
t = transport.get_transport(self.server.test_case_server._home_dir)
58
55
# TODO: We might like to support streaming responses. 1.0 allows no
59
56
# Content-length in this case, so for integrity we should perform our
60
57
# own chunking within the stream.
64
61
# HTTP trailer facility which may not be widely available.
65
62
out_buffer = StringIO()
66
63
smart_protocol_request = protocol.SmartServerRequestProtocolOne(
67
transport, out_buffer.write)
68
65
# if this fails, we should return 400 bad request, but failure is
69
66
# failure for now - RBC 20060919
70
67
data_length = int(self.headers['Content-Length'])
79
76
self.wfile.write(out_buffer.getvalue())
82
class TestCaseWithWebserver(TestCaseWithTransport):
79
class TestCaseWithWebserver(tests.TestCaseWithTransport):
83
80
"""A support class that provides readonly urls that are http://.
85
82
This is done by forcing the readonly server to be an http
90
87
super(TestCaseWithWebserver, self).setUp()
91
self.transport_readonly_server = HttpServer
88
self.transport_readonly_server = http_server.HttpServer
94
91
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
101
98
super(TestCaseWithTwoWebservers, self).setUp()
102
self.transport_secondary_server = HttpServer
99
self.transport_secondary_server = http_server.HttpServer
103
100
self.__secondary_server = None
105
102
def create_transport_secondary_server(self):
118
115
return self.__secondary_server
121
class ProxyServer(HttpServer):
118
class ProxyServer(http_server.HttpServer):
122
119
"""A proxy test server for http transports."""
124
121
proxy_requests = True
127
class RedirectRequestHandler(TestingHTTPRequestHandler):
124
class RedirectRequestHandler(http_server.TestingHTTPRequestHandler):
128
125
"""Redirect all request to the specified server"""
130
127
def parse_request(self):
131
128
"""Redirect a single HTTP request to another host"""
132
valid = TestingHTTPRequestHandler.parse_request(self)
129
valid = http_server.TestingHTTPRequestHandler.parse_request(self)
134
131
tcs = self.server.test_case_server
135
132
code, target = tcs.is_redirected(self.path)
148
class HTTPServerRedirecting(HttpServer):
145
class HTTPServerRedirecting(http_server.HttpServer):
149
146
"""An HttpServer redirecting to another server """
151
148
def __init__(self, request_handler=RedirectRequestHandler):
152
HttpServer.__init__(self, request_handler)
149
http_server.HttpServer.__init__(self, request_handler)
153
150
# redirections is a list of tuples (source, target, code)
154
151
# - source is a regexp for the paths requested
155
152
# - target is a replacement for re.sub describing where
207
204
self.old_server = self.get_secondary_server()
210
class AuthRequestHandler(TestingHTTPRequestHandler):
207
class AuthRequestHandler(http_server.TestingHTTPRequestHandler):
211
208
"""Requires an authentication to process requests.
213
210
This is intended to be used with a server that always and
223
220
def do_GET(self):
224
221
if self.authorized():
225
return TestingHTTPRequestHandler.do_GET(self)
222
return http_server.TestingHTTPRequestHandler.do_GET(self)
227
224
# Note that we must update test_case_server *before*
228
225
# sending the error or the client may try to read it
293
290
self.send_header(tcs.auth_header_sent,header)
296
class AuthServer(HttpServer):
293
class AuthServer(http_server.HttpServer):
297
294
"""Extends HttpServer with a dictionary of passwords.
299
296
This is used as a base class for various schemes which should
311
308
auth_realm = "Thou should not pass"
313
310
def __init__(self, request_handler, auth_scheme):
314
HttpServer.__init__(self, request_handler)
311
http_server.HttpServer.__init__(self, request_handler)
315
312
self.auth_scheme = auth_scheme
316
313
self.password_of = {}
317
314
self.auth_required_errors = 0