Would it be possible to dynamically modify responses, by having a callback? The callback, if provided, would receive the current HTTP request, the current Mock and MockResponse, the interpolated chunk and return an eventually modified chunk, headers and status. One single callback would be enough, set as an additional property on the MiddlewareConfiguration class.
This apparently simple change would allow all kinds of dynamic behaviors, like injecting values from the request into the response, call public or private APIs that provide random data, etc. etc.
Having the same functionality available through the docker container would be trickier, the callback would have to be a physical JavaScript file provided through a volume mount. My node.js knowledge doesn't go that far... Maybe the default image could have a dummy callback file that would not make any change to the response items. Maybe it's not even worth to consider, for this kind of scenarios it would be better to just let people create their own docker images.
The proposed code change would have to be applied on MockRequestHandler class (mock.request.handler.ts)
handle(request: http.IncomingMessage, response: http.ServerResponse, next: Function, params: { id: string, mock: Mock }): void {
const _response: MockResponse = this.state.getResponse(params.mock.name, params.id);
if (_response !== undefined) {
const { headers } = _response;
const { status } = _response;
const { then } = _response;
const delay: number = this.state.getDelay(params.mock.name, params.id);
const jsonCallbackName = this.getJsonCallbackName(request);
try {
const chunk = this.getChunk(_response, params, jsonCallbackName);
// call external callback, providing request, _response, params, chunk, overwritting chunk, headers and status with returned values
// ideally, the jsonCallbackName should be applied after the external callback is invoked
// delay could also be allowed to be dynamically changed, why not?
setTimeout(() => {
this.respond(params, then, response, status, headers, chunk);
}, delay);
} catch (e) {
response.writeHead(HttpStatusCode.INTERNAL_SERVER_ERROR, HttpHeaders.CONTENT_TYPE_APPLICATION_JSON);
response.end(JSON.stringify(e, ['message']));
}
} else {
next();
}
}
Let me know if this can be easily applied into the code. This is an amazingly simple tool, covers all my mocking needs except this particular one.
Thank you
Would it be possible to dynamically modify responses, by having a callback? The callback, if provided, would receive the current HTTP request, the current Mock and MockResponse, the interpolated chunk and return an eventually modified chunk, headers and status. One single callback would be enough, set as an additional property on the MiddlewareConfiguration class.
This apparently simple change would allow all kinds of dynamic behaviors, like injecting values from the request into the response, call public or private APIs that provide random data, etc. etc.
Having the same functionality available through the docker container would be trickier, the callback would have to be a physical JavaScript file provided through a volume mount. My node.js knowledge doesn't go that far... Maybe the default image could have a dummy callback file that would not make any change to the response items. Maybe it's not even worth to consider, for this kind of scenarios it would be better to just let people create their own docker images.
The proposed code change would have to be applied on MockRequestHandler class (mock.request.handler.ts)
Let me know if this can be easily applied into the code. This is an amazingly simple tool, covers all my mocking needs except this particular one.
Thank you