10

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

Robert jardonneau
  • 434
  • 2
  • 4
  • 14

3 Answers3

14

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"
        }
    }
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
7

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38
3

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.

entpnerd
  • 10,049
  • 8
  • 47
  • 68