Dynamic Mock - Overview¶
Code Guide and API Documentation¶
There are four global objects available in the code:
req- an object which represents an HTTP request sent to SmartMock.io
res- an object which represents an HTTP response sent back by SmartMock.io
console- logger, which may be used for debugging purposes. Its output is visible in the Request log.
In some use cases, to model real system behavior, it's necessary to maintain a server-side state between various mocks executions.
SmartMock.io addresses this kind of use case with a global State (
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
stateand 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 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.
Since Dynamic Mock evaluates your code, there may be some problems during the execution of that code:
- Invalid code syntax and accessing
- 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.