How to match request parameters in wire-mock url?
Asked Answered
D

3

6

I'm trying to create a mock server using wire-mock and I'm facing the following problem: I want to hit an URL like this /customers?customerId={customerId}&customerNo={customerNo}.

My question is how can I match the request parameters customerId and customerNo from the stub of the mock server in Java code.

EDIT

After the first response, this is the result:

enter image description here

EDIT 2

Here is my stub:

WireMockServer mockServer = new WireMockServer(8079);
mockServer.start();
mockServer.stubFor(get(urlEqualTo("/api/loan/admin/contracts"))
                .withQueryParam("status", equalTo("ACTIVE"))
                .withQueryParam("cnp", equalTo("1950503410033"))
                .willReturn(aResponse().withBody("Welcome to Baeldung!")));
Dobsonfly answered 29/3, 2018 at 6:38 Comment(0)
D
0

My problem was that the WireMock is hardcoded in UTF-8 format, so when firing requests from browser I didn't send the UTF-8 format to the endpoint. You can't change the WireMock to accept anything instead of UTF-8, but you can fire some UTF-8 requests.

Dobsonfly answered 29/3, 2018 at 8:28 Comment(0)
D
4

Query parameters can be passed in URL.

In Java:

urlEqualTo("/your/url?and=query")

Json:

{
  "request": {
    "url": "/your/url?and=query"
    ...
  },
  ...
}

Reference: http://wiremock.org/docs/request-matching/

Example: Try any of the following:

stubFor(any(urlEqualTo("/customers?customerId={your_customer_id}&customerNo={your_customer_no}"))
          .willReturn(aResponse()));


stubFor(any(urlPathEqualTo("/customers"))
          .withQueryParam("customerId", equalTo("your_customer_id"))
          .withQueryParam("customerNo", equalTo("your_customer_no"))
          .willReturn(aResponse()));
Dionnadionne answered 29/3, 2018 at 7:9 Comment(6)
Can you help me giving an example in case of my URL? I've read the docs, but I didn't understand very well how to manage with it. Thx.Dobsonfly
I've edited the question. I've do as you suggest, but it seems that it doesn't work.Dobsonfly
How did you define your stub?Dionnadionne
Your cnp looks different than the one defined in stubDionnadionne
Trust me that I've tried with the same cnp query param. It's ok.Dobsonfly
@DinaBogdan I see. You probably need to use 'urlPathEqualTo' instead of 'urlEqualTo' if you're defining parameters separately using 'withQueryParam'Dionnadionne
D
0

My problem was that the WireMock is hardcoded in UTF-8 format, so when firing requests from browser I didn't send the UTF-8 format to the endpoint. You can't change the WireMock to accept anything instead of UTF-8, but you can fire some UTF-8 requests.

Dobsonfly answered 29/3, 2018 at 8:28 Comment(0)
J
0

Stubbing with Query Parameters

stubFor(post(urlPathMatching("url"))
.withHeader(CONTENT_TYPE, matching(APPLICATION_JSON_VALUE))
.withQueryParams(queryParams)
.withRequestBody(equalToJson("request-json-goes-here"))
.willReturn(aResponse().withStatus(200)
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.withBody("response-json-goes-here")))

Where query params is

Map<String, StringValuePattern> queryParams = new HashMap<String, StringValuePattern>();
queryParams.put("show_all", equalTo("true"));

Verification of the query parameter can be done using withQueryParam

verify(1, postRequestedFor(urlPathMatching("url"))
.withQueryParam("show_all",equalTo("true")))
Jillion answered 6/6, 2023 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.