Skip to content

Dynamic Mock - Overview

Dynamic Mock is the most flexible way of defining mocks. You write JavaScript code that sets HTTP response fields (status, headers, body) to values required by your use case. supports most features of the ES2015 specification. Besides standard JavaScript, runtime provides a bunch of utility libraries that help you to create more concise and smarter code. Code submitted by you is executed safely, and separates client code executions from each other.

Code Guide and API Documentation

There are four global objects available in the code:

  • req - an object which represents an HTTP request sent to
  • res - an object which represents an HTTP response sent back by
  • console - logger, which may be used for debugging purposes. Its output is visible in the Request log.
  • state - a JavaScript object, which may be modified by code. It holds user-defined data.

Runtime Versions

Stateful Responses

In some use cases, to model real system behavior, it's necessary to maintain a server-side state between various mocks executions. addresses this kind of use case with a global State (state) variable. State acts like a cache, which holds user-defined data and is shared between mocks in the same workspace.

  • State is propagated to code with a global variable state and exposes a set of methods (see below) which allow access to State data or to mutate the State.
  • State is workspace-level object, i.e. all mocks inside a single workspace share it.
  • Direct assignment to State is ignored, i.e. does not change the State value (for example: state = 'aaaaa' - this does not work. Use State methods instead.)
  • Objects set to the State must be serializable to a JSON string, i.e. cannot contain cyclic references.
  • Non-data values set to the state (like functions) are ignored.
  • If request ends with an error (for example, due to incorrect code syntax) changes made to the State up to the error line are not propagated, i.e. the workspace State does not change.

State Limits

  • State may not contain more than 1000 distinct keys. An error is raised when trying to set more than 1000 distinct keys in the State.
  • All values set to the State should not be larger than 100KB after serialization to JSON. If the serialized state is larger than 100KB, an error is raised.

Error Handling

Since Dynamic Mock evaluates your code, there may be some problems during the execution of that code:

  • Invalid code syntax and accessing undefined variables
  • Incorrect usage of state or built-in libraries
  • Too long a time spent for execution (your code has 2 seconds to evaluate)
  • Internal errors caused by a too high memory footprint

For most of the cases described above, mock returns an HTTP 500 status with a generic error message. However, more helpful messages can be found in then Request Logs.

Last update: August 24, 2020