Skip to content

Commit

Permalink
fix: Do not apply include matcher at the collection level #485
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jan 29, 2025
1 parent 096deed commit e98a218
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
54 changes: 50 additions & 4 deletions rust/pact_ffi/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{env, thread};
use std::collections::HashSet;
use std::ffi::{CStr, CString};
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -2039,9 +2040,54 @@ fn numeric_matcher_passing_test_sending_string_value() {
.map(|mismatch| mismatch.as_object().cloned().unwrap())
.map(|mismatch| mismatch.get("mismatch").cloned().unwrap())
.map(|mismatch| mismatch.as_str().unwrap().to_string())
.collect_vec();
assert_eq!(mismatches, vec![
"Expected '456' (String) to be a number",
"Expected '321.1' (String) to be a number"
.collect::<HashSet<_>>();
assert_eq!(mismatches, hashset![
"Expected '456' (String) to be a number".to_string(),
"Expected '321.1' (String) to be a number".to_string()
]);
}

// Issue #485
#[test_log::test]
fn include_matcher_in_query_parameters() {
let consumer_name = CString::new("485-consumer").unwrap();
let provider_name = CString::new("485-provider").unwrap();
let pact_handle = pactffi_new_pact(consumer_name.as_ptr(), provider_name.as_ptr());

let description = CString::new("request_with_query_matcher").unwrap();
let interaction = pactffi_new_interaction(pact_handle.clone(), description.as_ptr());

let path = CString::new("/request").unwrap();
let query_param_matcher = CString::new("{\"value\":\"substring\",\"pact:matcher:type\":\"include\", \"value\":\"sub\"}").unwrap();
let address = CString::new("127.0.0.1:0").unwrap();
let method = CString::new("GET").unwrap();
let query = CString::new("item").unwrap();

pactffi_upon_receiving(interaction.clone(), description.as_ptr());
pactffi_with_request(interaction.clone(), method.as_ptr(), path.as_ptr());
pactffi_with_query_parameter_v2(interaction.clone(), query.as_ptr(), 0, query_param_matcher.as_ptr());
pactffi_response_status(interaction.clone(), 200);

let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false);
expect!(port).to(be_greater_than(0));

let client = Client::default();
let result = client.get(format!("http://127.0.0.1:{}/request?item=subway", port).as_str())
.send();

let mismatches = unsafe {
CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned()
};

pactffi_cleanup_mock_server(port);

expect!(mismatches).to(be_equal_to("[]"));
match result {
Ok(res) => {
expect!(res.status()).to(be_eq(200));
},
Err(_) => {
panic!("expected 200 response but request failed");
}
};
}
4 changes: 4 additions & 0 deletions rust/pact_matching/src/matchingrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ impl <T: Debug + Display + PartialEq + Clone> Matches<&[T]> for &[T] {
MatchingRule::Values => Ok(()),
MatchingRule::Number | MatchingRule::Decimal | MatchingRule::Integer => Ok(()),
MatchingRule::Time(_) | MatchingRule::Date(_) | MatchingRule::Timestamp(_) => Ok(()),
MatchingRule::Include(_) => Ok(()),
MatchingRule::ContentType(_) => Ok(()),
MatchingRule::Boolean => Ok(()),
MatchingRule::Semver => Ok(()),
_ => Err(anyhow!("Unable to match {} using {:?}", self.for_mismatch(), matcher))
};
debug!("Comparing '{:?}' to '{:?}' using {:?} -> {:?}", self, actual, matcher, result);
Expand Down

0 comments on commit e98a218

Please sign in to comment.