~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/HTTPTestUtil.py

  • Committer: Vincent Ladeuil
  • Date: 2007-03-14 16:39:24 UTC
  • mto: (2323.7.1 redirection)
  • mto: This revision was merged to the branch mainline in revision 2390.
  • Revision ID: v.ladeuil+lp@free.fr-20070314163924-5xoey9mkxe6qaptd
Test the http redirection at the request level even if it's not
used anymore, so far.

* bzrlib/transport/http/_urllib2_wrappers.py
(HTTPRedirectHandler.http_error_302): Renamed from http_error_30x.
(HTTPRedirectHandler.http_error_301): Deleted. Finally, that's the
easiest way to fix bug #88780 :) In retrospect, that code was a
first attempt to handle this whole redirection handling by
providing the final target to the first request. This is not
needed anymore.

* bzrlib/tests/test_http.py:
(RedirectedRequest, TestHTTPSilentRedirections_urllib): Test the
_urllib2_wrappers redirection at the request level.

* bzrlib/tests/HTTPTestUtil.py:
(RedirectRequestHandler.parse_request): Delegate the redirection
handling to the test_case_server.
(HTTPServerRedirecting.is_redirected): Handle redirections by
searching a matching directive.
(HTTPServerRedirecting.redirect_to): Redefined to use the nre
redirections definitions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from cStringIO import StringIO
18
18
import errno
19
19
from SimpleHTTPServer import SimpleHTTPRequestHandler
 
20
import re
20
21
import socket
21
22
import urlparse
22
23
 
232
233
        """Redirect a single HTTP request to another host"""
233
234
        valid = TestingHTTPRequestHandler.parse_request(self)
234
235
        if valid:
235
 
            rhost, rport = self.server.test_case_server.redirected_to_address()
236
 
            if rhost is not None and rport is not None:
237
 
                # Just redirect permanently
238
 
                self.send_response(301)
239
 
                target = 'http://%s:%s%s' % (rhost, rport, self.path)
 
236
            tcs = self.server.test_case_server
 
237
            code, target = tcs.is_redirected(self.path)
 
238
            if code is not None and target is not None:
 
239
                # Redirect as instructed
 
240
                self.send_response(code)
240
241
                self.send_header('Location', target)
241
242
                self.end_headers()
242
243
                return False # The job is done
 
244
            else:
 
245
                # We leave the parent class serve the request
 
246
                pass
243
247
        return valid
244
248
 
245
249
 
248
252
 
249
253
    def __init__(self, request_handler=RedirectRequestHandler):
250
254
        HttpServer.__init__(self, request_handler)
251
 
        self._redirect_to_host = None
252
 
        self._redirect_to_port = None
253
 
 
254
 
    def redirect_to(self, redir_host, redir_port):
255
 
        self._redirect_to_host = redir_host
256
 
        self._redirect_to_port = redir_port
257
 
 
258
 
    def redirected_to_address(self):
259
 
        return self._redirect_to_host, self._redirect_to_port
 
255
        # redirections is a list of tuples (source, target, code)
 
256
        # - source is a regexp for the paths requested
 
257
        # - target is a replacement for re.sub describing where
 
258
        #   the request will be redirected
 
259
        # - code is the http error code associated to the
 
260
        #   redirection (301 permanent, 302 temporarry, etc
 
261
        self.redirections = []
 
262
 
 
263
    def redirect_to(self, host, port):
 
264
        """Redirect all requests to a specific host:port"""
 
265
        self.redirections = [('(.*)',
 
266
                              r'http://%s:%s\1' % (host, port) ,
 
267
                              301)]
 
268
 
 
269
    def is_redirected(self, path):
 
270
        """Is the path redirected by this server.
 
271
 
 
272
        :param path: the requested relative path
 
273
 
 
274
        :returns: a tuple (code, target) if a matching
 
275
             redirection is found, (None, None) otherwise.
 
276
        """
 
277
        code = None
 
278
        target = None
 
279
        for (rsource, rtarget, rcode) in self.redirections:
 
280
            target, match = re.subn(rsource, rtarget, path)
 
281
            if match:
 
282
                code = rcode
 
283
                break # The first match wins
 
284
            else:
 
285
                target = None
 
286
        return code, target
260
287
 
261
288
 
262
289
class TestCaseWithRedirectedWebserver(TestCaseWithTwoWebservers):