mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
WithBody(Func) executes for all requests, ignore path matching #450
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 @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:
@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
VerifyCrmRequestBodymethod is used in both mappings, this method is also executed twice because in total you have two mappings.@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 } ?
@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
@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.
@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!!