3

How can I make query parameter(pageSize) in below json as optional while using wiremock

{
"request": {
    "method": "GET",
    "urlPath": "/claims-search",        
    "queryParameters" : {
        
        "pageSize" : {
            "equalTo" : "10"
        },
        "pageNumber" : {
            "equalTo" : "2"
        }
    }       
},
"response": {
    "status": 200,
    "bodyFileName": "response_200.json",
    "headers": {
      "Content-Type": "application/json"
    }
Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
vinay
  • 33
  • 3

2 Answers2

5

If you don't care about what value the query parameters have, you can simply exclude them.

If you need them under certain circumstances, you can use the Or operator to include an absent flag. In your case, it'd look something like...

{ 
    "request": { 
        "method": "GET", 
        "urlPath": "/claims-search",
        "queryParameters" : {
            "pageSize" : {
                "or": [{
                    "equalTo" : "10"
                }, {
                    "absent": true
                }]
            },
            "pageNumber" : {
                "or": [{
                    "equalTo" : "10"
                }, {
                    "absent": true
                }]
            }
        }       
    },
    "response": {
        "status": 200,
        "bodyFileName": "response_200.json",
        "headers": {
          "Content-Type": "application/json"
        }
    }
}

I think this functionality was introduced in WireMock 2.29.0.

agoff
  • 5,818
  • 1
  • 7
  • 20
0

If you want to test if the query param is not then you can use

import static com.github.tomakehurst.wiremock.client.WireMock.absent;

postRequestedFor(urlPathMatching("your Url Regex")).withQueryParam("pageSize", absent())

If you want to test the query param is present then we have couple of options

postRequestedFor(urlPathMatching("your Url Regex")).withQueryParam("pageSize", equalTo(10))

or matching against regex

postRequestedFor(urlPathMatching("your Url Regex")).withQueryParam("pageSize", matching("[0-9]+"))

If we have to match value only if exists and otherwise accept missing as well, then use or

import static com.github.tomakehurst.wiremock.client.WireMock.or;

postRequestedFor(urlPathMatching("your Url Regex")).withQueryParam("pageSize", or(absent(),matching("[0-9]+")))
Sanjay Bharwani
  • 3,317
  • 34
  • 31