From 54e09bc045b4d313f1e0287a11f0e6163b981a97 Mon Sep 17 00:00:00 2001 From: zuisong Date: Tue, 26 Mar 2024 21:42:38 +0800 Subject: [PATCH] add test case --- src/async_impl/body.rs | 17 +++++++++++++++++ tests/blocking.rs | 26 ++++++++++++++++++++++++-- tests/client.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/async_impl/body.rs b/src/async_impl/body.rs index cc9497c37..3ba9350de 100644 --- a/src/async_impl/body.rs +++ b/src/async_impl/body.rs @@ -355,6 +355,8 @@ where #[cfg(test)] mod tests { + use http_body::Body as _; + use super::Body; #[test] @@ -363,4 +365,19 @@ mod tests { let body = Body::from(&test_data[..]); assert_eq!(body.as_bytes(), Some(&test_data[..])); } + + #[test] + fn body_exact_length() { + let empty_body = Body::empty(); + assert!(empty_body.is_end_stream()); + assert_eq!(empty_body.size_hint().exact(), Some(0)); + + let bytes_body = Body::reusable("abc".into()); + assert!(!bytes_body.is_end_stream()); + assert_eq!(bytes_body.size_hint().exact(), Some(3)); + + let stream_body = Body::streaming(bytes_body); + assert!(!stream_body.is_end_stream()); + assert_eq!(stream_body.size_hint().exact(), None); + } } diff --git a/tests/blocking.rs b/tests/blocking.rs index c4ea04bef..7d57db7b4 100644 --- a/tests/blocking.rs +++ b/tests/blocking.rs @@ -1,7 +1,6 @@ mod support; -#[cfg(feature = "json")] -use http::header::CONTENT_TYPE; +use http::header::{CONTENT_LENGTH, CONTENT_TYPE, TRANSFER_ENCODING}; use http_body_util::BodyExt; #[cfg(feature = "json")] use std::collections::HashMap; @@ -21,6 +20,29 @@ fn test_response_text() { assert_eq!(b"Hello", body.as_bytes()); } +#[test] +fn donot_set_conent_length_0_if_have_no_body() { + let server = server::http(move |req| async move { + let headers = req.headers(); + assert_eq!(headers.get(CONTENT_LENGTH), None); + assert!(headers.get(CONTENT_TYPE).is_none()); + assert!(headers.get(TRANSFER_ENCODING).is_none()); + dbg!(&headers); + http::Response::default() + }); + + let url = format!("http://{}/conent-length", server.addr()); + let res = reqwest::blocking::Client::builder() + .no_proxy() + .build() + .expect("client builder") + .post(&url) + .send() + .expect("request"); + + assert_eq!(res.status(), reqwest::StatusCode::OK); +} + #[test] #[cfg(feature = "charset")] fn test_response_non_utf_8_text() { diff --git a/tests/client.rs b/tests/client.rs index 02f32fc85..bc6002fc7 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -3,8 +3,7 @@ mod support; use support::server; -#[cfg(feature = "json")] -use http::header::CONTENT_TYPE; +use http::header::{CONTENT_LENGTH, CONTENT_TYPE, TRANSFER_ENCODING}; #[cfg(feature = "json")] use std::collections::HashMap; @@ -54,6 +53,30 @@ async fn auto_headers() { assert_eq!(res.remote_addr(), Some(server.addr())); } +#[tokio::test] +async fn donot_set_conent_length_0_if_have_no_body() { + let server = server::http(move |req| async move { + let headers = req.headers(); + assert_eq!(headers.get(CONTENT_LENGTH), None); + assert!(headers.get(CONTENT_TYPE).is_none()); + assert!(headers.get(TRANSFER_ENCODING).is_none()); + dbg!(&headers); + http::Response::default() + }); + + let url = format!("http://{}/conent-length", server.addr()); + let res = reqwest::Client::builder() + .no_proxy() + .build() + .expect("client builder") + .get(&url) + .send() + .await + .expect("request"); + + assert_eq!(res.status(), reqwest::StatusCode::OK); +} + #[tokio::test] async fn user_agent() { let server = server::http(move |req| async move {