1
# Copyright (C) 2005 by Canonical Ltd
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.
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.
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
17
from cStringIO import StringIO
21
from bzrlib.tests import TestCaseWithTransport
22
from bzrlib.tests.HttpServer import (
24
TestingHTTPRequestHandler,
26
from bzrlib.transport import (
32
class WallRequestHandler(TestingHTTPRequestHandler):
33
"""Whatever request comes in, close the connection"""
35
def handle_one_request(self):
36
"""Handle a single HTTP request, by abruptly closing the connection"""
37
self.close_connection = 1
40
class BadStatusRequestHandler(TestingHTTPRequestHandler):
41
"""Whatever request comes in, returns a bad status"""
43
def parse_request(self):
44
"""Fakes handling a single HTTP request, returns a bad status"""
45
ignored = TestingHTTPRequestHandler.parse_request(self)
47
self.send_response(0, "Bad status")
49
except socket.error, e:
50
if (len(e.args) > 0) and (e.args[0] == errno.EPIPE):
51
# We don't want to pollute the test reuslts with
52
# spurious server errors while test succeed. In
53
# our case, it may occur that the test have
54
# already read the 'Bad Status' and closed the
55
# socket while we are still trying to send some
56
# headers... So the test is ok but if we raise
57
# the exception the output is dirty. So we don't
58
# raise, but we close the connection, just to be
60
self.close_connection = 1
67
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
68
"""Whatever request comes in, returns am invalid status"""
70
def parse_request(self):
71
"""Fakes handling a single HTTP request, returns a bad status"""
72
ignored = TestingHTTPRequestHandler.parse_request(self)
73
self.wfile.write("Invalid status line\r\n")
77
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
78
"""Whatever request comes in, returns a bad protocol version"""
80
def parse_request(self):
81
"""Fakes handling a single HTTP request, returns a bad status"""
82
ignored = TestingHTTPRequestHandler.parse_request(self)
83
# Returns an invalid protocol version, but curl just
84
# ignores it and those cannot be tested.
85
self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
87
'Look at my protocol version'))
91
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
92
"""Whatever request comes in, returns a 403 code"""
94
def parse_request(self):
95
"""Handle a single HTTP request, by replying we cannot handle it"""
96
ignored = TestingHTTPRequestHandler.parse_request(self)
101
class HTTPServerWithSmarts(HttpServer):
102
"""HTTPServerWithSmarts extends the HttpServer with POST methods that will
103
trigger a smart server to execute with a transport rooted at the rootdir of
108
HttpServer.__init__(self, SmartRequestHandler)
111
class SmartRequestHandler(TestingHTTPRequestHandler):
112
"""Extend TestingHTTPRequestHandler to support smart client POSTs."""
115
"""Hand the request off to a smart server instance."""
116
self.send_response(200)
117
self.send_header("Content-type", "application/octet-stream")
118
transport = get_transport(self.server.test_case._home_dir)
119
# TODO: We might like to support streaming responses. 1.0 allows no
120
# Content-length in this case, so for integrity we should perform our
121
# own chunking within the stream.
122
# 1.1 allows chunked responses, and in this case we could chunk using
123
# the HTTP chunking as this will allow HTTP persistence safely, even if
124
# we have to stop early due to error, but we would also have to use the
125
# HTTP trailer facility which may not be widely available.
126
out_buffer = StringIO()
127
smart_protocol_request = smart.SmartServerRequestProtocolOne(
128
transport, out_buffer.write)
129
# if this fails, we should return 400 bad request, but failure is
130
# failure for now - RBC 20060919
131
data_length = int(self.headers['Content-Length'])
132
# Perhaps there should be a SmartServerHTTPMedium that takes care of
133
# feeding the bytes in the http request to the smart_protocol_request,
134
# but for now it's simpler to just feed the bytes directly.
135
smart_protocol_request.accept_bytes(self.rfile.read(data_length))
136
assert smart_protocol_request.next_read_size() == 0, (
137
"not finished reading, but all data sent to protocol.")
138
self.send_header("Content-Length", str(len(out_buffer.getvalue())))
140
self.wfile.write(out_buffer.getvalue())
143
class TestCaseWithWebserver(TestCaseWithTransport):
144
"""A support class that provides readonly urls that are http://.
146
This is done by forcing the readonly server to be an http
147
one. This will currently fail if the primary transport is not
148
backed by regular disk files.
151
super(TestCaseWithWebserver, self).setUp()
152
self.transport_readonly_server = HttpServer