New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@cantremember/node-mocks-http

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cantremember/node-mocks-http

Mock 'http' objects for testing Express routing functions

1.5.0
latest
Source
npm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

node-mocks-http logo

NPM version Build Status Gitter chat

Mock 'http' objects for testing Express routing functions, but could be used for testing any Node.js web server applications that have code that requires mockups of the request and response objects.

Installation

This project is available as a NPM package.

$ npm install --save-dev node-mocks-http

Our example includes --save-dev based on the assumption that node-mocks-http will be used as a development dependency..

After installing the package include the following in your test files:

var httpMocks = require('node-mocks-http');

Usage

Suppose you have the following Express route:

app.get('/user/:id', routeHandler);

And you have created a function to handle that route's call:

var routeHandler = function( request, response ) { ... };

You can easily test the routeHandler function with some code like this using the testing framework of your choice:

exports['routeHandler - Simple testing'] = function(test) {

    var request  = httpMocks.createRequest({
        method: 'GET',
        url: '/user/42',
        params: {
          id: 42
        }
    });

    var response = httpMocks.createResponse();

    routeHandler(request, response);

    var data = JSON.parse( response._getData() );
    test.equal("Bob Dog", data.name);
    test.equal(42, data.age);
    test.equal("bob@dog.com", data.email);

    test.equal(200, response.statusCode );
    test.ok( response._isEndCalled());
    test.ok( response._isJSON());
    test.ok( response._isUTF8());

    test.done();

};

API

.createRequest()

httpMocks.createRequest(options)

Where options is an object hash with any of the following values:

optiondescriptiondefault value
methodrequest HTTP method'GET'
urlrequest URL''
originalUrlrequest original URLurl
pathrequest path''
paramsobject hash with params{}
sessionobject hash with session valuesundefined
cookiesobject hash with request cookies{}
signedCookiesobject hash with signed cookiesundefined
headersobject hash with request headers{}
bodyobject hash with body{}
queryobject hash with query values{}
filesobject hash with values{}

.createResponse()

httpMocks.createResponse(options)

Where options is an object hash with any of the following values:

optiondescriptiondefault value
eventEmitterevent emitter used by response objectmockEventEmitter
writableStreamwritable stream used by response objectmockWritableStream

Design Decisions

We wanted some simple mocks without a large framework.

We also wanted the mocks to act like the original framework being mocked, but allow for setting of values before calling and inspecting of values after calling.

For Developers

We are looking for more volunteers to bring value to this project, including the creation of more objects from the HTTP module.

This project doesn't address all features that must be mocked, but it is a good start. Feel free to send pull requests, and a member of the team will be timely in merging them.

If you wish to contribute please read our Contributing Guidelines.

Release Notes

Most releases fix bugs with our mocks or add features similar to the actual Request and Response objects offered by Node.js and extended by Express.

Most Recent Release Notes

  • v1.4.2 - April 30, 2015
  • v1.4.1 - April 14, 2015
  • v1.4.0 - April 12, 2015
  • v1.3.0 - April 5, 2015
  • v1.2.7 - March 24, 2015
  • v1.2.6 - March 19, 2015
  • v1.2.5 - March 5, 2015

License

Licensed under MIT.

Keywords

mock

FAQs

Package last updated on 13 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts