Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules / ___dist / oneRequest / deps / StubRequest.js
Size: Mime:
import uuid from 'uuid'
import express from 'express'
// interface Request extends http.IncomingMessage, Express.Request
export interface StubRequest {
  // header(name: string): string | undefined

  /**
   * Short-hand for `url.parse(req.url).pathname`.
   */
  path: string;

  /**
   * Parse the "Host" header field hostname.
   */
  hostname: string;

  body: any;
  // cookies: { string remember: boolean }
  cookies: any;
  method: string;
  params: any;
  query: any;
  route: any;
  signedCookies: any;
  originalUrl: string;
  url: string;
  baseUrl: string;
  app: Application;
}

// interface Response extends http.ServerResponse, Express.Response {
export interface StubResponse {
  /**
   * Set status `code`.
   *
   * @param code
   */
  status(code: number): Response;

  /**
   * Set the response HTTP status code to `statusCode` and send its string representation as the response body.
   * @link http://expressjs.com/4x/api.html#res.sendStatus
   *
   * @example
   *
   *    res.sendStatus(200); // equivalent to res.status(200).send('OK')
   *    res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
   *    res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
   *    res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')
   *
   * @param code
   */
  sendStatus(code: number): Response;

  /**
   * Send a response.
   *
   * Examples:
   *
   *     res.send(new Buffer('wahoo'));
   *     res.send({ some: 'json' });
   *     res.send('<p>some html</p>');
   *     res.send(404, 'Sorry, cant find that');
   *     res.send(404);
  */
  send: Send;

  /**
   * Send JSON response.
   *
   * Examples:
   *
   *     res.json(null);
   *     res.json({ user: 'tj' });
   *     res.json(500, 'oh noes!');
   *     res.json(404, 'I dont have that');
   */
  json: Send;

  /**
   * Send JSON response with JSONP callback support.
   *
   * Examples:
   *
   *     res.jsonp(null);
   *     res.jsonp({ user: 'tj' });
   *     res.jsonp(500, 'oh noes!');
   *     res.jsonp(404, 'I dont have that');
   */
  jsonp: Send;

  /**
   * Transfer the file at the given `path`.
   *
   * Automatically sets the _Content-Type_ response header field.
   * The callback `fn(err)` is invoked when the transfer is complete
   * or when an error occurs. Be sure to check `res.sentHeader`
   * if you wish to attempt responding, as the header and some data
   * may have already been transferred.
   *
   * Options:
   *
   *   - `maxAge`   defaulting to 0 (can be string converted by `ms`)
   *   - `root`     root directory for relative filenames
   *   - `headers`  object of headers to serve with file
   *   - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them
   *
   * Other options are passed along to `send`.
   *
   * Examples:
   *
   *  The following example illustrates how `res.sendFile()` may
   *  be used as an alternative for the `static()` middleware for
   *  dynamic situations. The code backing `res.sendFile()` is actually
   *  the same code, so HTTP cache support etc is identical.
   *
   *     app.get('/user/:uid/photos/:file', function(req, res){
   *       var uid = req.params.uid
   *         , file = req.params.file;
   *
   *       req.user.mayViewFilesFrom(uid, function(yes){
   *         if (yes) {
   *           res.sendFile('/uploads/' + uid + '/' + file);
   *         } else {
   *           res.send(403, 'Sorry! you cant see that.');
   *         }
   *       });
   *     });
   *
   * @api public
   */
  sendFile(path: string): void;
  sendFile(path: string, options: any): void;
  sendFile(path: string, fn: Errback): void;
  sendFile(path: string, options: any, fn: Errback): void;

  /**
   * Set _Content-Type_ response header with `type` through `mime.lookup()`
   * when it does not contain "/", or set the Content-Type to `type` otherwise.
   *
   * Examples:
   *
   *     res.type('.html');
   *     res.type('html');
   *     res.type('json');
   *     res.type('application/json');
   *     res.type('png');
   *
   * @param type
   */
  type(type: string): Response;

  /**
   * Set the location header to `url`.
   *
   * The given `url` can also be the name of a mapped url, for
   * example by default express supports "back" which redirects
   * to the _Referrer_ or _Referer_ headers or "/".
   *
   * Examples:
   *
   *    res.location('/foo/bar').;
   *    res.location('http://example.com');
   *    res.location('../login'); // /blog/post/1 -> /blog/login
   *
   * Mounting:
   *
   *   When an application is mounted and `res.location()`
   *   is given a path that does _not_ lead with "/" it becomes
   *   relative to the mount-point. For example if the application
   *   is mounted at "/blog", the following would become "/blog/login".
   *
   *      res.location('login');
   *
   *   While the leading slash would result in a location of "/login":
   *
   *      res.location('/login');
   *
   * @param url
   */
  location(url: string): Response;
}

const stubRequestRegistry = new Map()
let identity = 0

/* @lint - is stub test function */
/* eslint-disable max-statements */
const makeStubRequest = () => {
  let result = {
    httpStatusCode: 0,
    headers: {
      length: 0,
    },
  }
  let res = {}
  let req = {}

  res.status = httpStatusCode => {
    result.httpStatusCode = httpStatusCode
    return res
  }
  res.setHeader = (headerName, data) => {
    result.headers[headerName] = data
    result.headers.length += 1
    return res
  }
  res.end = () => {
    return res
  }
  res.send = (stringResponse = `<!DOCTYPE html>${html}`) => {
    result.sent = stringResponse
    return res
  }
  res.locals = {
    nonce: uuid.v4(),
  }
  const requestMock = {
    req,
    res,
  }

  // save to cache
  identity += 1
  const stub = {
    identity,
    result,
    request: requestMock,
  }
  stubRequestRegistry.set(identity, stub)

  // done
  return requestMock
}

export { makeStubRequest }
export default makeStubRequest