Repository navigation
feign-mock: allow matching request headers by subset - #3661
Conversation
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>
88006c4 to
1cb5276
Compare
velo
left a comment
There was a problem hiding this comment.
Thanks, opt-in is the right call and the additions are binary compatible. Two things block merging:
-
The flag is ignored on the verify path.
MockClient.verifyTimes(and soverifyOne) callsrecordedKey.equalsExtended(requestKey)(MockClient.java:283), andverifyNeverdoes the same (MockClient.java:310). The receiver there is the key built from the incoming request, which always hasheaderSubset = false. SomockClient.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, checkheaderSubseton either side inequalsExtended, or flip the receiver in the two verify loops. Then add aMockClient-level test that stubs and verifies withmatchHeadersBySubset()while the client sends an extra header. -
Documentation:
mockdoes have a README. It'smock/README.asciidoc. Please add a short example ofmatchHeadersBySubset()there next to the existingRequestKeyusage.
Nits, not blocking:
- RequestKeyHeaderSubsetTest:
new java.util.ArrayList<>()should use an import. - In
methodAndUrlAreStillComparedthe URL is the same and only the method differs, so theotherUrlname is misleading. - The class javadoc reads "Request#1552". It should be "Issue #1552", or just drop it.
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
|
Pushed the review fixes to the branch (merged master first):
|
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
|
Merged master again; the CHANGELOG entry was dropped because CHANGELOG.md was removed from the repo. |
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.
A request carrying
Authorization: Bearer tokenplus a generatedContent-LengthandHostnowmatches, 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 actualrequest 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 areuntouched.
Values are compared as a set (
containsAll), so header order is not significant. Tell me if youwould rather have strict ordering and I will switch to
List#equals.Tests
RequestKeyHeaderSubsetTest, 7 cases:Full module regression, unchanged tests included:
validate-code-formatpasses.Documentation
Both new methods carry Javadoc. This PR does not add
mock/README.md: the AGENTS.md READMErequirement applies to new modules, and
mockis 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 anypart of the diff.