-
Notifications
You must be signed in to change notification settings - Fork 1
Creating a dynamic mock using variables with javascript
When crafting a mock, it's possible to define a template path
featuring variables.
For instance, consider setting your path as:
/api/message/send/:sender/:receiver
With this setup, requests like:
/api/message/send/ross/rachel?isMultiMedia=true
will be directed to this mock.
Particularly significant for dynamic mocks, the path variables
sender and receiver, along with the query parameter
isMultiMedia, will be accessible within the Response Body Content as variables.
To access these variables within the Response Body Content, follow these steps:
- Any path variable can be accessed by prefixing it with
pathVariable___
(e.g. pathVariable + three underscores). - Similarly, any query parameter can be accessed by prefixing it with
queryParameter___
(e.g. queryParameter + three underscores).
Illustrated below is how these variables are utilized in the Response Body Content
:
return JSON.stringify(
{
"message":"Hi there. How are you?",
"location":"IN",
"sender": pathVariable___sender,
"receiver": pathVariable___receiver,
"isMultiMedia": queryParameter___isMultiMedia
}
);
The outcome is depicted through the response, showcasing the application of path variables and query parameters:
This walkthrough demonstrates the implementation of dynamic mocks employing variables, allowing you to dynamically adjust your mock responses based on path variables and query parameters.