Exact byte array request matching fails on specific byte arrays #344

Closed
opened 2025-12-29 15:21:25 +01:00 by adam · 3 comments
Owner

Originally created by @Archomeda on GitHub (Apr 12, 2021).

Originally assigned to: @StefH on GitHub.

Describe the bug

Exact matching on byte arrays sometimes fails, depending on the byte array contents.

Expected behavior:

Exact matching should succeed, no matter what the byte array contents are, as long as it's the same.

Test to reproduce

The following code reproduces the bug:

static async Task Main(string[] args)
{
    await DoStuff(new byte[] { 201, 202, 203 });
    await DoStuff(new byte[] { 49, 50, 51 });
}

static async Task DoStuff(byte[] bytes)
{
    using var server = WireMockServer.Start();
    using var httpClient = new HttpClient { BaseAddress = new Uri(server.Urls[0]) };

    server
        .Given(Request.Create()
            .WithPath("/test")
            .WithBody(bytes)
            .UsingPost())
        .RespondWith(Response.Create()
            .WithSuccess()
            .WithBody("success"));

    using var content = new ByteArrayContent(bytes);
    var response = await httpClient.PostAsync("test", content);

    Console.WriteLine($"{response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
}

The output:

OK - success
NotFound - {"Status":"No matching mapping found"}

From my limited debugging, I've noticed that the request log shows different behavior. In the first case, it shows that DetectedBodyType = Bytes, while in the second case it's DetectedBodyType = String. In both cases the DetectedBodyTypeFromContentType = Bytes.

I don't expect WireMock to transform a request to a string automatically and then fail to match because it expects a byte array.

Originally created by @Archomeda on GitHub (Apr 12, 2021). Originally assigned to: @StefH on GitHub. ### Describe the bug Exact matching on byte arrays sometimes fails, depending on the byte array contents. ### Expected behavior: Exact matching should succeed, no matter what the byte array contents are, as long as it's the same. ### Test to reproduce The following code reproduces the bug: ```cs static async Task Main(string[] args) { await DoStuff(new byte[] { 201, 202, 203 }); await DoStuff(new byte[] { 49, 50, 51 }); } static async Task DoStuff(byte[] bytes) { using var server = WireMockServer.Start(); using var httpClient = new HttpClient { BaseAddress = new Uri(server.Urls[0]) }; server .Given(Request.Create() .WithPath("/test") .WithBody(bytes) .UsingPost()) .RespondWith(Response.Create() .WithSuccess() .WithBody("success")); using var content = new ByteArrayContent(bytes); var response = await httpClient.PostAsync("test", content); Console.WriteLine($"{response.StatusCode} - {await response.Content.ReadAsStringAsync()}"); } ``` The output: ``` OK - success NotFound - {"Status":"No matching mapping found"} ``` ### Other related info From my limited debugging, I've noticed that the request log shows different behavior. In the first case, it shows that DetectedBodyType = Bytes, while in the second case it's DetectedBodyType = String. In both cases the DetectedBodyTypeFromContentType = Bytes. I don't expect WireMock to transform a request to a string automatically and then fail to match because it expects a byte array.
adam added the bug label 2025-12-29 15:21:25 +01:00
adam closed this issue 2025-12-29 15:21:25 +01:00
Author
Owner

@StefH commented on GitHub (Apr 16, 2021):

The different DetectedBodyType should not cause this error I think.

I'll take a look what's the root cause.

Thank you for noticing this issue.

@StefH commented on GitHub (Apr 16, 2021): The different DetectedBodyType should not cause this error I think. I'll take a look what's the root cause. Thank you for noticing this issue.
Author
Owner

@StefH commented on GitHub (Apr 18, 2021):

New version will be released soon.

@StefH commented on GitHub (Apr 18, 2021): New version will be released soon.
Author
Owner

@Archomeda commented on GitHub (Apr 19, 2021):

Cool, thanks! 👍

@Archomeda commented on GitHub (Apr 19, 2021): Cool, thanks! 👍
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#344