947
954
TestCaseWithWebserver):
948
955
"""Test the Range header in GET methods for pycurl implementation"""
958
class TestHTTPRedirections(object):
959
"""Test redirection between http servers.
961
This MUST be used by daughter classes that also inherit from
962
TestCaseWithRedirectedWebserver.
964
We can't inherit directly from TestCaseWithTwoWebservers or the
965
test framework will try to create an instance which cannot
966
run, its implementation being incomplete.
969
def create_transport_secondary_server(self):
970
"""Create the secondary server redirecting to the primary server"""
971
new = self.get_readonly_server()
973
redirecting = HTTPServerRedirecting()
974
redirecting.redirect_to(new.host, new.port)
978
super(TestHTTPRedirections, self).setUp()
979
self.build_tree_contents([('a', '0123456789'),
981
'# Bazaar revision bundle v0.9\n#\n')
984
self.old_transport = self._transport(self.old_server.get_url())
986
def test_redirected(self):
987
self.assertRaises(errors.RedirectRequested, self.old_transport.get, 'a')
988
t = self._transport(self.new_server.get_url())
989
self.assertEqual('0123456789', t.get('a').read())
991
def test_read_redirected_bundle_from_url(self):
992
from bzrlib.bundle import read_bundle_from_url
993
url = self.old_transport.abspath('bundle')
994
bundle = read_bundle_from_url(url)
995
# If read_bundle_from_url was successful we get an empty bundle
996
self.assertEqual([], bundle.revisions)
999
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1000
TestCaseWithRedirectedWebserver):
1001
"""Tests redirections for urllib implementation"""
1003
_transport = HttpTransport_urllib
1007
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1008
TestHTTPRedirections,
1009
TestCaseWithRedirectedWebserver):
1010
"""Tests redirections for pycurl implementation"""
1013
class RedirectedRequest(_urllib2_wrappers.Request):
1014
"""Request following redirections"""
1016
init_orig = _urllib2_wrappers.Request.__init__
1018
def __init__(self, method, url, *args, **kwargs):
1019
RedirectedRequest.init_orig(self, method, url, args, kwargs)
1020
self.follow_redirections = True
1023
class TestHTTPSilentRedirections_urllib(TestCaseWithRedirectedWebserver):
1024
"""Test redirections provided by urllib.
1026
http implementations do not redirect silently anymore (they
1027
do not redirect at all in fact). The mechanism is still in
1028
place at the _urllib2_wrappers.Request level and these tests
1031
For the pycurl implementation
1032
the redirection have been deleted as we may deprecate pycurl
1033
and I have no place to keep a working implementation.
1037
_transport = HttpTransport_urllib
1040
super(TestHTTPSilentRedirections_urllib, self).setUp()
1041
self.setup_redirected_request()
1042
self.addCleanup(self.cleanup_redirected_request)
1043
self.build_tree_contents([('a','a'),
1045
('1/a', 'redirected once'),
1047
('2/a', 'redirected twice'),
1049
('3/a', 'redirected thrice'),
1051
('4/a', 'redirected 4 times'),
1053
('5/a', 'redirected 5 times'),
1056
self.old_transport = self._transport(self.old_server.get_url())
1058
def setup_redirected_request(self):
1059
self.original_class = _urllib2_wrappers.Request
1060
_urllib2_wrappers.Request = RedirectedRequest
1062
def cleanup_redirected_request(self):
1063
_urllib2_wrappers.Request = self.original_class
1065
def create_transport_secondary_server(self):
1066
"""Create the secondary server, redirections are defined in the tests"""
1067
return HTTPServerRedirecting()
1069
def test_one_redirection(self):
1070
t = self.old_transport
1072
req = RedirectedRequest('GET', t.abspath('a'))
1073
req.follow_redirections = True
1074
new_prefix = 'http://%s:%s' % (self.new_server.host,
1075
self.new_server.port)
1076
self.old_server.redirections = \
1077
[('(.*)', r'%s/1\1' % (new_prefix), 301),]
1078
self.assertEquals('redirected once',t._perform(req).read())
1080
def test_five_redirections(self):
1081
t = self.old_transport
1083
req = RedirectedRequest('GET', t.abspath('a'))
1084
req.follow_redirections = True
1085
old_prefix = 'http://%s:%s' % (self.old_server.host,
1086
self.old_server.port)
1087
new_prefix = 'http://%s:%s' % (self.new_server.host,
1088
self.new_server.port)
1089
self.old_server.redirections = \
1090
[('/1(.*)', r'%s/2\1' % (old_prefix), 302),
1091
('/2(.*)', r'%s/3\1' % (old_prefix), 303),
1092
('/3(.*)', r'%s/4\1' % (old_prefix), 307),
1093
('/4(.*)', r'%s/5\1' % (new_prefix), 301),
1094
('(/[^/]+)', r'%s/1\1' % (old_prefix), 301),
1096
self.assertEquals('redirected 5 times',t._perform(req).read())
1099
class TestDoCatchRedirections(TestCaseWithRedirectedWebserver):
1100
"""Test transport.do_catching_redirections.
1102
We arbitrarily choose to use urllib transports
1105
_transport = HttpTransport_urllib
1108
super(TestDoCatchRedirections, self).setUp()
1109
self.build_tree_contents([('a', '0123456789'),],)
1111
self.old_transport = self._transport(self.old_server.get_url())
1113
def get_a(self, transport):
1114
return transport.get('a')
1116
def test_no_redirection(self):
1117
t = self._transport(self.new_server.get_url())
1119
# We use None for redirected so that we fail if redirected
1120
self.assertEquals('0123456789',
1121
do_catching_redirections(self.get_a, t, None).read())
1123
def test_one_redirection(self):
1124
self.redirections = 0
1126
def redirected(transport, exception, redirection_notice):
1127
self.redirections += 1
1128
dir, file = urlutils.split(exception.target)
1129
return self._transport(dir)
1131
self.assertEquals('0123456789',
1132
do_catching_redirections(self.get_a,
1136
self.assertEquals(1, self.redirections)
1138
def test_redirection_loop(self):
1140
def redirected(transport, exception, redirection_notice):
1141
# By using the redirected url as a base dir for the
1142
# *old* transport, we create a loop: a => a/a =>
1144
return self.old_transport.clone(exception.target)
1146
self.assertRaises(errors.TooManyRedirections, do_catching_redirections,
1147
self.get_a, self.old_transport, redirected)