Skip to content

Commit

Permalink
fix(pact_matching): Allow matching strings using Values matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jan 21, 2024
1 parent a990c55 commit f4891c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
35 changes: 27 additions & 8 deletions rust/pact_matching/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,23 +681,42 @@ mod tests {
"arrayContaining".to_string()
]
};
let actual = hashmap! {
let wrong_order = hashmap! {
"X-IMPROVED".to_string() => vec![
"regex".to_string(),
"like".to_string(),
"values".to_string(),
"arrayContaining".to_string()
]
};
let result = match_headers(Some(expected), Some(actual), &context);
let result = match_headers(Some(expected.clone()), Some(wrong_order), &context);
expect!(result.values().flatten()).to_not(be_empty());

let mismatches: Vec<Mismatch> = result.values().flatten().cloned().collect();
expect!(mismatches[0].clone()).to(be_equal_to(Mismatch::HeaderMismatch {
key: "X-IMPROVED".to_string(),
expected: "like".to_string(),
actual: "regex".to_string(),
mismatch: "Mismatch with header 'X-IMPROVED': Unable to match 'like' using Values for value at index 0".to_string(),
}));
expect!(mismatches.clone()).to(be_equal_to(vec![
Mismatch::HeaderMismatch {
key: "X-IMPROVED".to_string(),
expected: "like".to_string(),
actual: "regex".to_string(),
mismatch: "Mismatch with header 'X-IMPROVED': Expected 'regex' to be equal to 'like' for value at index 0".to_string(),
},
Mismatch::HeaderMismatch {
key: "X-IMPROVED".to_string(),
expected: "regex".to_string(),
actual: "like".to_string(),
mismatch: "Mismatch with header 'X-IMPROVED': Expected 'like' to be equal to 'regex' for value at index 1".to_string(),
}
]));

let actual = hashmap! {
"X-IMPROVED".to_string() => vec![
"like".to_string(),
"regex".to_string(),
"values".to_string(),
"arrayContaining".to_string()
]
};
let result = match_headers(Some(expected.clone()), Some(actual), &context);
expect!(result.values().flatten()).to(be_empty());
}
}
2 changes: 1 addition & 1 deletion rust/pact_matching/src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl Matches<&str> for &str {
Err(err) => Err(anyhow!("'{}' is not a valid regular expression - {}", regex, err))
}
},
MatchingRule::Equality => {
MatchingRule::Equality | MatchingRule::Values => {
if self == &actual {
Ok(())
} else {
Expand Down

0 comments on commit f4891c0

Please sign in to comment.