Search code examples
javaarrayswiremock

Wiremock Mapping - use array in one of header values for one of the keys


I want to make mapping for my wiremock server. However I have no idea how to map char array instead of string.

  "request": {
    "urlPathPattern": "/some/url",
    "method": "GET",
    "headers" : {
      "apiKey": {
        "equalTo": "test"
      }
    }

  }

This mapping will stub for string 'test' but i wanna match it to ["t","e","s","t"] Putting it in that, ends up with initializationError -> equalTo operand must be a non-null string


Solution

  • WireMock uses regex matching, so you can use the matches keyword and a pipe character (|) to achieve this.

    {
      "request": {
        "urlPathPattern": "/some/url",
        "method": "GET",
        "headers": {
          "apiKey": {
            "matches": "t|e|s|t"
          }
        }
      },
      "response": {
        "status": 200
      }
    }
    

    regex101's documentation on the | character:

    Matches either what is before the | or what is after it - in this case a or b.