summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_urllib2.py
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-05-16 07:45:28 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-05-16 07:45:28 (GMT)
commit0b39a556e8aa4d15de21a3305f74e12886d5f31b (patch)
tree6ed9623fc778c14e7ee27dfa9a497b8d09b06363 /Lib/test/test_urllib2.py
parentc944c2dab8036bab6c4d3b2391fa3fbbab8859b5 (diff)
parente6f060903cf2080b6570a87fde5021aa14d05530 (diff)
downloadcpython-0b39a556e8aa4d15de21a3305f74e12886d5f31b.zip
cpython-0b39a556e8aa4d15de21a3305f74e12886d5f31b.tar.gz
cpython-0b39a556e8aa4d15de21a3305f74e12886d5f31b.tar.bz2
Issue #14132, Issue #17214: Merge two redirect handling fixes from 3.5
Diffstat (limited to 'Lib/test/test_urllib2.py')
-rw-r--r--Lib/test/test_urllib2.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index 8716a5e..eda7ccc 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1208,6 +1208,57 @@ class HandlerTests(unittest.TestCase):
fp = o.open('http://www.example.com')
self.assertEqual(fp.geturl(), redirected_url.strip())
+ def test_redirect_no_path(self):
+ # Issue 14132: Relative redirect strips original path
+ real_class = http.client.HTTPConnection
+ response1 = b"HTTP/1.1 302 Found\r\nLocation: ?query\r\n\r\n"
+ http.client.HTTPConnection = test_urllib.fakehttp(response1)
+ self.addCleanup(setattr, http.client, "HTTPConnection", real_class)
+ urls = iter(("/path", "/path?query"))
+ def request(conn, method, url, *pos, **kw):
+ self.assertEqual(url, next(urls))
+ real_class.request(conn, method, url, *pos, **kw)
+ # Change response for subsequent connection
+ conn.__class__.fakedata = b"HTTP/1.1 200 OK\r\n\r\nHello!"
+ http.client.HTTPConnection.request = request
+ fp = urllib.request.urlopen("http://python.org/path")
+ self.assertEqual(fp.geturl(), "http://python.org/path?query")
+
+ def test_redirect_encoding(self):
+ # Some characters in the redirect target may need special handling,
+ # but most ASCII characters should be treated as already encoded
+ class Handler(urllib.request.HTTPHandler):
+ def http_open(self, req):
+ result = self.do_open(self.connection, req)
+ self.last_buf = self.connection.buf
+ # Set up a normal response for the next request
+ self.connection = test_urllib.fakehttp(
+ b'HTTP/1.1 200 OK\r\n'
+ b'Content-Length: 3\r\n'
+ b'\r\n'
+ b'123'
+ )
+ return result
+ handler = Handler()
+ opener = urllib.request.build_opener(handler)
+ tests = (
+ (b'/p\xC3\xA5-dansk/', b'/p%C3%A5-dansk/'),
+ (b'/spaced%20path/', b'/spaced%20path/'),
+ (b'/spaced path/', b'/spaced%20path/'),
+ (b'/?p\xC3\xA5-dansk', b'/?p%C3%A5-dansk'),
+ )
+ for [location, result] in tests:
+ with self.subTest(repr(location)):
+ handler.connection = test_urllib.fakehttp(
+ b'HTTP/1.1 302 Redirect\r\n'
+ b'Location: ' + location + b'\r\n'
+ b'\r\n'
+ )
+ response = opener.open('http://example.com/')
+ expected = b'GET ' + result + b' '
+ request = handler.last_buf
+ self.assertTrue(request.startswith(expected), repr(request))
+
def test_proxy(self):
o = OpenerDirector()
ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))