Skip to content

Commit

Permalink
fix: if response body empty, Response must be None
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoLeonardo committed Mar 17, 2024
1 parent fe6a262 commit 504c34c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 11 deletions.
30 changes: 26 additions & 4 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,20 @@ impl ExtendedHandler for Collector {
fn get_response_body(&self) -> Option<Vec<u8>> {
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,
}
}
Expand All @@ -338,7 +350,13 @@ impl ExtendedHandler for Collector {
fn get_response_body_and_headers(&self) -> (Option<Vec<u8>>, Option<HeaderMap>) {
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();
Expand All @@ -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();
Expand Down
69 changes: 62 additions & 7 deletions src/test/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}

Expand Down Expand Up @@ -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());
}

0 comments on commit 504c34c

Please sign in to comment.