Skip to content

Commit d73d961

Browse files
authored
fix blocking client to use request timeout for response body (#1395)
1 parent 8b37ae4 commit d73d961

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/blocking/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ impl ClientHandle {
10391039
Ok(Err(err)) => Err(err.with_url(url)),
10401040
Ok(Ok(res)) => Ok(Response::new(
10411041
res,
1042-
self.timeout.0,
1042+
timeout,
10431043
KeepCoreThreadAlive(Some(self.inner.clone())),
10441044
)),
10451045
Err(wait::Waited::TimedOut(e)) => Err(crate::error::request(e).with_url(url)),

tests/timeouts.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,43 @@ fn timeout_blocking_request() {
174174
assert_eq!(err.url().map(|u| u.as_str()), Some(url.as_str()));
175175
}
176176

177+
#[cfg(feature = "blocking")]
178+
#[test]
179+
fn blocking_request_timeout_body() {
180+
let _ = env_logger::try_init();
181+
182+
let client = reqwest::blocking::Client::builder()
183+
// this should be overridden
184+
.connect_timeout(Duration::from_millis(200))
185+
// this should be overridden
186+
.timeout(Duration::from_millis(200))
187+
.build()
188+
.unwrap();
189+
190+
let server = server::http(move |_req| {
191+
async {
192+
// immediate response, but delayed body
193+
let body = hyper::Body::wrap_stream(futures_util::stream::once(async {
194+
tokio::time::sleep(Duration::from_secs(1)).await;
195+
Ok::<_, std::convert::Infallible>("Hello")
196+
}));
197+
198+
http::Response::new(body)
199+
}
200+
});
201+
202+
let url = format!("http://{}/closes", server.addr());
203+
let res = client
204+
.get(&url)
205+
// longer than client timeout
206+
.timeout(Duration::from_secs(5))
207+
.send()
208+
.expect("get response");
209+
210+
let text = res.text().unwrap();
211+
assert_eq!(text, "Hello");
212+
}
213+
177214
#[cfg(feature = "blocking")]
178215
#[test]
179216
fn write_timeout_large_body() {

0 commit comments

Comments
 (0)