Skip to content

Commit

Permalink
fix: Handle empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Nov 13, 2024
1 parent b29ed8c commit b5415d0
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions rust/pact_ffi/src/mock_server/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,11 @@ fn process_body(
matching_rules,
generators
);

if body.is_empty() {
return OptionalBody::Empty;
}

let detected_type = detect_content_type_from_string(body);
let content_type = content_type
.clone()
Expand Down Expand Up @@ -1752,11 +1757,7 @@ fn process_body(
_ => {
// We either have no content type, or an unsupported content type.
trace!("Raw body");
if body.is_empty() {
OptionalBody::Empty
} else {
OptionalBody::Present(Bytes::from(body.to_owned()), content_type, None)
}
OptionalBody::Present(Bytes::from(body.to_owned()), content_type, None)
}
}
}
Expand Down Expand Up @@ -4374,4 +4375,35 @@ mod tests {
Some(Bytes::from(raw))
)
}

#[test]
fn pactffi_with_empty_body_test() {
let pact_handle = PactHandle::new("Consumer", "Provider");
let description = CString::new("Generator Test").unwrap();
let i_handle = pactffi_new_interaction(pact_handle, description.as_ptr());

let body = CString::new("").unwrap();
let content_type = CString::new("text/plain").unwrap();
let result = pactffi_with_body(
i_handle,
InteractionPart::Request,
content_type.as_ptr(),
body.as_ptr(),
);
assert!(result);

let interaction = i_handle
.with_interaction(&|_, _, inner| inner.as_v4_http().unwrap())
.unwrap();

expect!(
interaction
.request
.headers
).to(be_none());
assert_eq!(
interaction.request.body.value(),
None
)
}
}

0 comments on commit b5415d0

Please sign in to comment.