Skip to content

fix(matching): RFC 9110 repeated-header matching (review PR for #568) - #584

Closed
JP-Ellis wants to merge 7 commits into
fix/repeated-headersfrom
fix/repeated-headers-rfc9110
Closed

JP-Ellis wants to merge 7 commits into
fix/repeated-headersfrom
fix/repeated-headers-rfc9110

Conversation

@JP-Ellis

Copy link
Copy Markdown
Member

Warning

I pushed this late in the day. I (Josh) have not vetted all of it yet. Treat it as a draft for discussion.

This is a review PR for #568. It targets fix/repeated-headers, so the diff shows only my changes on top of yours. Each commit addresses one behaviour, so you can cherry-pick the ones you want and drop the rest. Take it or leave it.

The commits read best one at a time: open the Commits tab and step through them. Each commit message explains its reasoning in more depth than this summary.

Approach

RFC 9110 §5.3 defines repeated field lines as equivalent to one line joined with ", ". Joining is always safe. Splitting needs the header's grammar, which Pact doesn't know for custom headers. #568 splits only the actual values, so a repeated header whose values contain commas (dates, Link URIs) no longer matches itself. These commits join both sides instead, and split only where a matcher needs list elements.

Nothing that passes on master fails here, in either engine. Where only master's per-line comparison matches, the header still passes and logs a deprecation warning, which suggests the matcher to use instead.

Commits

  1. Classify headers once for both engines. One header_kind predicate, used by both the legacy and the V2 engine. Only custom headers get the new behaviour. Content-Type and the known multi-value headers stop being split in legacy, which matches master and V2.
  2. Compare repeated custom headers by their joined value. With no matcher, both sides are joined and compared. Whitespace and empty elements outside quoted strings are ignored. This fixes the comma-in-value regression. The V2 engine calls the same shared function, so the two engines can't drift apart.
  3. Apply scalar matchers to the joined value. Regex, Type, number matchers and the like see a, b as one value. If only the per-line match passes, the warning suggests wrapping the rule in EachValue.
  4. Split into list elements for list matchers. MinType, MaxType, MinMaxType, EachValue, ArrayContains, indexed and [*] rules see elements, split on commas outside quoted strings. This removes split_header_field_lines. This is the first commit that is safe against master. Stopping at commit 2 or 3 keeps fix(matching): match combined headers against repeated expectations #568's behaviour for list matchers over values containing commas.
  5. Link and Via grammars. A comma inside <…> (RFC 8288) or a nested (…) comment (RFC 9110 §5.6.5) doesn't split.
  6. Test repair. Restores master's separate-line inputs for the ArrayContains and EachValue X-Id tests, keeps your combined-line versions beside them, and drops two asserts that couldn't fail.
  7. README. Describes header matching by header kind.

Known gaps

  • The deprecation warning fires on every match call. A mock server tries each request against every interaction, so it can warn for interactions it doesn't pick.
  • The V2 engine now reports legacy-style mismatch text for repeated custom headers.
  • I haven't built wasm32-wasip2 locally. CI covers it.

@YOU54F

YOU54F commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Hey dude! Finally got some time to look at this, set up a little harness to test it against the various ffi consumers, findings are below. It was written by Claude FYI

Downstream validation of #568 + #584

I built pact_ffi from three sources and ran every pact_ffi consumer against each one on macOS arm64:

label source
released pact_ffi 0.5.12 release download
pr568 fix/repeated-headers @ 138fc40
pr584 fix/repeated-headers-rfc9110 @ b20acd8 (+ the header grammar fix below)

The FFI surface is unchanged (rust/pact_ffi diff is empty, crate still 0.5.12), so consumers kept the released pact.h and only swapped the library.

Verdict

Scenarios

✅ = behaved as expected, ❌ = didn't.

case expected released pr568 pr584
H1 custom header a, b on separate lines match ✅ ✅ ✅
H2 custom header a, b on one line (the pact-net regression) match ❌ ✅ ✅
H3 values containing commas (HTTP dates) on separate lines match ✅ ❌ ✅
H4 Content-Type: text/plain, text/html (Parameterised) no match ✅ ❌ ✅
H5 Cache-Control: no-cache, no-store combined match ✅ ✅ ✅
H6a per-index regex matchers on repeated lines match, no warning ✅ ✅ ✅
H6b only the per-line pass matches match + warning ✅ ✅ ✅ (warns)
H8 Link with commas inside <…>: separate lines / one line match ✅ / ❌ ❌ / ❌ ✅ / ✅
H9 pact file written the same way identical ✅ ¹

H1–H5 were run in pact-go, pact-js-core and pact-python, with identical results in all three. H6 and H8 were run in pact-go, and later as per-consumer tests in all six consumers.

  1. pact-php's example pact files are byte-identical across the three libraries, apart from metadata.pactRust.models (1.3.16 vs 1.3.17).

Consumer suites

consumer released pr568 pr584
pact-net (146) 138 / 8 fail ² 146 / 0 146 / 0
pact-go unit + consumer examples pass pass pass
pact-php unit (995) + examples pass ³ pass ³ pass ³
pact-ruby-ffi (6) 6 / 0 6 / 0 6 / 0
pact-js-core (98) 93 / 4 ⁴ 92 / 5 ⁴ 94 / 3 ⁴
pact-python (718 incl. compatibility suite V1–V4) + examples (18) 694 / 1 693 / 2 695 / 0
  1. Seven of these are repeated X-Request lines returning a 500, the failure chore: download pact_ffi 0.5.12 from the dist-built release pact-net#570 is working around. With fix(matching): RFC 9110 repeated-header matching (review PR for #568) #584, the original X-Request tests pass, so chore: download pact_ffi 0.5.12 from the dist-built release pact-net#570 can drop its Accept-Language workaround once 0.5.13 is out. The eighth is a fixture that pins pact_models 1.3.17.
  2. Except the SSE example, which fails on all three libraries because its plugin isn't installed locally.
  3. Three MATT plugin tests fail on all three libraries because the plugin isn't installed. The remaining failures are H2 (released) and H3/H4 (pr568).

After the consumer-side fixes listed under "Follow-ups", a final pr584 rerun was green everywhere: pact-net 173/0, pact-go 251/0 + 8/0, pact-js 314/314, pact-js-core 101 pass / 1 skipped, pact-python 699 pass, pact-php 996 OK, pact-ruby 9/0, pact-ruby-ffi 16/0.

Deprecation warning

  • The RFC 9110 §5.3 warning appeared 0 times across all existing consumer suites. Nothing real hits the fallback path, so upgrading won't flood users' logs.
  • It only appears when forced (H6b), and the match still succeeds, so it's warn-only.
  • Per-index matchers (H6a) don't trigger it. It needs a scalar rule on the bare header path, and the language DSLs don't generate one for repeated values.
  • The known gap still stands: the warning can fire for interactions the mock server then doesn't pick. That didn't come up in practice here.

Gap found: header normalisation ignores the header's grammar

With no matching rules, header values were split without the header's own grammar. So a whitespace change inside a Link <URI>, a Via (comment) or a quoted string matched when it shouldn't have. This happened in two places:

  • Repeated values: normalise_list always used the plain grammar.
  • Single values: match_header_value's no-rule fallback (and the V2 engine's multi-value normalisation) used a naive split(',') + trim. That one is in released code since pact_ffi 0.5.3.

Fix: YOU54F#14 (link, commits de449d7 and fd5ed24, based on this branch).

  • normalise_list uses Grammar::for_header(name), and both single-value paths use normalise_list.
  • New cases::all() rows cover repeated and single values: whitespace inside a Link target, a Via comment and a quoted string, plus Cache-Control whitespace around commas (still matches).
  • cargo test -p pact_matching passes. A cross-core probe (Rust / pact-jvm / pact-ruby v1) confirms Link whitespace inside the URI now mismatches at V2/V3/V4.

Behaviour change for single values: Cache-Control: private="x,y" no longer matches private="x, y". That's RFC 9110 correct, but it changes released behaviour, so it needs a release note.

Happy for it to be cherry-picked here.

Separate, pre-existing: per-index header/query matchers are lost when the pact file is written

This isn't caused by #568 or #584, but #584 makes it more visible.

  • pactffi_with_header_v2 with index > 0 keys rules as $.name[n]. MatchingRuleCategory::to_v3_json (and the generator equivalent) reduces each key to first_field(), so every index collapses onto the bare name.
  • Which rule survives is random (HashMap order). It happens on released 0.5.12 too, for V3 and V4.
  • A provider replaying the pact then applies one regex to every value, which on fix(matching): RFC 9110 repeated-header matching (review PR for #568) #584 goes through the scalar fallback and its warning.

There's a draft fix on the fork, YOU54F#15 (link). It writes indexed keys as $['X-Request'][1]. It's a pact file format extension, so it needs a pact-specification discussion first.

I tested pact-jvm (master): it doesn't support per-value header or query rules in any key layout. The details are in the drafted issue, which I'll raise separately.

Suggested path

  1. Pick up the header grammar fix (fix(matching): normalise header values using the header's own grammar YOU54F/pact-reference#14) into fix(matching): RFC 9110 repeated-header matching (review PR for #568) #584.
  2. Merge fix(matching): RFC 9110 repeated-header matching (review PR for #568) #584 into fix/repeated-headers. CI hasn't run on fix(matching): RFC 9110 repeated-header matching (review PR for #568) #584, because test.yml only triggers pull_request for base master. Merging it gets CI on fix(matching): match combined headers against repeated expectations #568.
  3. Merge fix(matching): match combined headers against repeated expectations #568, release pact_ffi 0.5.13.
  4. Bump chore: download pact_ffi 0.5.12 from the dist-built release pact-net#570 to 0.5.13 and restore the X-Request tests.

Follow-ups in the consumers (to land with the 0.5.13 upgrade)

Each consumer has repeated-header tests (H1–H8b). These consumer API gaps came up and are fixed in draft PRs on my forks:

consumer draft gap fixed
pact-js YOU54F/pact-js#6 joined array header values into one string
pact-ruby YOU54F/pact-ruby#1 joined array header values, and skipped cleanup on error
pact-go YOU54F/pact-go#2 mangled values containing " or \; no way to read mock server logs
pact-net YOU54F/pact-net#39 no multi-value WithHeader; no way to read mock server logs
pact-php YOU54F/pact-php#4 no way to read mock server logs
pact-js-core YOU54F/pact-js-core#51 no way to read mock server logs; no per-index header API
pact-python YOU54F/pact-python#3 tests only, no API gap
pact-ruby-ffi YOU54F/pact-ruby-ffi#1 tests only, no API gap

Without a way to read the mock server logs, those consumers couldn't assert the deprecation warning.

The pact-php and pact-net changes add interface methods, so they'll need release notes.

@YOU54F

YOU54F commented Oct 29, 2026

Copy link
Copy Markdown
Member

I've tested a merge of the three stacked pr's against CI

YOU54F#17

which is all green, and also tested locally against the client libs in the linked draft pr's

The legacy matcher and the V2 plan builder each decided on their own
which headers may be split on commas. Legacy split Content-Type and
multi-value headers such as Cache-Control, while V2 skipped both, so the
same pact verified differently depending on the engine.

repeated_headers::header_kind now maps a header name to Singleton,
Parameterised, MultiValue or Custom, using the lists in pact_models.
Both engines split only Custom headers, which removes the Content-Type
divergence: ["text/plain", "text/html"] no longer matches a single
"text/plain, text/html" line in either engine. Tests pin that
Content-Type and Cache-Control stay unsplit.

Assisted-by: Claude Code:claude-opus-5-5
RFC 9110 §5.3 makes repeated field lines equivalent to one line with
the values joined by commas. A repeated custom header without matchers
now joins both sides with ", " and compares the results, ignoring
whitespace around commas outside quoted strings and empty list elements.
Both engines share this logic through `match_repeated_header`.

The previous change split only the actual field lines on commas. That
broke repeated values that contain commas themselves: `["a, b", "c"]`
against the same lines, two `X-Sunset` dates, or two `Link` lines whose
URIs hold commas. Joining both sides keeps the combined, separate and
mixed `X-Request` groupings matching.

When the joined comparison fails, the header falls back to the
per-line comparison it used before. A pass through the fallback logs a
deprecation warning: per-line matching of repeated headers is
deprecated, and a future release will compare only the joined value. A failed
fallback reports one mismatch for the joined comparison.

The legacy engine extracts the per-line body of `match_header_maps`
into `match_header_lines`. The V2 plan replaces `header:split-values`
with `header:match-repeated`. A custom header with one expected value
gains a `header:repeated` check, so a single actual line keeps the
existing plan node. Headers with matching rules keep the actual-only
split until matchers move onto the joined value.

Changed expectations:
- `match_headers_returns_a_mismatch_if_the_header_values_have_different_lengths`
  now reports the joined mismatch (`Expected 'd' to be equal to 'd, e'`).
- The V2 plan snapshots in `engine/tests/header_tests.rs` show the new
  actions. `match_headers_with_number_type_matching_rule` reports the
  legacy engine's matcher message for repeated actual lines.

Assisted-by: Claude Code:claude-opus-5-5
A repeated custom header with scalar matchers (Regex, Type, Integer and
the like) on its own path is now matched as RFC 9110 §5.3 describes it:
the expected and actual field lines are joined with commas and the
matchers run once against each joined value. List-aware rules (MinType,
MaxType, MinMaxType, EachValue, ArrayContains) and headers with indexed
rules go through the actual-only split_header_field_lines, with no
fallback, until the list-element commit.

When the joined value fails but the field lines pass one at a time, the
header still matches and logs the per-line deprecation warning. The
warning also suggests wrapping the user's rule in EachValue, rendered
as JSON, as the way to keep per-value matching once only the joined
value is compared.

Two existing expectations change under the joined semantics, and the
mismatch now describes the joined value instead of one line:
- engine header_tests match_headers_with_number_type_matching_rule:
  "Expected '1111, two' to match an integer number".
- headers::tests::match_header_with_a_values_matcher: a Values rule is
  not list-aware, so a reordered list reports one mismatch between the
  two joined values.

A header whose expected list is empty skips the repeated-header node in the
V2 plan and the matcher tolerates an empty expected list, so it cannot panic.

Assisted-by: Claude Code:claude-opus-5-5
…chers

List-aware matchers (MinType, MaxType, MinMaxType, EachValue,
ArrayContains) and index-specific matchers now see the list elements of
a repeated custom header. The elements come from the joined actual
value, so `3, 4` / `567` and `3` / `4` / `567` both give three elements.
The splitter honours RFC 9110 §5.6.4: commas inside quoted strings do
not separate elements, and `\` escapes inside quotes are respected. It
trims each element and drops empty ones (§5.6.1). It has no `{}` or `[]`
nesting, so a JSON-looking value splits at its commas like any other.
`normalise_list` shares the same tokenizer.

An indexed matcher triggers the split when it sits at `$.<key>.<n>` or
`$.<key>[<n>]` with `n` below the larger of the expected count and the
element count. Wildcard element rules at `$.<key>[*]` or `$.<key>.*`
address every element, so they count as list rules too. This replaces a
bare path-length check that caught any deeper path.

When the element match fails, the matcher retries over the field lines
as received, which is the old behaviour. A pass there logs a warning.
The warning says whole-line matching is deprecated, gives the element
count a future release will see, and points at RFC 9110 §5.5's advice to
quote values that contain commas. This keeps MaxType(2) against
`x, y` / `z`, and EachValue over unquoted dates or Link values, passing.

The PR's interim `split_header_field_lines`, which tracked brace and
bracket nesting, is removed. No existing test expectation changed: the
JSON-valued compatibility cases carry no rules, so they still go through
plain comparison.

Assisted-by: Claude Code:claude-opus-5-5
Splitting a repeated header into list elements cut every comma outside
a quoted string. Two registered headers carry commas of their own:

- Link (RFC 8288 §3): a comma inside `<…>` belongs to the target URI,
  as in `<https://a.example/?fields=id,name>; rel="next"`.
- Via (RFC 9110 §5.6.5): a comma inside a `(…)` comment belongs to the
  comment, and comments nest.

`split_list_elements` now keeps those commas, matching the header name
case-insensitively. Every other header reads `<`, `>`, `(` and `)` as
ordinary characters. Delimiters inside quoted strings do not count, and
an unbalanced `<` or `(` extends to the end of the value, as an
unbalanced quote does.

`normalise_list` stays name-agnostic. Plain comparison normalises both
sides the same way, so a comma inside `<…>` that both share is harmless.

User-Agent and Server are singletons (their product comments may hold
commas) and are never split.

The "each-value link fallback" case no longer needs the per-line
fallback, so it becomes "each-value link" with no warning. New cases
cover a combined Link line and a MaxType rule on Via.

Assisted-by: Claude Code:claude-opus-5-5
…ones

The ArrayContains and EachValue X-Id tests had their inputs rewritten to
combined field lines, which dropped coverage of the separate-line form
that master tested. Restore the original inputs and add
`_with_combined_field_lines` variants carrying the combined-line inputs
with the same assertions.

Also remove the two length asserts at the end of
match_headers_combined_custom_header_compatibility. match_headers
receives clones, so they cannot fail.

Assisted-by: Claude Code:claude-opus-5-5
The "Matching Headers" section still said values are compared after
stripping whitespace following commas. Describe the current rules:
repeated custom headers compare by their comma-joined values per RFC 9110
section 5.3, list-aware and index-specific matchers see split elements
(respecting quotes, Link angle brackets and Via comments), other matchers
see the joined value, per-line-only matches pass with a deprecation
warning, and single-value, Accept/Content-Type and known multi-value
headers are unchanged.

Assisted-by: Claude Code:claude-opus-5-5
@YOU54F

YOU54F commented Nov 12, 2026

Copy link
Copy Markdown
Member

merged in #602

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants