~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/HttpServer.py

  • Committer: Robert Collins
  • Date: 2007-04-23 02:29:35 UTC
  • mfrom: (2441 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2442.
  • Revision ID: robertc@robertcollins.net-20070423022935-9hhongamvk6bfdso
Resolve conflicts with bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import urlparse
30
30
 
31
31
from bzrlib.transport import Server
 
32
from bzrlib.transport.local import LocalURLServer
32
33
 
33
34
 
34
35
class WebserverNotAvailable(Exception):
43
44
class TestingHTTPRequestHandler(SimpleHTTPRequestHandler):
44
45
 
45
46
    def log_message(self, format, *args):
46
 
        self.server.test_case.log('webserver - %s - - [%s] %s "%s" "%s"',
47
 
                                  self.address_string(),
48
 
                                  self.log_date_time_string(),
49
 
                                  format % args,
50
 
                                  self.headers.get('referer', '-'),
51
 
                                  self.headers.get('user-agent', '-'))
 
47
        tcs = self.server.test_case_server
 
48
        tcs.log('webserver - %s - - [%s] %s "%s" "%s"',
 
49
                self.address_string(),
 
50
                self.log_date_time_string(),
 
51
                format % args,
 
52
                self.headers.get('referer', '-'),
 
53
                self.headers.get('user-agent', '-'))
52
54
 
53
55
    def handle_one_request(self):
54
56
        """Handle a single HTTP request.
153
155
            self.end_headers()
154
156
            self.send_range_content(file, start, end - start + 1)
155
157
            self.wfile.write("--%s\r\n" % boundary)
156
 
            pass
157
158
 
158
159
    def do_GET(self):
159
160
        """Serve a GET request.
248
249
 
249
250
 
250
251
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
251
 
    def __init__(self, server_address, RequestHandlerClass, test_case):
 
252
 
 
253
    def __init__(self, server_address, RequestHandlerClass,
 
254
                 test_case_server):
252
255
        BaseHTTPServer.HTTPServer.__init__(self, server_address,
253
 
                                                RequestHandlerClass)
254
 
        self.test_case = test_case
 
256
                                           RequestHandlerClass)
 
257
        # test_case_server can be used to communicate between the
 
258
        # tests and the server (or the request handler and the
 
259
        # server), allowing dynamic behaviors to be defined from
 
260
        # the tests cases.
 
261
        self.test_case_server = test_case_server
255
262
 
256
263
 
257
264
class HttpServer(Server):
267
274
    def __init__(self, request_handler=TestingHTTPRequestHandler):
268
275
        Server.__init__(self)
269
276
        self.request_handler = request_handler
 
277
        self.host = 'localhost'
 
278
        self.port = 0
 
279
        self._httpd = None
270
280
 
271
281
    def _get_httpd(self):
272
 
        return TestingHTTPServer(('localhost', 0),
273
 
                                  self.request_handler,
274
 
                                  self)
 
282
        if self._httpd is None:
 
283
            self._httpd = TestingHTTPServer((self.host, self.port),
 
284
                                            self.request_handler,
 
285
                                            self)
 
286
            host, self.port = self._httpd.socket.getsockname()
 
287
        return self._httpd
275
288
 
276
289
    def _http_start(self):
277
 
        httpd = None
278
290
        httpd = self._get_httpd()
279
 
        host, self.port = httpd.socket.getsockname()
280
 
        self._http_base_url = '%s://localhost:%s/' % (self._url_protocol,
281
 
                                                      self.port)
 
291
        self._http_base_url = '%s://%s:%s/' % (self._url_protocol,
 
292
                                               self.host,
 
293
                                               self.port)
282
294
        self._http_starting.release()
283
295
        httpd.socket.settimeout(0.1)
284
296
 
304
316
        """Capture Server log output."""
305
317
        self.logs.append(format % args)
306
318
 
307
 
    def setUp(self):
308
 
        """See bzrlib.transport.Server.setUp."""
 
319
    def setUp(self, backing_transport_server=None):
 
320
        """See bzrlib.transport.Server.setUp.
 
321
        
 
322
        :param backing_transport_server: The transport that requests over this
 
323
            protocol should be forwarded to. Note that this is currently not
 
324
            supported for HTTP.
 
325
        """
 
326
        # XXX: TODO: make the server back onto vfs_server rather than local
 
327
        # disk.
 
328
        assert backing_transport_server is None or \
 
329
            isinstance(backing_transport_server, LocalURLServer), \
 
330
            "HTTPServer currently assumes local transport, got %s" % \
 
331
            backing_transport_server
309
332
        self._home_dir = os.getcwdu()
310
333
        self._local_path_parts = self._home_dir.split(os.path.sep)
311
334
        self._http_starting = threading.Lock()