pokemock

A mock server generated from one or more arbitrary Swagger files. Supports seeding, timeouts, response picking, entity memory, semantic action inference, etc.

Usage no npm install needed!

<script type="module">
  import pokemock from 'https://cdn.skypack.dev/pokemock';
</script>

README

Pokemock

A mock server generated from one or more arbitrary Swagger files. Supports seeding, timeouts, response picking, entity memory, semantic action inference, etc.

Usage

Syntax:
  pokemock <swagger-urls-or-files> ... [-h] [-v] [-w] [-p <port>]

Options:
  -h, --help        Show help
  -v, --version     Show version
  -p, --port <port> Set server port, default is 8000
  -w, --watch       Watch mode: Restart on Swagger changes
  -k, --killable    Publish /kill endpoint to stop the service
      --memory      Enable memory module (experimental)

Server

The mock server listens to the specified port and mocks endpoints defined in the provided Swagger document. Additionally, it publishes a Swagger UI under /ui, the Swagger API under /api-docs and a /kill endpoint for shutdown.

Request Headers

Using optional headers, clients can control the server's behavior:

  • X-Mock-Status
    • Specifies the response status code
    • The correct response is inferred from the API if possible
    • Defaults to the first response code specified in the API
  • X-Mock-Seed
    • Specifies a seed for data generation
    • If omitted, a random seed is generated
    • The current seed is always returned in a X-Mock-Seed response header
  • X-Mock-Time
    • Specifies the minimum response time (milliseconds)
  • X-Mock-Size
    • Specifies array size(s) in the response
    • Must be a valid JSON object of <definitionName|attributeName>: <size> pairs
    • If omitted, array sizes are randomly between 1 and 5
  • X-Mock-Depth
    • Specifies the maximum JSON data depth
    • Defaults to 5
  • X-Mock-Override
    • Specifies response data via JSON Path
    • Must be a valid JSON object of <jsonPath>: <data> pairs
    • <data> is arbitrary JSON
  • X-Mock-Replay
    • Specifies the number of times the current X-Mock-* headers should be replayed
    • The next N requests to the requested URL will replay the current X-Mock-* headers
  • X-Mock-Replay-Pattern
    • Specifies a regular expression to match for X-Mock-Replay
    • If omitted, the exact path is used for replaying

Memory (experimental)

Use the --memory switch to enable the memory module. When enabled, entities containing an ID are remembered by the server. If the entity is requested again, the remembered data is returned. This also applies to sub-entities across endpoints.

Additionally, the server tries to infer semantic actions from requests, such as:

  • Get by id
  • Delete by id
  • Update by id
  • Create new entity

These actions are applied to known entities in memory. For example, requesting a deleted entity will result in a 404 response.

Customization

Pokemock provides a set of Express middlewares which you can use independently. The default app defined in createDefaultApp.js is an opinionated stack of middlewares which you're encouraged to hack on. By re-arranging and adding middlewares (especially generators) you can tailor Pokemock to fit your APIs.