From 504c34c40f923706e0a107512313674bf306a80c Mon Sep 17 00:00:00 2001 From: Lorenzo Leonardo Date: Sun, 17 Mar 2024 21:28:15 +0800 Subject: [PATCH] fix: if response body empty, Response must be None --- src/collector.rs | 30 ++++++++++++++++++--- src/test/post.rs | 69 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/collector.rs b/src/collector.rs index 18f3a8c..df429a8 100644 --- a/src/collector.rs +++ b/src/collector.rs @@ -325,8 +325,20 @@ impl ExtendedHandler for Collector { fn get_response_body(&self) -> Option> { match self { Collector::File(_) => None, - Collector::Ram(container) => Some(container.clone()), - Collector::RamAndHeaders(container, _) => Some(container.clone()), + Collector::Ram(container) => { + if container.is_empty() { + None + } else { + Some(container.clone()) + } + } + Collector::RamAndHeaders(container, _) => { + if container.is_empty() { + None + } else { + Some(container.clone()) + } + } Collector::FileAndHeaders(_, _) => None, } } @@ -338,7 +350,13 @@ impl ExtendedHandler for Collector { fn get_response_body_and_headers(&self) -> (Option>, Option) { match self { Collector::File(_) => (None, None), - Collector::Ram(container) => (Some(container.clone()), None), + Collector::Ram(container) => { + if container.is_empty() { + (None, None) + } else { + (Some(container.clone()), None) + } + } Collector::RamAndHeaders(container, headers) => { let header_str = std::str::from_utf8(headers).unwrap(); let mut header_map = HeaderMap::new(); @@ -354,7 +372,11 @@ impl ExtendedHandler for Collector { } } } - (Some(container.clone()), Some(header_map)) + if container.is_empty() { + (None, Some(header_map)) + } else { + (Some(container.clone()), Some(header_map)) + } } Collector::FileAndHeaders(_, headers) => { let header_str = std::str::from_utf8(headers).unwrap(); diff --git a/src/test/post.rs b/src/test/post.rs index d7bf748..559c16b 100644 --- a/src/test/post.rs +++ b/src/test/post.rs @@ -31,7 +31,7 @@ async fn test_post() { println!("Response: {:?}", response); assert_eq!(response.status(), StatusCode::OK); - assert_eq!(*response.body(), Some(Vec::new())); + assert_eq!(*response.body(), None); assert!(!response.headers().is_empty()); } @@ -60,7 +60,7 @@ async fn test_post_none() { println!("Response: {:?}", response); assert_eq!(response.status(), StatusCode::OK); - assert_eq!(*response.body(), Some(Vec::new())); + assert_eq!(*response.body(), None); assert!(!response.headers().is_empty()); } @@ -89,7 +89,7 @@ async fn test_post_none_no_option() { println!("Response: {:?}", response); assert_eq!(response.status(), StatusCode::OK); - assert_eq!(*response.body(), Some(Vec::new())); + assert_eq!(*response.body(), None); assert!(!response.headers().is_empty()); } @@ -117,7 +117,7 @@ async fn test_post_with_headers() { println!("Response: {:?}", response); assert_eq!(response.status(), StatusCode::OK); - assert_eq!(*response.body(), Some(Vec::new())); + assert_eq!(*response.body(), None); assert!(!response.headers().is_empty()); } @@ -143,7 +143,7 @@ async fn test_post_sync() { println!("Response: {:?}", response); assert_eq!(response.status(), StatusCode::OK); - assert_eq!(*response.body(), Some(Vec::new())); + assert_eq!(*response.body(), None); assert!(!response.headers().is_empty()); } @@ -169,7 +169,7 @@ async fn test_post_sync_not_option() { println!("Response: {:?}", response); assert_eq!(response.status(), StatusCode::OK); - assert_eq!(*response.body(), Some(Vec::new())); + assert_eq!(*response.body(), None); assert!(!response.headers().is_empty()); } @@ -198,6 +198,61 @@ async fn test_post_async_not_option() { println!("Response: {:?}", response); assert_eq!(response.status(), StatusCode::OK); - assert_eq!(*response.body(), Some(Vec::new())); + assert_eq!(*response.body(), None); + assert!(!response.headers().is_empty()); +} + +#[tokio::test] +async fn test_post_sync_not_option_empty_string() { + let responder = MockResponder::new(ResponderType::Body(Vec::new())); + let (server, _tempdir) = setup_test_environment(responder).await; + let target_url = Url::parse(format!("{}/test", server.uri()).as_str()).unwrap(); + + let collector = Collector::Ram(Vec::new()); + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::POST) + .body(Vec::new()) + .unwrap(); + + let response = HttpClient::new(collector) + .request(request) + .unwrap() + .blocking() + .perform() + .unwrap(); + + println!("Response: {:?}", response); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); + assert!(!response.headers().is_empty()); +} + +#[tokio::test] +async fn test_post_async_not_option_empty_string() { + let responder = MockResponder::new(ResponderType::Body(Vec::new())); + let (server, _tempdir) = setup_test_environment(responder).await; + let target_url = Url::parse(format!("{}/test", server.uri()).as_str()).unwrap(); + + let actor = CurlActor::new(); + let collector = Collector::Ram(Vec::new()); + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::POST) + .body(Vec::new()) + .unwrap(); + + let response = HttpClient::new(collector) + .request(request) + .unwrap() + .nonblocking(actor) + .perform() + .await + .unwrap(); + + println!("Response: {:?}", response); + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); assert!(!response.headers().is_empty()); }