Skip to content

Commit

Permalink
feat(ffi): allow toggling pending state
Browse files Browse the repository at this point in the history
Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Feb 26, 2024
1 parent 0d12bee commit 8ac21bd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
27 changes: 27 additions & 0 deletions rust/pact_ffi/src/mock_server/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,33 @@ ffi_fn!{
}
}

ffi_fn!{
/// Mark the interaction as pending.
///
/// * `interaction` - Interaction handle to modify.
/// * `pending` - Boolean value to toggle the pending state of the interaction.
///
/// This function will return `true` if the key was successfully updated.
fn pactffi_set_pending(interaction: InteractionHandle, pending: bool) -> bool {
interaction.with_interaction(&|_, _, inner| {
if let Some(reqres) = inner.as_v4_http_mut() {
reqres.pending = pending;
Ok(())
} else if let Some(message) = inner.as_v4_async_message_mut() {
message.pending = pending;
Ok(())
} else if let Some(sync_message) = inner.as_v4_sync_message_mut() {
sync_message.pending = pending;
Ok(())
} else {
error!("Interaction is an unknown type, is {}", inner.type_of());
Err(anyhow!("Interaction is an unknown type, is {}", inner.type_of()))
}
}).unwrap_or(Err(anyhow!("Not value to unwrap"))).is_ok()
} {
false
}
}

fn convert_ptr_to_body(body: *const u8, size: size_t, content_type: Option<ContentType>) -> OptionalBody {
if body.is_null() {
Expand Down
37 changes: 35 additions & 2 deletions rust/pact_ffi/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ use pact_ffi::mock_server::{
#[allow(deprecated)]
use pact_ffi::mock_server::handles::{
InteractionPart,
PactHandle,
pact_default_file_name,
pactffi_free_pact_handle,
pactffi_given_with_params,
pactffi_message_expects_to_receive,
pactffi_message_given,
pactffi_message_reify,
Expand All @@ -41,20 +45,22 @@ use pact_ffi::mock_server::handles::{
pactffi_new_message,
pactffi_new_message_pact,
pactffi_new_pact,
pactffi_pact_handle_write_file,
pactffi_response_status,
pactffi_set_key,
pactffi_set_pending,
pactffi_upon_receiving,
pactffi_with_binary_file,
pactffi_with_body,
pactffi_with_header,
pactffi_with_header_v2,
pactffi_with_multipart_file,
pactffi_with_multipart_file_v2,
pactffi_with_query_parameter_v2,
pactffi_with_request,
pactffi_with_specification,
pactffi_write_message_pact_file
pactffi_write_message_pact_file,
};
use pact_ffi::mock_server::handles::{pact_default_file_name, pactffi_free_pact_handle, pactffi_given_with_params, pactffi_pact_handle_write_file, pactffi_with_header_v2, PactHandle};
use pact_ffi::verifier::{
OptionsFlags,
pactffi_verifier_add_directory_source,
Expand Down Expand Up @@ -258,6 +264,33 @@ fn set_key() {
});
}

#[test]
fn set_pending() {
let consumer_name = CString::new("consumer").unwrap();
let provider_name = CString::new("provider").unwrap();
let pact_handle = pactffi_new_pact(consumer_name.as_ptr(), provider_name.as_ptr());
let description = CString::new("set_pending").unwrap();
let interaction = pactffi_new_interaction(pact_handle, description.as_ptr());

assert!(pactffi_set_pending(interaction, true));

interaction.with_interaction(&|_, _, i| {
assert_eq!(
i.as_v4_http().unwrap().pending,
true,
)
});

assert!(pactffi_set_pending(interaction, false));

interaction.with_interaction(&|_, _, i| {
assert_eq!(
i.as_v4_http().unwrap().pending,
false,
)
});
}

#[test_log::test]
#[allow(deprecated)]
fn http_consumer_feature_test() {
Expand Down

0 comments on commit 8ac21bd

Please sign in to comment.