/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/HTTPTestUtil.py

Implement a remote Repository.has_revision method.

Show diffs side-by-side

added added

removed removed

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