Skip to content

feign-mock: allow matching request headers by subset - #3661

Merged
velo merged 4 commits into
OpenFeign:masterfrom
lxcxjxhx:1552-mock-header-subset
Nov 9, 2026
Merged

velo merged 4 commits into
OpenFeign:masterfrom
lxcxjxhx:1552-mock-header-subset

Conversation

@lxcxjxhx

Copy link
Copy Markdown
Contributor

Fixes #1552

What this does

Adds an opt-in way to match only the request headers a test declares, instead of requiring the
expected headers to be exactly equal to the ones the client actually sends.

RequestKey expected =
    RequestKey.builder(HttpMethod.GET, "/api")
        .headers(RequestHeaders.builder().add("Authorization", "Bearer token").build())
        .matchHeadersBySubset()
        .build();

A request carrying Authorization: Bearer token plus a generated Content-Length and Host now
matches, which was not possible before.

Why opt-in instead of changing the default

Making subset matching the default would silently weaken every existing test: a test that declares
three headers currently asserts nothing else is present, and after the change extra headers would
be ignored with no notice. That is a breaking change in assertion strength, so the new behaviour is
a builder flag and the old exact comparison is kept verbatim when the flag is absent. There is a
test that pins this (exactMatchingIsPreservedByDefault).

Change

  • RequestHeaders#isSubsetOf(RequestHeaders) — every declared header must be present on the actual
    request with all of its declared values; headers only the actual request carries are ignored.
  • RequestKey.Builder#matchHeadersBySubset() — enables the above for that key.
  • RequestKey#equalsExtended — branches on the flag; method, URL, charset and body comparison are
    untouched.

Values are compared as a set (containsAll), so header order is not significant. Tell me if you
would rather have strict ordering and I will switch to List#equals.

Tests

RequestKeyHeaderSubsetTest, 7 cases:

  • extra headers are ignored when matching by subset
  • exact matching is preserved by default
  • a missing declared header still fails
  • a wrong declared value still fails
  • every declared value of a multi-value header must be present
  • a key with no declared headers matches any request
  • method and URL are still compared
Tests run: 7, Failures: 0  -- RequestKeyHeaderSubsetTest

Full module regression, unchanged tests included:

feign-core   772 tests, 0 failures, 4 skipped
feign-mock    43 tests, 0 failures
BUILD SUCCESS

validate-code-format passes.

Documentation

Both new methods carry Javadoc. This PR does not add mock/README.md: the AGENTS.md README
requirement applies to new modules, and mock is an existing module that has never had one
(13 of 56 modules are in that state). Happy to add it in a separate docs PR if you want it.

Was this PR generated or assisted by AI?

Partly. The patch and tests were produced with AI assistance and then verified by running the mock
module suite (the counts above are from an actual run). The API shape decision — opt-in flag rather
than changing equalsExtended — and the set-versus-order tradeoff are my own, and I can explain any
part of the diff.

Adds an opt-in way to match only the request headers a test declares,
instead of requiring the expected headers to be exactly equal to the ones
the client actually sends.

    RequestKey expected = RequestKey.builder(HttpMethod.GET, "/api")
        .headers(RequestHeaders.builder().add("Authorization", "Bearer t").build())
        .matchHeadersBySubset()
        .build();

A request carrying Authorization plus a generated Content-Length and Host
now matches, which was not possible before.

Making subset matching the default would silently weaken every existing
test: a test that declares three headers currently asserts nothing else is
present, and after the change extra headers would be ignored with no
notice. That is a breaking change in assertion strength, so the new
behaviour is a builder flag and the old exact comparison is kept verbatim
when the flag is absent. exactMatchingIsPreservedByDefault pins this.

- RequestHeaders#isSubsetOf: every declared header must be present on the
  actual request with all of its declared values; headers only the actual
  request carries are ignored.
- RequestKey.Builder#matchHeadersBySubset: enables the above for that key.
- RequestKey#equalsExtended: branches on the flag; method, URL, charset and
  body comparison are untouched.

Values are compared as a set (containsAll), so header order is not
significant.

Fixes OpenFeign#1552

Signed-off-by: lxcxjxhx <lxcxjxhx@users.noreply.github.com>
@lxcxjxhx
lxcxjxhx force-pushed the 1552-mock-header-subset branch from 88006c4 to 1cb5276 Compare November 2, 2026 23:59

@velo velo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, opt-in is the right call and the additions are binary compatible. Two things block merging:

  1. The flag is ignored on the verify path. MockClient.verifyTimes (and so verifyOne) calls recordedKey.equalsExtended(requestKey) (MockClient.java:283), and verifyNever does the same (MockClient.java:310). The receiver there is the key built from the incoming request, which always has headerSubset = false. So mockClient.verifyOne(RequestKey.builder(GET, "/api").headers(...).matchHeadersBySubset().build()) still does an exact header comparison and fails when the client added extra headers. Only stubbing (getResponseBuilder, executeSequential) honors the flag. Please make the match symmetric. For example, check headerSubset on either side in equalsExtended, or flip the receiver in the two verify loops. Then add a MockClient-level test that stubs and verifies with matchHeadersBySubset() while the client sends an extra header.

  2. Documentation: mock does have a README. It's mock/README.asciidoc. Please add a short example of matchHeadersBySubset() there next to the existing RequestKey usage.

Nits, not blocking:

  • RequestKeyHeaderSubsetTest: new java.util.ArrayList<>() should use an import.
  • In methodAndUrlAreStillCompared the URL is the same and only the method differs, so the otherUrl name is misleading.
  • The class javadoc reads "Request#1552". It should be "Issue #1552", or just drop it.

@velo

velo commented Nov 9, 2026

Copy link
Copy Markdown
Member

Pushed the review fixes to the branch (merged master first):

  • RequestKey.equalsExtended now honors matchHeadersBySubset() on either side, so verifyOne/verifyTimes/verifyNever work with a subset key, not only stubbing.
  • Added MockClientTest.matchHeadersBySubsetWhenStubbingAndVerifying (client sends an extra Content-Length), plus a reverse-direction case in RequestKeyHeaderSubsetTest.
  • Documented matchHeadersBySubset() with an example in mock/README.asciidoc and added a CHANGELOG entry under 13.18.
  • Nits: imported ArrayList, renamed otherUrl to otherMethod, dropped the "Request#1552" javadoc.
    Thanks for the contribution!

@velo velo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review items addressed; LGTM.

@velo
velo enabled auto-merge (squash) November 9, 2026 20:50
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
@velo

velo commented Nov 9, 2026

Copy link
Copy Markdown
Member

Merged master again; the CHANGELOG entry was dropped because CHANGELOG.md was removed from the repo.

@velo
velo merged commit be25bbb into OpenFeign:master Nov 9, 2026
3 checks passed
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.

feign-mock is too strict with request headers when matching requests

2 participants