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

Respond to exact url with querystring #37

Closed
henninga opened this issue Jun 28, 2017 · 3 comments
Closed

Respond to exact url with querystring #37

henninga opened this issue Jun 28, 2017 · 3 comments

Comments

@henninga
Copy link

henninga commented Jun 28, 2017

Hi

How can I make sure to only respond when the exact url with querystring is called.
Given these variations, the response is always given.

_mockHttpHandler
       .When($"myurl/mydata")
       .WithQueryString("query", query)
       .Respond(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(_mydata) }));

 _mockHttpHandler
        .When($"myurl/mydata?query=foo")
        .WithQueryString("query", query)
        .Respond(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(_mydata) }));

and the requested url is
url = "myurl/mydata?query=foo&other=bar"

I believe the culprit is this line,
https://github.com/richardszalay/mockhttp/blob/master/RichardSzalay.MockHttp.Shared/Matchers/QueryStringMatcher.cs#L44

@richardszalay
Copy link
Owner

The WithQueryString selector ensures that all of the supplied querystring values are present in the requested URL, but allows the URL to have additional querystring parameters beyond those specified.

As a temporary workaround, if you can rely on querystring order, you can specify a custom matcher:

_mockHttpHandler
       .When($"myurl/mydata")
       .With(req => req.RequestUri.Query == "?query=foo&other=bar")
       .Respond(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(_mydata) }));

Otherwise, I'd happily accept a PR for a new WithExactQueryString that passes in exact: true to the QueryStringMatcher constructor (updated to support this scenario). If you go in this direction, just don't forget the unit tests!

@richardszalay
Copy link
Owner

More on the PR direction above, the QueryStringMatcherTests.Should_not_fail_for_additional_keys would become Should_not_fail_for_additional_keys_when_exact_is_false and you would add a new test for Should_fail_for_additional_keys_when_exact_is_true.

@richardszalay
Copy link
Owner

This was just released in 3.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants