WithPath and With(Method) ignored when WithBody is used #646

Closed
opened 2025-12-29 15:29:38 +01:00 by adam · 1 comment
Owner

Originally created by @paul-green on GitHub (Nov 12, 2024).

Originally assigned to: @StefH on GitHub.

Describe the bug

.WithBody is always triggered even though the Url and Method is completely different to what has been specified

Expected behavior:

WithBody only executes when the Path/Url/Method match

Test to reproduce

using System.Net.Http.Json;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Util;

var client = new HttpClient();
var server = WireMockServer.Start();

server.Given(
        Request.Create().WithPath($"/v1/customer")
        .UsingPut()
        .WithBody((IBodyData? body) =>
        {
            ArgumentNullException.ThrowIfNull(body);
            return true;
        }))
        .RespondWith(Response.Create().WithStatusCode(200));

var updateCustomerResponse = await client.PutAsJsonAsync($"http://localhost:{server.Port}/v1/customer", 
    new
    {
        customerId="1234",
        customerName = "John"
    });
updateCustomerResponse.EnsureSuccessStatusCode();

// This also causes the WithBody section to be triggered - ignoring the Method and Path entirely.
var someGetRequest = await client.GetAsync($"http://localhost:{server.Port}/v1/balance");

Provide additional information if any.

Originally created by @paul-green on GitHub (Nov 12, 2024). Originally assigned to: @StefH on GitHub. ### Describe the bug .WithBody is always triggered even though the Url and Method is completely different to what has been specified ### Expected behavior: WithBody only executes when the Path/Url/Method match ### Test to reproduce ```csharp using System.Net.Http.Json; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; using WireMock.Util; var client = new HttpClient(); var server = WireMockServer.Start(); server.Given( Request.Create().WithPath($"/v1/customer") .UsingPut() .WithBody((IBodyData? body) => { ArgumentNullException.ThrowIfNull(body); return true; })) .RespondWith(Response.Create().WithStatusCode(200)); var updateCustomerResponse = await client.PutAsJsonAsync($"http://localhost:{server.Port}/v1/customer", new { customerId="1234", customerName = "John" }); updateCustomerResponse.EnsureSuccessStatusCode(); // This also causes the WithBody section to be triggered - ignoring the Method and Path entirely. var someGetRequest = await client.GetAsync($"http://localhost:{server.Port}/v1/balance"); ``` ### Other related info Provide additional information if any.
adam added the question label 2025-12-29 15:29:38 +01:00
adam closed this issue 2025-12-29 15:29:38 +01:00
Author
Owner

@StefH commented on GitHub (Nov 13, 2024):

@paul-green

I understand your issue, however this is by design.

All conditions are evaluated, there is no "stop on first mismatch" logic.

@StefH commented on GitHub (Nov 13, 2024): @paul-green I understand your issue, however this is by design. All conditions are evaluated, there is no "stop on first mismatch" logic.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#646