@@ -462,7 +462,7 @@ def reset(self):
462462 self .requests = []
463463
464464 def http_open (self , req ):
465- import email , http . client , copy
465+ import email , copy
466466 self .requests .append (copy .deepcopy (req ))
467467 if self ._count == 0 :
468468 self ._count = self ._count + 1
@@ -1208,6 +1208,22 @@ def test_redirect_fragment(self):
12081208 fp = o .open ('http://www.example.com' )
12091209 self .assertEqual (fp .geturl (), redirected_url .strip ())
12101210
1211+ def test_redirect_no_path (self ):
1212+ # Issue 14132: Relative redirect strips original path
1213+ real_class = http .client .HTTPConnection
1214+ response1 = b"HTTP/1.1 302 Found\r \n Location: ?query\r \n \r \n "
1215+ http .client .HTTPConnection = test_urllib .fakehttp (response1 )
1216+ self .addCleanup (setattr , http .client , "HTTPConnection" , real_class )
1217+ urls = iter (("/path" , "/path?query" ))
1218+ def request (conn , method , url , * pos , ** kw ):
1219+ self .assertEqual (url , next (urls ))
1220+ real_class .request (conn , method , url , * pos , ** kw )
1221+ # Change response for subsequent connection
1222+ conn .__class__ .fakedata = b"HTTP/1.1 200 OK\r \n \r \n Hello!"
1223+ http .client .HTTPConnection .request = request
1224+ fp = urllib .request .urlopen ("http://python.org/path" )
1225+ self .assertEqual (fp .geturl (), "http://python.org/path?query" )
1226+
12111227 def test_proxy (self ):
12121228 o = OpenerDirector ()
12131229 ph = urllib .request .ProxyHandler (dict (http = "proxy.example.com:3128" ))
0 commit comments