Wiremock urlPattern with query param regex matching

576 Views Asked by At

I have the below wiremock setup & trying to set up the urlPattern matching using regex.

{
  "scenarioName": "CPM Get Entitlements:Error - CPM_10000 - DOWNSTREAM_ERROR - Status 500",
  "request": {
    "method": "GET",
    "urlPattern": "<REGEX>"
  },
  "response": {
    "status": 500,
    "body": "{\"code\": \"CPM_10000\",\"description\": \"some description\"}"
  }
}

My URLs could be any one of these two.

  1. http://localhost:8091/household/7c4c5cb4-ed2e-4fde-a774-66294698c035/entitlements
  2. http://localhost:8091/household/7c4c5cb4-ed2e-4fde-a774-66294698c035/entitlements?includeFutureDated=true

I have tried various REGEX patterns like the one below but non of them works for 2nd URL with query param. I am able to construct REGEX that works in the text editor or in java but it seems not to be working in wiremock config file.

REGEX

  • /household/.+/entitlements(.*)
  • /household/.+/entitlements(\?includeFutureDated=true)
  • /household/.+/entitlements(?includeFutureDated=true)

Is there a way to fix this in wiremock?

1

There are 1 best solutions below

0
agoff On

I find it easier to match on urlPathPattern instead of urlPattern in this case, and then have a separate queryParameters matcher.

{
  "scenarioName": "CPM Get Entitlements:Error - CPM_10000 - DOWNSTREAM_ERROR - Status 500",
  "request": {
    "method": "GET",
    "urlPathPattern": "/househould/.+/entitlements", // this should match your URL
    "queryParameters": {
      "includeFutureDated": {
        "or": [
          { "matches": "true" }, // update this to change your query parameter matching
          { "absent": true }, // this allows for a match when the query parameter is absent
        ],
      },
    },
  },
  "response": {
    "status": 500,
    "body": "{\"code\": \"CPM_10000\",\"description\": \"some description\"}"
  }
}