leanovate / play-mockws

Mock WS client for Play Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

gaeljw opened this issue · 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.

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

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.

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.