How to use @hapi/shot - 8 common examples

To help you get started, we’ve selected a few @hapi/shot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / testlab / src / shot.ts View on Github external
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;
}
github strongloop / loopback-next / packages / testlab / src / shot.ts View on Github external
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;
}
github cjihrig / artificial / lib / index.js View on Github external
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); });
}
github cjihrig / artificial / lib / index.js View on Github external
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); });
}
github cjihrig / artificial / lib / index.js View on Github external
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); });
}
github cjihrig / artificial / lib / index.js View on Github external
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); });
}
github hapijs / hapi / lib / transmit.js View on Github external
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') {
github hapijs / hapi / lib / server.js View on Github external
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;
    }

@hapi/shot

Injects a fake HTTP request/response into a node HTTP server

BSD-3-Clause
Latest version published 2 years ago

Package Health Score

74 / 100
Full package analysis