/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

  • Committer: v.ladeuil+lp at free
  • Date: 2006-10-13 10:50:33 UTC
  • mfrom: (2077 +trunk)
  • mto: (2145.1.1 keepalive)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: v.ladeuil+lp@free.fr-20061013105033-7e091ff8dcc0ed0c
Merge bzr.dev. Including http modifications by "smart" related code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by 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
import socket
 
20
 
 
21
from bzrlib.tests import TestCaseWithTransport
 
22
from bzrlib.tests.HttpServer import (
 
23
    HttpServer,
 
24
    TestingHTTPRequestHandler,
 
25
    )
 
26
from bzrlib.transport import (
 
27
    get_transport,
 
28
    smart,
 
29
    )
 
30
 
 
31
 
 
32
class WallRequestHandler(TestingHTTPRequestHandler):
 
33
    """Whatever request comes in, close the connection"""
 
34
 
 
35
    def handle_one_request(self):
 
36
        """Handle a single HTTP request, by abruptly closing the connection"""
 
37
        self.close_connection = 1
 
38
 
 
39
 
 
40
class BadStatusRequestHandler(TestingHTTPRequestHandler):
 
41
    """Whatever request comes in, returns a bad status"""
 
42
 
 
43
    def parse_request(self):
 
44
        """Fakes handling a single HTTP request, returns a bad status"""
 
45
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
46
        try:
 
47
            self.send_response(0, "Bad status")
 
48
            self.end_headers()
 
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
 
59
                # safe :)
 
60
                self.close_connection = 1
 
61
                pass
 
62
            else:
 
63
                raise
 
64
        return False
 
65
 
 
66
 
 
67
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
 
68
    """Whatever request comes in, returns am invalid status"""
 
69
 
 
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")
 
74
        return False
 
75
 
 
76
 
 
77
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
 
78
    """Whatever request comes in, returns a bad protocol version"""
 
79
 
 
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',
 
86
                                           404,
 
87
                                           'Look at my protocol version'))
 
88
        return False
 
89
 
 
90
 
 
91
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
 
92
    """Whatever request comes in, returns a 403 code"""
 
93
 
 
94
    def parse_request(self):
 
95
        """Handle a single HTTP request, by replying we cannot handle it"""
 
96
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
97
        self.send_error(403)
 
98
        return False
 
99
 
 
100
 
 
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
 
104
    the HTTP server.
 
105
    """
 
106
 
 
107
    def __init__(self):
 
108
        HttpServer.__init__(self, SmartRequestHandler)
 
109
 
 
110
 
 
111
class SmartRequestHandler(TestingHTTPRequestHandler):
 
112
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
 
113
 
 
114
    def do_POST(self):
 
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())))
 
139
        self.end_headers()
 
140
        self.wfile.write(out_buffer.getvalue())
 
141
 
 
142
 
 
143
class TestCaseWithWebserver(TestCaseWithTransport):
 
144
    """A support class that provides readonly urls that are http://.
 
145
 
 
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.
 
149
    """
 
150
    def setUp(self):
 
151
        super(TestCaseWithWebserver, self).setUp()
 
152
        self.transport_readonly_server = HttpServer