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 stubServerRequest(
options: ShotRequestOptions,
): IncomingMessage {
const stub = new ShotRequest(options);
// 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, ShotRequest.prototype);
return stub;
}
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); });
}
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); });
}
internals.transmit = function (response) {
const request = response.request;
const length = internals.length(response);
// Pipes
const encoding = request._core.compression.encoding(response, length);
const ranger = encoding ? null : internals.range(response, length);
const compressor = internals.encoding(response, encoding);
// Connection: close
const isInjection = Shot.isInjection(request.raw.req);
if (!(isInjection || request._core.started) ||
request._isPayloadPending && !request.raw.req._readableState.ended) {
response._header('connection', 'close');
}
// Write headers
internals.writeHead(response);
// Injection
if (isInjection) {
request.raw.res[Config.symbol] = { request };
if (response.variety === 'plain') {
Hoek.assert(!options.credentials, 'options.credentials no longer supported (use options.auth)');
if (options.auth) {
Hoek.assert(typeof options.auth === 'object', 'options.auth must be an object');
Hoek.assert(options.auth.credentials, 'options.auth.credentials is missing');
Hoek.assert(options.auth.strategy, 'options.auth.strategy is missing');
}
const needle = this._core._dispatch({
auth: options.auth,
allowInternals: options.allowInternals,
app: options.app,
plugins: options.plugins
});
const res = await Shot.inject(needle, settings);
const custom = res.raw.res[Config.symbol];
if (custom) {
res.result = custom.result;
res.request = custom.request;
delete res.raw.res[Config.symbol];
}
if (res.result === undefined) {
res.result = res.payload;
}
return res;
}