~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/http_utils.py

  • Committer: Vincent Ladeuil
  • Date: 2007-12-21 09:44:07 UTC
  • mto: (3146.3.1 179368) (3156.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 3158.
  • Revision ID: v.ladeuil+lp@free.fr-20071221094407-hkt3d5nt1lfzt8im
Fix more imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from cStringIO import StringIO
18
18
import errno
19
19
import md5
20
 
from SimpleHTTPServer import SimpleHTTPRequestHandler
21
20
import re
22
21
import sha
23
22
import socket
26
25
import urllib2
27
26
import urlparse
28
27
 
 
28
 
 
29
from bzrlib import (
 
30
    tests,
 
31
    transport,
 
32
    )
29
33
from bzrlib.smart import protocol
30
 
from bzrlib.tests import TestCaseWithTransport
31
 
from bzrlib.tests.http_server import (
32
 
    HttpServer,
33
 
    TestingHTTPRequestHandler,
34
 
    )
35
 
from bzrlib.transport import (
36
 
    get_transport,
37
 
    )
38
 
 
39
 
 
40
 
class HTTPServerWithSmarts(HttpServer):
 
34
from bzrlib.tests import http_server
 
35
 
 
36
 
 
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
43
40
    the HTTP server.
44
41
    """
45
42
 
46
43
    def __init__(self):
47
 
        HttpServer.__init__(self, SmartRequestHandler)
48
 
 
49
 
 
50
 
class SmartRequestHandler(TestingHTTPRequestHandler):
 
44
        http_server.HttpServer.__init__(self, SmartRequestHandler)
 
45
 
 
46
 
 
47
class SmartRequestHandler(http_server.TestingHTTPRequestHandler):
51
48
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
52
49
 
53
50
    def do_POST(self):
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)
 
64
                t, 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())
80
77
 
81
78
 
82
 
class TestCaseWithWebserver(TestCaseWithTransport):
 
79
class TestCaseWithWebserver(tests.TestCaseWithTransport):
83
80
    """A support class that provides readonly urls that are http://.
84
81
 
85
82
    This is done by forcing the readonly server to be an http
88
85
    """
89
86
    def setUp(self):
90
87
        super(TestCaseWithWebserver, self).setUp()
91
 
        self.transport_readonly_server = HttpServer
 
88
        self.transport_readonly_server = http_server.HttpServer
92
89
 
93
90
 
94
91
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
99
96
    """
100
97
    def setUp(self):
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
104
101
 
105
102
    def create_transport_secondary_server(self):
118
115
        return self.__secondary_server
119
116
 
120
117
 
121
 
class ProxyServer(HttpServer):
 
118
class ProxyServer(http_server.HttpServer):
122
119
    """A proxy test server for http transports."""
123
120
 
124
121
    proxy_requests = True
125
122
 
126
123
 
127
 
class RedirectRequestHandler(TestingHTTPRequestHandler):
 
124
class RedirectRequestHandler(http_server.TestingHTTPRequestHandler):
128
125
    """Redirect all request to the specified server"""
129
126
 
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)
133
130
        if valid:
134
131
            tcs = self.server.test_case_server
135
132
            code, target = tcs.is_redirected(self.path)
145
142
        return valid
146
143
 
147
144
 
148
 
class HTTPServerRedirecting(HttpServer):
 
145
class HTTPServerRedirecting(http_server.HttpServer):
149
146
    """An HttpServer redirecting to another server """
150
147
 
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()
208
205
 
209
206
 
210
 
class AuthRequestHandler(TestingHTTPRequestHandler):
 
207
class AuthRequestHandler(http_server.TestingHTTPRequestHandler):
211
208
    """Requires an authentication to process requests.
212
209
 
213
210
    This is intended to be used with a server that always and
222
219
 
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)
226
223
        else:
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)
294
291
 
295
292
 
296
 
class AuthServer(HttpServer):
 
293
class AuthServer(http_server.HttpServer):
297
294
    """Extends HttpServer with a dictionary of passwords.
298
295
 
299
296
    This is used as a base class for various schemes which should
311
308
    auth_realm = "Thou should not pass"
312
309
 
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