POST
Request json athttp://localhost:8888/customerData
{
"config": [
{
"components": [
"CUSTOMER_DATA_SCENARIO_1"
]
}
],
"inputParams": {
"paginationRequest": {
"currentPage": 1,
"pageSize": 100
}
}
}
Create a new javascript file
anyname_of_your_choice.resolver.js
insrc/resolver/provider
folder
Implement a
resolve
function like below , you can also use theJsonPathBuilder
andJsonPathUtil
to condtionally check the request and based on that either return arouteKey
ornull
const { JsonPathBuilder, JsonPathUtil } = require("../../util/jsonpath.util");
exports.CustomerDataResolver = function () {
return {
resolve: function (request, response) {
let requestBody = request.body;
let builder = new JsonPathBuilder();
let path = builder
.arraynode("config")
.select(0)
.arraynode("components")
.select(0)
.build();
let jsonPathUtil = new JsonPathUtil();
let data = jsonPathUtil.search(requestBody, path)
return data[0] === 'CUSTOMER_DATA_SCENARIO_1' ? 'CUSTOMER_DATA_SCENARIO_1' : null;
}
}
}
Optionally a
process
function like below where you can modify data based on some conditions or checks.
const { uuidv4 } = requires("../../lib/helper/uuid.helper")
exports.CustomerDataResolver = function () {
return {
// omitted for the better readability
process: function (data, request, response) {
data.transactionId = uuidv4();
}
}
}
Optionally a
responseType
in case the return type isstring
, defualt type isjson
๐.
const { uuidv4 } = requires("../../lib/helper/uuid.helper")
exports.CustomerDataResolver = function () {
return {
// omitted for the better readability
responseType: 'string'
}
}
Optionally a
responseHeaders
in case if you want to send some weird headers for fun ๐.
const { uuidv4 } = requires("../../lib/helper/uuid.helper")
exports.CustomerDataResolver = function () {
return {
// omitted for the better readability
responseHeaders: {
"X-custom-header-test": "Blablabla"
}
}
}
Register the resolver in
resolver.registry.js
created inStep 1
atsrc/resolver/core/resolver.registry.js
along with the HttpMethod i.e. either'POST'
or'GET'
const {RequestResolver} = require("../../lib/resolver/request.resolver");
// omitted for the better readability
const {CustomerDataResolver} = require("../provider/customerData.resolver");
exports.init = function () {
let resolver = new RequestResolver();
// omitted for the better readability
registerCustomerDataResolver(resolver);
return resolver;
}
function registerTestResolver(resolver){
// omitted for the better readability
}
function registerAnotherTestResolver(resolver){
// omitted for the better readability
}
function registerCustomerDataResolver(resolver) {
let customerDataResolver = new CustomerDataResolver();
resolver.register('POST', '/customerData', customerDataResolver);
}
Whatever the return value we have given for the resolver's resolve in our case
CUSTOMER_DATA_SCENARIO_1
inStep 1
we need to map that value inmockey-route.json
atsrc/route/mockey-route.json
with key as the returned stringCUSTOMER_DATA_SCENARIO_1
and value as the path where the mocked response resides for examplesrc/response/customerData/customerData.json
{
"404": "404.json",
"TEST": "/test/test.json",
"ANOTHER_TEST": "/test/anotherTest.json",
"CUSTOMER_DATA_SCENARIO_1": "/customer/customerData.json"
}
Now we can create a file named customerData.json at
src/response/customer/customerData.json
and add the mocked response which will then be sent back whenever we invoke aPOST
request tohttp://localhost:8888/customerData
We can mock the response delay in
mockey-config.json
atsrc/config/mockey-config.json
default value is 5000 millis (5 seconds)
{
"name" : "App Name",
"responseDelayInMillis" : 5000
}
For a response to have different delay other than config we can configure it using "responseDelayInMillis" in the response body.
see below example of customerData.json
{
"responseDelayInMillis": 3000,
"data": {}
}
refer
anotherTest.json
atsrc/response/test/anotherTest.json
for the complete response.