Robert jardonneau
Robert jardonneau

Reputation: 444

Wiremock query parameter JSON stub file

I'm trying to mock query parameter using a wiremock JSON stub file.

It works when I do it this way :

{
  "request": {
    "method": "GET",
    "url": "/posts?id=1",
  },
//...
}

However when I change my query parameter to use the dedicated field like this it doesn't work anymore :

{
  "request": {
    "method": "GET",
    "urlPath": "/posts",
    "queryParameters": {
      "id": {
        "equalTo": "1"
      }
    }
  },
  //...
}

Any idea why ?

The test request looks like http://some-host/posts?id=1

Upvotes: 11

Views: 36808

Answers (3)

Sagar Gangwal
Sagar Gangwal

Reputation: 7937

You can try with urlPathPattern instead of urlPath.

As said by here urlPath which is for an exact match, and urlPathPattern is for regex.

So using urlPathPattern in QueryParameter your query get resolve.

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/posts",
    "queryParameters": {
      "id": {
        "equalTo": "1"
      }
    }
  },
  //...
}

Try and understand below concept for Wiremock.

Upvotes: 7

Jon Thoms
Jon Thoms

Reputation: 10739

The issue is that urlPath doesn't work with queryParameters and that this is simply expected behavior. :-/ I found this Q&A on the topic at the Wiremock Github repo. According to @tomakehurst's answer, this is expected behavior and queryParameters will match if you use urlPathPattern.

Upvotes: 3

Ryuzaki L
Ryuzaki L

Reputation: 39978

This works for me, change your "urlPath" to "urlPathPattern" but be careful in structuring this JSON. so urlPath is exact matching pattern, but urlPathPattern is regex matching on path and query parameter

  {
        "request": {
            "urlPathPattern": "/posts",
            "method": "GET",
            "queryParameters": {
                "id": {
                    "equalTo": "1"
                }
            }
        },
        "response": {
            "status": 200,
            "body":"This is successful"
        }
    }

Upvotes: 18

Related Questions