Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow non-object matchers for array-contains #459

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions rust/pact_ffi/src/mock_server/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use either::Either;
use lazy_static::lazy_static;
use regex::Regex;
use serde_json::{Map, Value};
use tracing::{debug, error, trace, warn};
use tracing::{debug, error, trace};

use pact_models::bodies::OptionalBody;
use pact_models::content_types::ContentTypeHint;
Expand Down Expand Up @@ -113,9 +113,11 @@ fn process_matcher(
Value::Object(map) => {
process_object(map, &mut category, &mut generators, DocPath::root(), false)
}
Value::Array(arr) => {
process_array(arr, &mut category, &mut generators, DocPath::root(), false, false)
}
_ => {
warn!("arrayContains: JSON for variant {} is not correctly formed: {}", index, variant);
Value::Null
variant.clone()
}
};
json_values.push(value);
Expand Down Expand Up @@ -423,16 +425,19 @@ fn format_multipart_error(e: std::io::Error) -> String {

#[cfg(test)]
mod test {
use expectest::prelude::*;
use std::collections::HashMap;

use expectest::prelude::*;
use maplit::hashmap;
use pretty_assertions::assert_eq;
use pact_models::prelude::Category;
use pretty_assertions::assert_eq;
use rstest::rstest;
use serde_json::json;

use pact_models::{generators, HttpStatus, matchingrules_list};
use pact_models::content_types::ContentType;
use pact_models::generators::{Generator, Generators};
use pact_models::matchingrules::{MatchingRule, MatchingRuleCategory};
use pact_models::matchingrules::{MatchingRule, MatchingRuleCategory, RuleList};
use pact_models::matchingrules::expressions::{MatchingRuleDefinition, ValueType};
use pact_models::path_exp::DocPath;

Expand Down Expand Up @@ -860,6 +865,12 @@ mod test {
#[case(json!({ "pact:matcher:type": "content-type", "value": "text/plain" }), vec![MatchingRule::ContentType("text/plain".to_string())])]
#[case(json!({ "pact:matcher:type": "arrayContains", "variants": [] }), vec![MatchingRule::ArrayContains(vec![])])]
#[case(json!({ "pact:matcher:type": "array-contains", "variants": [] }), vec![MatchingRule::ArrayContains(vec![])])]
#[case(json!({ "pact:matcher:type": "array-contains", "variants": ["Thing1", "Thing2"] }), vec![
MatchingRule::ArrayContains(vec![
(0, MatchingRuleCategory{name: Category::BODY, rules: HashMap::from([ (DocPath::empty(), RuleList::equality()) ]) }, std::collections::HashMap::default()),
(0, MatchingRuleCategory{name: Category::BODY, rules: HashMap::from([ (DocPath::empty(), RuleList::equality()) ]) }, std::collections::HashMap::default())
])
])]
#[case(json!({ "pact:matcher:type": "values" }), vec![MatchingRule::Values])]
#[case(json!({ "pact:matcher:type": "statusCode" }), vec![MatchingRule::StatusCode(HttpStatus::Success)])]
#[case(json!({ "pact:matcher:type": "statusCode" }), vec![MatchingRule::StatusCode(HttpStatus::StatusCodes(vec![200]))])]
Expand Down
Loading