Handling Request Mismatch

Overview

It can happen that an HTTP Request doesn’t match any of the matching rules defined in the simlets for a simulation.

In that case API Simulator uses a default, catch-all simlet when it receives a request it cannot match. What the default simlet returns is configurable.

Also, API Simulator can be configured to forward unmatched requests to their actual destination and the live responses passed back to the client instead of returning a default output.

Default Simlet

The out-of-the-box default simlet for 'on request mismatch' situation returns HTTP 404 Not Found status and a canned text in the body as if it is defined as follows:

apiSim = httpApiSimulation("my-simulation",
   withConfiguration()
      .onRequestMismatch(defaultSimlet()
         .reply(httpResponse()
            .withStatus(NOT_FOUND)
            .withHeader(CONTENT_TYPE, "application/text; charset=UTF-8")
            .withBody("API Simulator couldn't find a matching simlet for the request.")
         )
      )
);

The response can be customized just by defining a different reply. For example:

apiSim = httpApiSimulation("SimulationWithCustomDefaultSimlet",
   withConfiguration()
      .onRequestMismatch(defaultSimlet("myDefaultSimlet")
         .reply(httpResponse(GONE))
      )
);

It can return anything, including 200 OK. Here is an example for when API Simulator is also customized:

apiSim = httpApiSimulation("my-simulation",
   usingApiSimulator().onPort(8080).instance(),
   withConfiguration()
      .onRequestMismatch(defaultSimlet()
         .reply(httpResponse(200)
            .withHeader("X-Reply-By", "API Simulator")
         )
      )
);

Request Forwarding

Unmatched requests can be forwarded to their actual destination and the live responses passed back to the client instead of returning a default output. For this to work, requests must have the destination address in Host header or as part of the request URI.

Here is how to configure forwarding of unrecognized requests:

apiSim = httpApiSimulation("ForwardRequestOnMismatchSimulation",
   withConfiguration().onRequestMismatch(forwardRequest())
);

We would love to hear your feedback! Shoot us a quick email to [feedback at APISimulator.com] to let us know what you think.

Happy API Simulating!