Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: WithBody can't be used with multiple matching rules? #303

Closed
PkAIR opened this issue Jul 22, 2019 · 7 comments
Closed

Question: WithBody can't be used with multiple matching rules? #303

PkAIR opened this issue Jul 22, 2019 · 7 comments
Assignees
Labels

Comments

@PkAIR
Copy link

PkAIR commented Jul 22, 2019

Stuck with a question: is it possible to use several matching rules for request body?

WithParam(...) from IParamsRequestBuilder as well as WithHeader(...) from IHeadersAndCookiesRequestBuilder support smth like:

foreach (var matcher in ParamMatchers)
{
     request.WithParam(matcher.Key, MatchBehaviour.AcceptOnMatch, matcher.Value.ToArray());
}

since it intakes key.

But for WithBody(...) from IRequestBuilder can't do smth similar.

How can I deal with several matching rules for request body?

@StefH StefH added the question label Jul 22, 2019
@StefH
Copy link
Collaborator

StefH commented Jul 22, 2019

Good point.
I don't even know if multiple matchers is now supported in the interface?
Like:

"Body": {
            "Matchers": [
            {
                "Name": "JsonMatcher",
                "Pattern": "{ \"x\": 42, \"s\": \"text\" }"
            },
            {
                "Name": "JsonMatcher",
                "Pattern": "{ \"x\": 42, \"s\": \"stef\" }"
            }
            ]
        }

I'll have to check the code...

@StefH
Copy link
Collaborator

StefH commented Jul 22, 2019

I've updated the code, can you take a look at the latest version at MyGet?
WireMock.Net.1.0.25-ci-11541.nupkg?

@PkAIR
Copy link
Author

PkAIR commented Jul 22, 2019

@StefH Sorry for late notice. Yeah, will do tomorrow. I'm off for now.

Thank you for such a quick response though!

@PkAIR
Copy link
Author

PkAIR commented Jul 22, 2019

Hi @StefH

Nah, couldn't wait till tmr.

At a first glance I was able to get it working. I succeeded in building a valid mocking template with array of BodyMatchers:

[ { "Guid": "9af78b09-28b5-465a-80d0-c0b3261b2c64", "Scenario": "Test For EnterOrder", "SetStateTo": "1", "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "*/EnterOrder", "IgnoreCase": false } ] }, "Url": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "*/EnterOrder", "IgnoreCase": false } ] }, "Methods": [ "POST" ], "Body": { "Matchers": [ { "Name": "JsonMatcher", "Pattern": "{\"testse\": 213123123}" }, { "Name": "JsonMatcher", "Pattern": "{ \"test\": 2133}" } ] } }, "Response": { "StatusCode": 200, "BodyDestination": "SameAsSource", "Body": "{\"resp\": 231213}", "Headers": { "test": "test" } } } ]

(pay no attention to jsons. I changed them as I could).

But then I noticed such behaviour.

Let's assume I have two body matchers and all of them are for instance JsonMatchers. One of it is totally correct (worked without array introduction) and the second one is some rubbish (as in json from above).

Then after making a request I see it passed even with one 'negative' result of matching as I think. Is it expected or not? Does work like - if at least one matcher succeeded then return given response?

@StefH
Copy link
Collaborator

StefH commented Jul 23, 2019

Currently the logic is for all (~almost all) matchers is that the matchers are used as an OR.
So the best matching score from multiple matchers is used.

If required in future, I can also implement an AND, but for that I need to change the API and the JSON.

@PkAIR
Copy link
Author

PkAIR commented Jul 23, 2019

Got it, thanks. For now I don't see any reason for doing it.

Anyway, I'm happy with this change.

Do you have eta when it will be available in stable?

@StefH
Copy link
Collaborator

StefH commented Jul 23, 2019

In some hours.

@StefH StefH changed the title Question: WithBody can't be used with several matching rules? Question: WithBody can't be used with multiple matching rules? Jul 23, 2019
@StefH StefH self-assigned this Jul 23, 2019
@StefH StefH closed this as completed Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants