Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function stubServerResponse(
request: IncomingMessage,
onEnd: ShotCallback,
): ServerResponse {
const stub = new ShotResponse(request, onEnd);
// Hacky workaround for Express, see
// https://github.com/expressjs/express/blob/4.16.3/lib/middleware/init.js
// https://github.com/hapijs/shot/issues/82#issuecomment-247943773
// https://github.com/jfhbrook/pickleback
Object.assign(stub, ShotResponse.prototype);
return stub;
}
function inject (dispatchFunc, options, callback) {
options = (typeof options === 'string' ? { url: options } : options);
Assert(typeof dispatchFunc === 'function', 'Invalid dispatch function');
Joi.assert(options, schema);
const req = new Request(options);
const res = new Response(req, callback);
// Bind the req and res methods, as Express sets the prototype internally.
req._read = Request.prototype._read.bind(req);
req.destroy = Request.prototype.destroy.bind(req);
res.writeHead = Response.prototype.writeHead.bind(res);
res.write = Response.prototype.write.bind(res);
res.end = Response.prototype.end.bind(res);
res.destroy = Response.prototype.destroy.bind(res);
res.addTrailers = Response.prototype.addTrailers.bind(res);
return req.prepare(() => { dispatchFunc(req, res); });
}
function inject (dispatchFunc, options, callback) {
options = (typeof options === 'string' ? { url: options } : options);
Assert(typeof dispatchFunc === 'function', 'Invalid dispatch function');
Joi.assert(options, schema);
const req = new Request(options);
const res = new Response(req, callback);
// Bind the req and res methods, as Express sets the prototype internally.
req._read = Request.prototype._read.bind(req);
req.destroy = Request.prototype.destroy.bind(req);
res.writeHead = Response.prototype.writeHead.bind(res);
res.write = Response.prototype.write.bind(res);
res.end = Response.prototype.end.bind(res);
res.destroy = Response.prototype.destroy.bind(res);
res.addTrailers = Response.prototype.addTrailers.bind(res);
return req.prepare(() => { dispatchFunc(req, res); });
}