Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed Mar 27, 2024
1 parent ed4eed7 commit bf9ad87
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/async_impl/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ where

#[cfg(test)]
mod tests {
use http_body::Body as _;

use super::Body;

#[test]
Expand All @@ -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);
}
}
24 changes: 24 additions & 0 deletions tests/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod support;

#[cfg(feature = "json")]
use http::header::CONTENT_TYPE;
use http::header::{CONTENT_LENGTH, TRANSFER_ENCODING};
use http_body_util::BodyExt;
#[cfg(feature = "json")]
use std::collections::HashMap;
Expand All @@ -21,6 +22,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() {
Expand Down
27 changes: 25 additions & 2 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bf9ad87

Please sign in to comment.