Search code examples
wiremockwiremock-standalone

Implement different response with WireMock when no request(s) match


I'm trying to stub a RESTful API. One of the resources return the details when the resource is (indeed) found, or an HTTP 404 (Not Found) when, eventually, there is no resource for the given URL.

This is my simplified stub/mapping:

{
  "mappings": [
    {
      "name": "Retrieve Items",
      "request": {
        "headers": {
          "accept": {
            "caseInsensitive": true,
            "equalTo": "application/json"
          }
        },
        "method": "GET",
        "urlPathPattern": "/api/items/[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}"
      },
      "response": {
        "bodyFileName": "items-api/responses/retrieve/{{ request.pathSegments.[2] }}.json",
        "headers": {
          "content-type": "application/json"
        },
        "status": 200
      }
    }
  ]
}

Then I have several JSON files (in /home/wiremock/__files/items-api/responses/retrieve/ to match the requests against — but I can't find a way to implement the HTTP 404 (Not Found) scenario:

{
  "timestamp": {{ now }},
  "status": 404,
  "error": "Not Found",
  "message": null,
  "path": "{{ request.path }}"
}

With this config I get back (the expected, but not useful for my use case) response from WireMock that the file name uuid-sent-in-request.json is not found.

Is there a way to implement this behavior currently?


Solution

  • Tom's answer will work as well. I think the benefits to his solution are that they aren't tied to specific request URLs, but my suggestion is to have a specific mapping for the files that will match with their specific JSON files, and a catch-all mapping for un-matched files. By assigning the requests with JSON responses a higher priority, WireMock will check those first, and if the request does not match any of the values specified in that mapping, will then go on to check if the second mapping matches, and return a 404.

    {
      "mappings": [
        {
          "name": "Retrieve Items - Success",
          "priority": 1, // arbitrary number lower than the second priority
          "request": {
            "headers": {
              "accept": {
                "caseInsensitive": true,
                "equalTo": "application/json"
              }
            },
            "method": "GET",
            "urlPathPattern": "/api/items/(UUID1|UUID2|UUID3|UUID4)"
          },
          "response": {
            "bodyFileName": "items-api/responses/retrieve/{{ request.pathSegments.[2] }}.json",
            "headers": {
              "content-type": "application/json"
            },
            "status": 200
          }
        },
        {
          "name": "Retrieve Items - 404 Not Found",
          "priority": 5, // arbitrary number that is higher than 1
          "request": {
            "headers": {
              "accept": {
                "caseInsensitive": true,
                "equalTo": "application/json"
              }
            },
            "method": "GET",
            "urlPathPattern": "/api/items/[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}"
          },
          "response": {
            "status": 404
          }
        }
      ]
    }