diff -r f16ae8ec5553 Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py Mon May 25 21:25:27 2015 -0500 +++ b/Lib/test/test_urllib.py Tue May 26 03:53:36 2015 +0000 @@ -82,10 +82,11 @@ # buffer to store data for verification in urlopen tests. buf = None - fakesock = FakeSocket(fakedata) def connect(self): - self.sock = self.fakesock + self.sock = FakeSocket(self.fakedata) + type(self).fakesock = self.sock + FakeHTTPConnection.fakedata = fakedata return FakeHTTPConnection @@ -249,6 +250,21 @@ finally: self.unfakehttp() + def test_redirect_no_path(self): + # Issue 14132: Relative redirect strips original path + self.fakehttp(b"HTTP/1.1 302 Found\r\nLocation: ?query\r\n\r\n") + self.addCleanup(self.unfakehttp) + urls = iter(("/path", "/path?query")) + def request(conn, method, url, *pos, **kw): + self.assertEqual(url, next(urls)) + self._connection_class.request(conn, method, url, *pos, **kw) + # Change response for subsequent connection + type(conn).fakedata = b"HTTP/1.1 200 OK\r\n\r\nHello!" + http.client.HTTPConnection.request = request + with urllib.request.urlopen("http://python.org/path") as fp: + pass + self.assertEqual(fp.geturl(), "http://python.org/path?query") + def test_willclose(self): self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!") try: diff -r f16ae8ec5553 Lib/urllib/request.py --- a/Lib/urllib/request.py Mon May 25 21:25:27 2015 -0500 +++ b/Lib/urllib/request.py Tue May 26 03:53:36 2015 +0000 @@ -651,7 +651,7 @@ "%s - Redirection to url '%s' is not allowed" % (msg, newurl), headers, fp) - if not urlparts.path: + if not urlparts.path and urlparts.netloc: urlparts = list(urlparts) urlparts[2] = "/" newurl = urlunparse(urlparts)