~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
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
16
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
17
from cStringIO import StringIO
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
18
import errno
2004.1.29 by v.ladeuil+lp at free
New tests for http range requests handling.
19
from SimpleHTTPServer import SimpleHTTPRequestHandler
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
20
import socket
1530.1.14 by Robert Collins
Remove duplicate web server from HTTPTestUtil.
21
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
22
from bzrlib.tests import TestCaseWithTransport
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
23
from bzrlib.tests.HttpServer import (
24
    HttpServer,
25
    TestingHTTPRequestHandler,
26
    )
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
27
from bzrlib.transport import (
28
    get_transport,
29
    smart,
30
    )
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
31
32
33
class WallRequestHandler(TestingHTTPRequestHandler):
34
    """Whatever request comes in, close the connection"""
35
36
    def handle_one_request(self):
37
        """Handle a single HTTP request, by abruptly closing the connection"""
38
        self.close_connection = 1
39
40
41
class BadStatusRequestHandler(TestingHTTPRequestHandler):
42
    """Whatever request comes in, returns a bad status"""
43
44
    def parse_request(self):
45
        """Fakes handling a single HTTP request, returns a bad status"""
46
        ignored = TestingHTTPRequestHandler.parse_request(self)
47
        try:
48
            self.send_response(0, "Bad status")
49
            self.end_headers()
50
        except socket.error, e:
51
            if (len(e.args) > 0) and (e.args[0] == errno.EPIPE):
52
                # We don't want to pollute the test reuslts with
53
                # spurious server errors while test succeed. In
54
                # our case, it may occur that the test have
55
                # already read the 'Bad Status' and closed the
56
                # socket while we are still trying to send some
57
                # headers... So the test is ok but if we raise
58
                # the exception the output is dirty. So we don't
59
                # raise, but we close the connection, just to be
60
                # safe :)
61
                self.close_connection = 1
62
                pass
63
            else:
64
                raise
65
        return False
66
67
68
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
69
    """Whatever request comes in, returns am invalid status"""
70
71
    def parse_request(self):
72
        """Fakes handling a single HTTP request, returns a bad status"""
73
        ignored = TestingHTTPRequestHandler.parse_request(self)
74
        self.wfile.write("Invalid status line\r\n")
75
        return False
76
77
78
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
79
    """Whatever request comes in, returns a bad protocol version"""
80
81
    def parse_request(self):
82
        """Fakes handling a single HTTP request, returns a bad status"""
83
        ignored = TestingHTTPRequestHandler.parse_request(self)
84
        # Returns an invalid protocol version, but curl just
85
        # ignores it and those cannot be tested.
86
        self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
87
                                           404,
88
                                           'Look at my protocol version'))
89
        return False
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
90
91
2004.1.27 by v.ladeuil+lp at free
Fix bug #57644 by issuing an explicit error message.
92
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
93
    """Whatever request comes in, returns a 403 code"""
94
95
    def parse_request(self):
96
        """Handle a single HTTP request, by replying we cannot handle it"""
97
        ignored = TestingHTTPRequestHandler.parse_request(self)
98
        self.send_error(403)
99
        return False
100
101
2004.1.28 by v.ladeuil+lp at free
Merge bzr.dev. Including http modifications by "smart" related code
102
class HTTPServerWithSmarts(HttpServer):
103
    """HTTPServerWithSmarts extends the HttpServer with POST methods that will
104
    trigger a smart server to execute with a transport rooted at the rootdir of
105
    the HTTP server.
106
    """
107
108
    def __init__(self):
109
        HttpServer.__init__(self, SmartRequestHandler)
110
111
112
class SmartRequestHandler(TestingHTTPRequestHandler):
113
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
114
115
    def do_POST(self):
116
        """Hand the request off to a smart server instance."""
117
        self.send_response(200)
118
        self.send_header("Content-type", "application/octet-stream")
119
        transport = get_transport(self.server.test_case._home_dir)
120
        # TODO: We might like to support streaming responses.  1.0 allows no
121
        # Content-length in this case, so for integrity we should perform our
122
        # own chunking within the stream.
123
        # 1.1 allows chunked responses, and in this case we could chunk using
124
        # the HTTP chunking as this will allow HTTP persistence safely, even if
125
        # we have to stop early due to error, but we would also have to use the
126
        # HTTP trailer facility which may not be widely available.
127
        out_buffer = StringIO()
128
        smart_protocol_request = smart.SmartServerRequestProtocolOne(
129
                transport, out_buffer.write)
130
        # if this fails, we should return 400 bad request, but failure is
131
        # failure for now - RBC 20060919
132
        data_length = int(self.headers['Content-Length'])
133
        # Perhaps there should be a SmartServerHTTPMedium that takes care of
134
        # feeding the bytes in the http request to the smart_protocol_request,
135
        # but for now it's simpler to just feed the bytes directly.
136
        smart_protocol_request.accept_bytes(self.rfile.read(data_length))
137
        assert smart_protocol_request.next_read_size() == 0, (
138
            "not finished reading, but all data sent to protocol.")
139
        self.send_header("Content-Length", str(len(out_buffer.getvalue())))
140
        self.end_headers()
141
        self.wfile.write(out_buffer.getvalue())
142
143
2004.1.29 by v.ladeuil+lp at free
New tests for http range requests handling.
144
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
145
    """Always reply to range request as if they were single.
146
147
    Don't be explicit about it, just to annoy the clients.
148
    """
149
150
    def get_multiple_ranges(self, file, file_size, ranges):
151
        """Answer as if it was a single range request and ignores the rest"""
152
        (start, end) = ranges[0]
153
        return self.get_single_range(file, file_size, start, end)
154
155
156
class NoRangeRequestHandler(TestingHTTPRequestHandler):
157
    """Ignore range requests without notice"""
158
159
    # Just bypass the range handling done by TestingHTTPRequestHandler
160
    do_GET = SimpleHTTPRequestHandler.do_GET
161
162
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
163
class TestCaseWithWebserver(TestCaseWithTransport):
164
    """A support class that provides readonly urls that are http://.
165
2004.3.3 by vila
Better (but still incomplete) design for bogus servers.
166
    This is done by forcing the readonly server to be an http
167
    one. This will currently fail if the primary transport is not
168
    backed by regular disk files.
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
169
    """
170
    def setUp(self):
1530.1.14 by Robert Collins
Remove duplicate web server from HTTPTestUtil.
171
        super(TestCaseWithWebserver, self).setUp()
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
172
        self.transport_readonly_server = HttpServer