@@ -1208,6 +1208,57 @@ 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+
1227+ def test_redirect_encoding (self ):
1228+ # Some characters in the redirect target may need special handling,
1229+ # but most ASCII characters should be treated as already encoded
1230+ class Handler (urllib .request .HTTPHandler ):
1231+ def http_open (self , req ):
1232+ result = self .do_open (self .connection , req )
1233+ self .last_buf = self .connection .buf
1234+ # Set up a normal response for the next request
1235+ self .connection = test_urllib .fakehttp (
1236+ b'HTTP/1.1 200 OK\r \n '
1237+ b'Content-Length: 3\r \n '
1238+ b'\r \n '
1239+ b'123'
1240+ )
1241+ return result
1242+ handler = Handler ()
1243+ opener = urllib .request .build_opener (handler )
1244+ tests = (
1245+ (b'/p\xC3 \xA5 -dansk/' , b'/p%C3%A5-dansk/' ),
1246+ (b'/spaced%20path/' , b'/spaced%20path/' ),
1247+ (b'/spaced path/' , b'/spaced%20path/' ),
1248+ (b'/?p\xC3 \xA5 -dansk' , b'/?p%C3%A5-dansk' ),
1249+ )
1250+ for [location , result ] in tests :
1251+ with self .subTest (repr (location )):
1252+ handler .connection = test_urllib .fakehttp (
1253+ b'HTTP/1.1 302 Redirect\r \n '
1254+ b'Location: ' + location + b'\r \n '
1255+ b'\r \n '
1256+ )
1257+ response = opener .open ('http://example.com/' )
1258+ expected = b'GET ' + result + b' '
1259+ request = handler .last_buf
1260+ self .assertTrue (request .startswith (expected ), repr (request ))
1261+
12111262 def test_proxy (self ):
12121263 o = OpenerDirector ()
12131264 ph = urllib .request .ProxyHandler (dict (http = "proxy.example.com:3128" ))
0 commit comments