play-mockws icon indicating copy to clipboard operation
play-mockws copied to clipboard

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

Open gaeljw opened this issue 2 years ago • 3 comments

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.

gaeljw avatar Apr 26 '22 20:04 gaeljw

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 { ... }
}

avdv avatar Apr 28 '22 14:04 avdv

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.

gaeljw avatar Apr 28 '22 15:04 gaeljw

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.

bojanbla avatar Jan 22 '24 11:01 bojanbla