MockHttp icon indicating copy to clipboard operation
MockHttp copied to clipboard

Ambiguity when mocking RequestURI with QueryString

Open jesperj-apmm opened this issue 3 years ago • 1 comments

Hey,

Thank you for writing this awesome library !

While using it I ran into a fun challenge :)

While mocking a Http Request with RequestURI and QueryString, like this:

_mockHttp = new MockHttpHandler();
        _mockHttp.When( matching => matching
                .Method("GET")
                .RequestUri("https://magic.rest.api.com/awesomepath")
                .QueryString("q=name:<name>&sort=desc")
            .Respond( with => with
                .StatusCode(200)
                .Body(<json string>)
                .ContentType("application/json"))
            .Verifiable();

The request matcher does not match the request even though the InvokedRequest and the Matcher URI and QueryString, combined is a perfect match.

To make the request work, I need to add '*' at the end of the URI, like this:

....
                .Method("GET")
                .RequestUri("https://magic.rest.api.com/awesomepath*")
                .QueryString("q=name:<name>&sort=desc")
....

When I looked through the code, here: https://github.com/skwasjer/MockHttp/blob/a1c7a909d2a588fb4bd9ec0dec6c147150f6c462/src/MockHttp/Extensions/RequestMatchingExtensions.cs#L35, I see that the builder does not pass the allowWildCard bool and it's defaulted to true, but I'll write a few test to verify that assumption.

I'll be happy to give it a try and create a PR with a fix if this is not intended behaviour.

jesperj-apmm avatar Dec 27 '22 16:12 jesperj-apmm

Thanks for inquiring.

This is by design (historically that is, before .QueryString() existed). Feel free to offer a PR but we have to be careful with the change in behavior as this may break existing (test) projects.

skwasjer avatar Dec 31 '22 16:12 skwasjer