mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Specifying .WithBody(byte[]) fails to match for JSON bodies #708
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @smfields on GitHub (Jul 27, 2025).
Describe the bug
When using the overload of
.WithBody()that accepts abyte[]it will fail to match on requests that have a matching body if the bytes happen to correspond to valid JSON.The behavior is inconsistent as it will correctly match on arbitrary strings that have been encoded and provided as bytes, but it will fail to match if the string corresponds to valid JSON, regardless of the content type specified on the request.
Expected behavior:
WireMock should match requests that have a body that matches the provided bytes, regardless of what those bytes represent.
Test to reproduce
See repro: https://github.com/smfields/WiremockIssueRepro/tree/master
Other related info
Seems like the issue probably stems from this section of
BodyDataMatchScoreCalculator.cs.When the matcher is an
ExactObjectMatcherit will compare the value against the bytes only if the detected body type is bytes, string, or form. When the detected body type is JSON we drop down to the next section that compares the provided value against theBodyAsJson, but since the provided value is bytes this doesn't line up.Seems like we need to either open up the comparison on line 41 to also include times when the provided value is of type
byte[], or we need to parse the bytes into JSON before doing the comparison againstBodyAsJson.@smfields commented on GitHub (Jul 27, 2025):
Might be as easy as what I've started in https://github.com/wiremock/WireMock.Net/pull/1339, but more testing would be needed to confirm.