Search code examples
wiremockurl-pattern

Wiremock matching path params


API Specs:

GET /customer/{{customerId}}

My Target responses:

  1. Input: "/customer/1234, Output: return 404 error
  2. Input: "/customer/(any value not equal to 1234): Output: 200 with return object

How do I set that any customerId path param not equal or equal to "1234" will have different outcomes?

Seems such a common scenario but I tried looking in this reference link below but can't find any

https://wiremock.org/docs/request-matching/


Solution

  • You can set up two separate mappings with different priorities, so that WireMock will check for the more specific ID first.

    Here's an example where the more specific url has Priority 1, and the less specific url has Priority 2, meaning WireMock checks the more specific url first, and if it does not match, checks the less specific url.

    {
      "mappings": [
        {
          "priority": 1,
          "request": {
            "url": "/customer/1234"
          },
          "response": {
            "status": 404
          }
        },
        {
          "priority": 2,
          "request": {
            "urlPath": "/customer/.*"
          },
            "response": {
                "status": 200,
                "bodyFileName": "customer.json"
            }
        }
      ]
    }