From 59c9ff32200998ff7527b9034e7edb34501f6f2e Mon Sep 17 00:00:00 2001 From: JP-Ellis Date: Fri, 30 Aug 2024 13:01:02 +1000 Subject: [PATCH] fix(ffi): annotate body in raw body branch If the content type was not otherwise handled, `OptionalBody::from(&str)` was used. This sets the content type associated with the body to `None` irrespective of the content type passed through to `process_body`. This fixes the issue by ensuring the `OptionalBody::Present` is created with the appropriate content type. Signed-off-by: JP-Ellis --- rust/pact_ffi/src/mock_server/handles.rs | 42 +++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/rust/pact_ffi/src/mock_server/handles.rs b/rust/pact_ffi/src/mock_server/handles.rs index 6f261e23..56da1796 100644 --- a/rust/pact_ffi/src/mock_server/handles.rs +++ b/rust/pact_ffi/src/mock_server/handles.rs @@ -1730,7 +1730,11 @@ fn process_body( _ => { // We either have no content type, or an unsupported content type. trace!("Raw body"); - OptionalBody::from(body) + if body.is_empty() { + OptionalBody::Empty + } else { + OptionalBody::Present(Bytes::from(body.to_owned()), content_type, None) + } } } } @@ -4305,5 +4309,41 @@ mod tests { "$.id" => Generator::RandomInt(0, 1000) } }); + } + + /// See https://github.com/pact-foundation/pact-php/pull/626 + /// and https://github.com/pact-foundation/pact-reference/pull/461 + #[test] + fn annotate_raw_body_branch() { + 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("a=1&b=2&c=3").unwrap(); + let content_type = CString::new("application/x-www-form-urlencoded").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(); + + assert_eq!( + interaction + .request + .headers + .expect("no headers found") + .get("Content-Type"), + Some(&vec!["application/x-www-form-urlencoded".to_string()]) + ); + assert_eq!( + interaction.request.body.value(), + Some(Bytes::from("a=1&b=2&c=3")) + ) } }