Skip to content

Specifying .WithBody(byte[]) fails to match for JSON bodies #1338

@smfields

Description

@smfields

Describe the bug

When using the overload of .WithBody() that accepts a byte[] 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 ExactObjectMatcher it 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 the BodyAsJson, 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 against BodyAsJson.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions