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

MockWs route not applied if user passing query parameters directly in the url #257

Open
gaeljw opened this issue Apr 26, 2022 · 3 comments

Comments

@gaeljw
Copy link
Collaborator

gaeljw commented Apr 26, 2022

When the production code uses code similar to the following:

wsClient
  .url(".../something?someQueryParam=some value")
  .get()

And a test defines MockWS like this:

MockWS {
  case ("GET", ".../something") => Action { ... }
}

Then the mocked route is not called.

It seems the mock only works is user do not define query parameters in the url(..).

While most of the time query parameters are passed via .addQueryStringParameters(..), it's still a valid usage of Play WS and would be nice if supported by MockWS.

@avdv
Copy link
Collaborator

avdv commented Apr 28, 2022

MockWS is just passing in the method and the full URL to the partial function. There's nothing stopping you from inspecting the URL to your hearts content:

def path(url: String) = ... // extract path without query params

MockWS {
  case ("GET", url) if path(url) == ".../something" => Action { ... }
}

To make this a bit more comfortable, you can use the SIRD matchers from Play:

import play.api.routing.sird.UrlContext

MockWS {
  case ("GET", p".../something") => Action { ... }
}

@gaeljw
Copy link
Collaborator Author

gaeljw commented Apr 28, 2022

But when the production code uses the following:

wsClient
  .url(".../something")
  .addQueryStringParameters("someQueryParam" -> "some value")
  .get()

Then MockWS partial function works with only the path ".../something".

It's this behavior that I find surprising. Basically it means that to define the MockWS partial function I need to know on my production code is implemented, which I should not care about.


I didn't know SIRD matchers would work in this context, I'll give it a try.

@bojanbla
Copy link

I have the same issue. It is not possible to match based on query parameters in tests if you add params via the method addQueryStringParameters.

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

3 participants