WithBody(Func) executes for all requests, ignore path matching #450

Closed
opened 2025-12-29 15:24:03 +01:00 by adam · 5 comments
Owner

Originally created by @antshc on GitHub (Sep 16, 2022).

Hello,
Thank you for this great NuGet package! It is awesome!!

During using the package I was faced with following issue:

I start a single WireMockServer and then create two mappings one for /api/Customer/add and another for /api/Customer/update,
also, I use .WithBody((body) => VerifyCrmRequestBody(TestCustomers.Customer1, body))) for both requests mapping.

When no matter what request send to wiremock WithBody executes for both mappings and ignore .WithPath("/api/Customer/add")

Is it correct behavior?
How can I solve this issue and verify WithBody for only a specific URL path?

Package:

 <PackageReference Include="WireMock.Net" Version="1.5.6" />
    // RestEase http client

    [BasePath("/api/Customer")]
    public interface ICustomerCrmClient
    {
        [Post("add")]
        public Task Add([Body] CustomerCrmCreation customer);

        [Put("update")]
        public Task Update([Body] CustomerCrmCreation customer);
    }

    public void Test()
    {
        var wireMockServer = WireMockServer.Start();
        wireMockServer.Given(Request.Create()
                                   .UsingPost()
                                   .WithPath("/api/Customer/add")
                                   .WithBody((body) => VerifyCrmRequestBody(TestCustomers.Customer1, body)))
                        .RespondWith(WireMock.ResponseBuilders.Response.Create().WithStatusCode(HttpStatusCode.OK));

        wireMockServer.Given(Request.Create()
                                    .UsingPut()
                                    .WithPath("/api/Customer/update")
                                    .WithBody((body) => VerifyRequestBody(TestCustomers.Customer1, body)))
                       .RespondWith(WireMock.ResponseBuilders.Response.Create().WithStatusCode(HttpStatusCode.OK));
    }
Originally created by @antshc on GitHub (Sep 16, 2022). Hello, Thank you for this great NuGet package! It is awesome!! During using the package I was faced with following issue: I start a single WireMockServer and then create two mappings one for /api/Customer/add and another for /api/Customer/update, also, I use ` .WithBody((body) => VerifyCrmRequestBody(TestCustomers.Customer1, body)))` for both requests mapping. When no matter what request send to wiremock WithBody executes for both mappings and ignore .WithPath("/api/Customer/add") Is it correct behavior? How can I solve this issue and verify WithBody for only a specific URL path? Package: ``` xml <PackageReference Include="WireMock.Net" Version="1.5.6" /> ```` ``` c# // RestEase http client [BasePath("/api/Customer")] public interface ICustomerCrmClient { [Post("add")] public Task Add([Body] CustomerCrmCreation customer); [Put("update")] public Task Update([Body] CustomerCrmCreation customer); } public void Test() { var wireMockServer = WireMockServer.Start(); wireMockServer.Given(Request.Create() .UsingPost() .WithPath("/api/Customer/add") .WithBody((body) => VerifyCrmRequestBody(TestCustomers.Customer1, body))) .RespondWith(WireMock.ResponseBuilders.Response.Create().WithStatusCode(HttpStatusCode.OK)); wireMockServer.Given(Request.Create() .UsingPut() .WithPath("/api/Customer/update") .WithBody((body) => VerifyRequestBody(TestCustomers.Customer1, body))) .RespondWith(WireMock.ResponseBuilders.Response.Create().WithStatusCode(HttpStatusCode.OK)); } ```
adam added the question label 2025-12-29 15:24:03 +01:00
adam closed this issue 2025-12-29 15:24:03 +01:00
Author
Owner

@StefH commented on GitHub (Sep 16, 2022):

@khdevnet

When WireMock.Net receives a http request, all mappings are processed in order to find the best match.

So in your case, because the VerifyCrmRequestBody method is used in both mappings, this method is also executed twice because in total you have two mappings.

@StefH commented on GitHub (Sep 16, 2022): @khdevnet When WireMock.Net receives a http request, all mappings are processed in order to find the best match. So in your case, because the `VerifyCrmRequestBody` method is used in both mappings, this method is also executed twice because in total you have two mappings.
Author
Owner

@antshc commented on GitHub (Sep 16, 2022):

Thank you for fast reply,

How does it choose the best match?
Is it uses some kind of score?

Case 1
POST /api/Customer/add
{ some body}
.UsingPost() +1
.WithPath("/api/Customer/add") +1
.WithBody("{ some body}") +1
.RespondWith({ some response body})

Score = 3

Case 2
PUT /api/Customer/update
{ some body}
.UsingPut() +0
.WithPath("/api/Customer/update") +0
.WithBody("{ some body}") +1
.RespondWith({ another response body})

Score = 1

because Case 2 has a score 1 then it skips it and returns the result for score 3 ?
what is the response { some response body } ?

@antshc commented on GitHub (Sep 16, 2022): Thank you for fast reply, How does it choose the best match? Is it uses some kind of score? Case 1 POST /api/Customer/add { some body} .UsingPost() +1 .WithPath("/api/Customer/add") +1 .WithBody("{ some body}") +1 .RespondWith({ some response body}) Score = 3 Case 2 PUT /api/Customer/update { some body} .UsingPut() +0 .WithPath("/api/Customer/update") +0 .WithBody("{ some body}") +1 .RespondWith({ another response body}) Score = 1 because Case 2 has a score 1 then it skips it and returns the result for score 3 ? what is the response { some response body } ?
Author
Owner

@StefH commented on GitHub (Sep 16, 2022):

In your case when you have 2 mappings, the best matching will be returned.

And indeed, the scoring looks like you defined, not 100% correct, but close.

If you want to know the details on the match, see the logging.
See this wiki page:
https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips

@StefH commented on GitHub (Sep 16, 2022): In your case when you have 2 mappings, the best matching will be returned. And indeed, the scoring looks like you defined, not 100% correct, but close. If you want to know the details on the match, see the logging. See this wiki page: https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips
Author
Owner

@mattisking commented on GitHub (Sep 16, 2022):

You can also assign a Priority for each mapping to allow you to prioritize one mapping over another when you match multiple times.

@mattisking commented on GitHub (Sep 16, 2022): You can also assign a Priority for each mapping to allow you to prioritize one mapping over another when you match multiple times.
Author
Owner

@antshc commented on GitHub (Sep 16, 2022):

yes, I used logging to verify that request was sent, great feature!!
Problem solved for me
@mattisking @StefH thank you guys,
you saved tons of my time.
have nice weekend!!

@antshc commented on GitHub (Sep 16, 2022): yes, I used logging to verify that request was sent, great feature!! Problem solved for me @mattisking @StefH thank you guys, you saved tons of my time. have nice weekend!!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#450