Fix 307/308 redirects (again)

- Add unit test
- Use the original method instead of hard-coding GET
This commit is contained in:
Joshua Nelson
2020-12-05 23:30:26 -05:00
parent 498b7137c2
commit aeeff40c95
2 changed files with 17 additions and 1 deletions

View File

@@ -127,3 +127,17 @@ fn redirect_post() {
assert!(resp.has("x-foo")); assert!(resp.has("x-foo"));
assert_eq!(resp.header("x-foo").unwrap(), "bar"); assert_eq!(resp.header("x-foo").unwrap(), "bar");
} }
#[test]
fn redirect_308() {
test::set_handler("/redirect_get3", |_| {
test::make_response(308, "Go here", vec!["Location: /valid_response"], vec![])
});
test::set_handler("/valid_response", |unit| {
assert_eq!(unit.method, "GET");
test::make_response(200, "OK", vec![], vec![])
});
let resp = get("test://host/redirect_get3").call().unwrap();
assert_eq!(resp.status(), 200);
assert_eq!(resp.get_url(), "test://host/valid_response");
}

View File

@@ -271,7 +271,9 @@ pub(crate) fn connect(
307 | 308 if ["GET", "HEAD", "OPTIONS", "TRACE"].contains(&method.as_str()) => { 307 | 308 if ["GET", "HEAD", "OPTIONS", "TRACE"].contains(&method.as_str()) => {
let empty = Payload::Empty.into_read(); let empty = Payload::Empty.into_read();
debug!("redirect {} {} -> {}", resp.status(), url, new_url); debug!("redirect {} {} -> {}", resp.status(), url, new_url);
return connect(unit, use_pooled, empty, Some(Arc::new(resp))); // recreate the unit to get a new hostname and cookies for the new host.
let new_unit = Unit::new(&unit.agent, &unit.method, &new_url, &unit.headers, &empty);
return connect(new_unit, use_pooled, empty, Some(Arc::new(resp)));
} }
_ => (), _ => (),
}; };