vendredi 31 juillet 2020

wiremock stub with query params not working

I am trying to match this url path -

/abc-service/abc?param1=value1&param2=VALUE_2

This gets matched with a json file as below

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/abc-service/abc(.*)"
  },
  "response": {
    "headers": {
      "Content-Type": "application/json"
    },
    "jsonBody": [],
    "status": 200
  }
}

But if I remove this and use stubFor() as below, it doesn't work.

WireMock.stubFor(
        WireMock.get(WireMock.urlPathMatching("/abc-service/abc(.*)"))
                .willReturn(okJson("[]")));

I even tried adding query params as below, that doesn't work either.

final Map<String, StringValuePattern> stringStringValuePatternMap = new HashMap<>();
stringStringValuePatternMap.put("param1", matching("value1"));
stringStringValuePatternMap.put("param2", matching("VALUE2"));
WireMock.stubFor(
        WireMock.get(WireMock.urlPathEqualTo("/abc-service/abc"))
                .withQueryParams(stringStringValuePatternMap)
                .willReturn(
                        aResponse()
                                .withStatus(HttpStatus.OK.value())
                                .withHeader(
                                        HttpHeaders.CONTENT_TYPE,
                                        MediaType.APPLICATION_JSON_VALUE)));

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire