Use iteration instead of recursion for connect (#291)

This allows handling larger redirect chains.

Fixes #290
This commit is contained in:
Joshua Nelson
2021-01-05 16:55:26 -05:00
committed by GitHub
parent f0245aad23
commit d0bd2d5ea9
5 changed files with 137 additions and 127 deletions

View File

@@ -35,6 +35,22 @@ fn redirect_many() {
.get("test://host/redirect_many1")
.call();
assert!(matches!(result, Err(e) if e.kind() == ErrorKind::TooManyRedirects));
test::set_handler("/redirect_many1", |_| {
test::make_response(302, "Go here", vec!["Location: /redirect_many2"], vec![])
});
test::set_handler("/redirect_many2", |_| {
test::make_response(302, "Go here", vec!["Location: /redirect_many3"], vec![])
});
test::set_handler("/redirect_many3", |_| {
test::make_response(302, "Go here", vec!["Location: /redirect_many4"], vec![])
});
let result = builder()
.redirects(2)
.build()
.get("test://host/redirect_many1")
.call();
assert!(matches!(result, Err(e) if e.kind() == ErrorKind::TooManyRedirects));
}
#[test]
@@ -141,3 +157,22 @@ fn redirect_308() {
assert_eq!(resp.status(), 200);
assert_eq!(resp.get_url(), "test://host/valid_response");
}
#[test]
fn too_many_redirects() {
for i in 0..10_000 {
test::set_handler(&format!("/malicious_redirect_{}", i), move |_| {
let location = format!("Location: /malicious_redirect_{}", i + 1);
test::make_response(302, "Go here", vec![&location], vec![])
});
}
test::set_handler("/malicious_redirect_10000", |unit| {
assert_eq!(unit.method, "GET");
test::make_response(200, "OK", vec![], vec![])
});
let req = crate::builder().redirects(10001).build();
let resp = req.get("test://host/malicious_redirect_0").call().unwrap();
assert_eq!(resp.get_url(), "test://host/malicious_redirect_10000");
}