Skip to content

Commit

Permalink
test(core): ResponseExt::context_length
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 25, 2023
1 parent 9b793b1 commit 653c533
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion viz-core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use http_body_util::Full;

use crate::{header, Bytes, Error, OutgoingBody, Response, Result, StatusCode};

/// The [Response] Extension.
/// The [`Response`] Extension.
pub trait ResponseExt: Sized {
/// Get the size of this response's body.
fn content_length(&self) -> Option<u64>;
Expand Down
28 changes: 28 additions & 0 deletions viz-core/tests/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Page {
async fn response_ext() -> Result<()> {
let resp = Response::with(Full::new("<xml/>".into()), mime::TEXT_XML.as_ref());
assert!(resp.ok());
assert!(resp.content_length().is_none());
let content_type = resp.headers().typed_get::<ContentType>();
assert_eq!(
Into::<mime::Mime>::into(content_type.unwrap()),
Expand Down Expand Up @@ -122,3 +123,30 @@ async fn response_ext() -> Result<()> {
fn response_ext_panic() {
Response::redirect_with_status("/oauth", StatusCode::OK);
}

#[tokio::test]
async fn response_ext_with_server() -> Result<()> {
use viz::{Request, Router};
use viz_test::TestServer;

let router = Router::new()
.get("/", |_: Request| async move { Ok("") })
.post("/", |_: Request| async move {
Ok(Response::with(
Full::new("<xml/>".into()),
mime::TEXT_XML.as_ref(),
))
});

let client = TestServer::new(router).await?;

let resp = client.get("/").send().await.map_err(Error::normal)?;
assert_eq!(resp.content_length(), Some(0));
assert_eq!(resp.text().await.map_err(Error::normal)?, "");

let resp = client.post("/").send().await.map_err(Error::normal)?;
assert_eq!(resp.content_length(), Some(6));
assert_eq!(resp.text().await.map_err(Error::normal)?, "<xml/>");

Ok(())
}

0 comments on commit 653c533

Please sign in to comment.