Skip to content

Mock Matching Approach

Jordan Walsh edited this page Jul 4, 2024 · 2 revisions

Mock Matching Approach

The mock matching algorithm mirrors much of Postman's supplied approach - https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/matching-algorithm/

The mock server will accept requests that use any path, with any HTTP method. The service then searches the list of responses in the supplied collection that potentially match the inbound request, and it then returns the closest match based based on a series of factors.

The following steps are used to find the best matched response:

  1. HTTP Method - Filter out any responses from the collection that don't match the inbound HTTP method.
  2. x-mock Headers - If an x-mock header was used, filter for only responses that match this header.
  3. URL Match - Filter for responses that match the URL supplied in the request. This matching looks for path matches as well as query parameters and partial matches.
  4. Match Headers - If the x-mock-match-request-headers modifier is used, validate that the responses contain the request header specified.
  5. Match Body - If the x-mock-match-request-body modifier is used, validate that the responses contain a matching request body.

If at the end of this no responses are left, the service will return a 404 Not Found. Otherwise, the best matched response based on a score will be returned.

Mock Matching Improvements

Multiple responses match the supplied x-mock header

If an x-mock header is provided and the collection has multiple responses that match, currently the service will simply return the first one found. To better match the Postman approach it should first order the list of responses that are left by ID and then return the first 2xx response. If none can be found it can then return the first mock response found.