1016
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())
1019
1099
class TestDoCatchRedirections(TestCaseWithRedirectedWebserver):
1020
1100
"""Test transport.do_catching_redirections.