pact-foundation / pact-reference

Reference implementations for the pact specifications
https://pact.io
MIT License
91 stars 46 forks source link

FFI > Using `regex` matcher for query: Should apply regex to all values, not just the first value #332

Closed tienvx closed 8 months ago

tienvx commented 8 months ago

I'm trying to match query to contain multiple values using ffi call but it doesn't work as I expected.

FFI call:

pactffi_with_query_parameter_v2($interactionId, 'pages', 0, '{"value":"1","regex":"\\d+","pact:matcher:type":"regex"}');

Http client query:

?pages=2&pages=3

Trimmed logs about matching rule:

2023-10-23T01:46:59.767978Z DEBUG tokio-runtime-worker pact_matching:      matching_rules: MatchingRules { rules: {QUERY: MatchingRuleCategory { name: QUERY, rules: {DocPath { path_tokens: [Root, Field("pages"), Index(0)], expr: "$.pages[0]" }: RuleList { rules: [Regex("\\d+")], rule_logic: And, cascaded: false }} }} }

Expected: No mismatches for pages

Actual: Mismatches with logs:

2023-10-23T01:46:59.768473Z DEBUG tokio-runtime-worker pact_matching: --> Mismatches: [QueryMismatch { parameter: "pages", expected: "", actual: "3", mismatch: "Expected query parameter 'pages' with value '' but was '3'" }, QueryMismatch { parameter: "pages", expected: "[\"1\"]", actual: "[\"2\", \"3\"]", mismatch: "Expected query parameter 'pages' with 1 value(s) but received 2 value(s)" }]

For more information:

tienvx commented 8 months ago

Similar ffi function pactffi_with_header_v2 has this note:

NOTE: If you pass in a form with multiple values, the index will be ignored.

Probably we can implement a similar behavior for pactffi_with_query_parameter_v2? For example, change value from '1' to ['1', '2', '3'] like this:

pactffi_with_query_parameter_v2($interactionId, 'pages', 0, '{"value":["1", "2", "3"],"regex":"\\d+","pact:matcher:type":"regex"}');

Then the index 0 will be ignored

rholshausen commented 8 months ago

If you specify the value as an array (i.e. "value":["1"]) it will work as expected.

tienvx commented 8 months ago

It works as expected, see https://github.com/pact-foundation/pact-php/pull/353

I will close this issue now, as it's not a bug.