Apply deadline across redirects. (#313)

Previously, each redirect could take timeout time, so a series of slow
redirects could run for longer than expected, or indefinitely.
This commit is contained in:
Jacob Hoffman-Andrews
2021-02-07 12:29:35 -08:00
committed by GitHub
parent d627ef9704
commit b246f0a9d2
3 changed files with 53 additions and 11 deletions

View File

@@ -1,6 +1,8 @@
use std::{
io::{self, Write},
net::TcpStream,
thread,
time::Duration,
};
use testserver::{self, TestServer};
@@ -158,6 +160,31 @@ fn redirect_308() {
assert_eq!(resp.get_url(), "test://host/valid_response");
}
#[test]
fn redirects_hit_timeout() {
// A chain of 2 redirects and an OK, each of which takes 50ms; we set a
// timeout of 100ms and should error.
test::set_handler("/redirect_sleep1", |_| {
thread::sleep(Duration::from_millis(50));
test::make_response(302, "Go here", vec!["Location: /redirect_sleep2"], vec![])
});
test::set_handler("/redirect_sleep2", |_| {
thread::sleep(Duration::from_millis(50));
test::make_response(302, "Go here", vec!["Location: /ok"], vec![])
});
test::set_handler("/ok", |_| {
thread::sleep(Duration::from_millis(50));
test::make_response(200, "Go here", vec![], vec![])
});
let req = crate::builder().timeout(Duration::from_millis(100)).build();
let result = req.get("test://host/redirect_sleep1").call();
assert!(
matches!(result, Err(Error::Transport(_))),
"expected Transport error, got {:?}",
result
);
}
#[test]
fn too_many_redirects() {
for i in 0..10_000 {