WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.35k stars 197 forks source link

Multiple Matchers MatchOperator #1098

Closed mattisking closed 1 month ago

mattisking commented 1 month ago

Is it possible to have a condition that all matches must succeed?

I want to use a path matcher, method matcher, and JsonPartialMatcher and I need all 3 to pass. I may be missing something but it seems the default is Or. I know I can use MatchOperator in some cases but I don’t see an obvious way to make this work across matchers. (Currently I can put in an incorrect uri for that matches as long as the JsonPartialMapper succeeds).

it matters for me because we are using WireMock as a more fully realized set of unit tests… basically integration tests and some of our functionality calls 8 different API’s that I want WireMock to intercept. I appreciate the score based matching, but in my case if the match isn’t 100% I would rather it fail with No Match Found because a single unit test may setup a lot of different request maps.

I can make it work based on a single match for Posts but I’d rather have multiple things that must be true.

StefH commented 1 month ago

If you use separate matchers (path, method and body), these should all be a perfect match, else the mapping will not be chosen. Can you please show the c# code + configuration you use ?

mattisking commented 1 month ago

I put together a sample. Which I can attach later. What I came to realize is that WireMock tries to make the best match it can. If there is no perfect match but there is a partial match, then it will take the highest rated match or the first of a list of equally rated.

So, if I put in just one mock that wants a match in route and a match on method, then make a call where method fails to match but path does match, it does return me the one I setup, but I’d expect the 500 No Match response because other wise 2 endpoints, one with a Get and one with a Post will both match the Post I setup in Wiremock.

Now, if our mocked both the get and the post it would return the correct one, but I only mocked one as long as there’s a partial match I get the mocked data which would be an issue for me in some cases. Generally I’ll have the perfect match mocked and this won’t be an issue.

But you can create this same thing by mocking a route using a GET and then making a POST. As long as the path matches, if you only defined the GET your POST will respond with your GET mock.

StefH commented 1 month ago

A quick question, did you set https://github.com/WireMock-Net/WireMock.Net/wiki/Settings#allowpartialmapping to true ?

mattisking commented 1 month ago

😂😂😂😂😂

mattisking commented 1 month ago

Nothing to see here. Move along. 😂 Thanks