diff --git a/core/lib/src/local/asynchronous/response.rs b/core/lib/src/local/asynchronous/response.rs index 9267064d..cabbdccc 100644 --- a/core/lib/src/local/asynchronous/response.rs +++ b/core/lib/src/local/asynchronous/response.rs @@ -167,8 +167,8 @@ impl LocalResponse<'_> { }); loop { - let mut buf = Vec::with_capacity(1024); // TODO: Try to fill as much as the buffer before send it off? + let mut buf = Vec::with_capacity(1024); match self.read_buf(&mut buf).await { Ok(n) if n == 0 => break, Ok(_) => tx.send(Ok(buf)).await.ok()?, @@ -179,6 +179,9 @@ impl LocalResponse<'_> { } } + // NOTE: We _must_ drop tx now to prevent a deadlock! + drop(tx); + reader.await.ok() } diff --git a/core/lib/tests/local-client-json.rs b/core/lib/tests/local-client-json.rs new file mode 100644 index 00000000..fae143e8 --- /dev/null +++ b/core/lib/tests/local-client-json.rs @@ -0,0 +1,24 @@ +#![cfg(feature = "json")] + +#[macro_use] extern crate rocket; + +use rocket::serde::json::Json; + +#[get("/int")] fn int() -> Json { Json(5) } +#[get("/nil")] fn nil() -> Json<()> { Json(()) } + +#[async_test] +async fn async_json_works() { + use rocket::local::asynchronous::Client; + + let client = Client::debug_with(routes![int, nil]).await.unwrap(); + + let int0 = client.get("/int").dispatch().await.into_json::().await; + let int1 = client.get("/int").dispatch().await.into_json::().await; + + assert_eq!(int0, Some(5)); + assert_eq!(int1, Some(5)); + + let nil0 = client.get("/nil").dispatch().await.into_json::<()>().await; + assert_eq!(nil0, Some(())); +}