Test case for preserving io::Error timed out
This commit is contained in:
@@ -8,9 +8,8 @@ use super::super::*;
|
|||||||
|
|
||||||
// Send an HTTP response on the TcpStream at a rate of two bytes every 10
|
// Send an HTTP response on the TcpStream at a rate of two bytes every 10
|
||||||
// milliseconds, for a total of 600 bytes.
|
// milliseconds, for a total of 600 bytes.
|
||||||
fn dribble_body_respond(mut stream: TcpStream) -> io::Result<()> {
|
fn dribble_body_respond(mut stream: TcpStream, contents: &[u8]) -> io::Result<()> {
|
||||||
read_headers(&stream);
|
read_headers(&stream);
|
||||||
let contents = [b'a'; 300];
|
|
||||||
let headers = format!(
|
let headers = format!(
|
||||||
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n",
|
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n",
|
||||||
contents.len() * 2
|
contents.len() * 2
|
||||||
@@ -43,7 +42,7 @@ fn get_and_expect_timeout(url: String) {
|
|||||||
#[test]
|
#[test]
|
||||||
fn overall_timeout_during_body() {
|
fn overall_timeout_during_body() {
|
||||||
// Start a test server on an available port, that dribbles out a response at 1 write per 10ms.
|
// Start a test server on an available port, that dribbles out a response at 1 write per 10ms.
|
||||||
let server = TestServer::new(dribble_body_respond);
|
let server = TestServer::new(|stream| dribble_body_respond(stream, &[b'a'; 300]));
|
||||||
let url = format!("http://localhost:{}/", server.port);
|
let url = format!("http://localhost:{}/", server.port);
|
||||||
get_and_expect_timeout(url);
|
get_and_expect_timeout(url);
|
||||||
}
|
}
|
||||||
@@ -67,3 +66,33 @@ fn overall_timeout_during_headers() {
|
|||||||
let url = format!("http://localhost:{}/", server.port);
|
let url = format!("http://localhost:{}/", server.port);
|
||||||
get_and_expect_timeout(url);
|
get_and_expect_timeout(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "json")]
|
||||||
|
fn overall_timeout_reading_json() {
|
||||||
|
// Start a test server on an available port, that dribbles out a response at 1 write per 10ms.
|
||||||
|
let server = TestServer::new(|stream| {
|
||||||
|
dribble_body_respond(
|
||||||
|
stream,
|
||||||
|
b"[1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||||
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||||
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||||
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||||
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]",
|
||||||
|
)
|
||||||
|
});
|
||||||
|
let url = format!("http://localhost:{}/", server.port);
|
||||||
|
|
||||||
|
let agent = Agent::default().build();
|
||||||
|
let timeout = Duration::from_millis(500);
|
||||||
|
let resp = agent.get(&url).timeout(timeout).call();
|
||||||
|
|
||||||
|
match resp.into_json() {
|
||||||
|
Ok(_) => Err("successful response".to_string()),
|
||||||
|
Err(e) => match e.kind() {
|
||||||
|
io::ErrorKind::TimedOut => Ok(()),
|
||||||
|
_ => Err(format!("Unexpected io::ErrorKind: {:?}", e)),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.expect("expected timeout but got something else");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user