Node Mocks Http

Mock 'http' objects for testing Express routing functions
Alternatives To Node Mocks Http
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Fiber25,2666562 days ago275September 08, 202238mitGo
⚡️ Express inspired web framework written in Go
Iris23,75332014 hours ago212September 21, 202280bsd-3-clauseGo
The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :leaves: :rocket: | 谢谢 | #Go
Postgraphile11,8067699a month ago156May 25, 202263mitTypeScript
Execute one command (or mount one Node.js middleware) and get an instant high-performance GraphQL API for your PostgreSQL database!
Http Proxy Middleware9,780359,5366,5287 days ago78April 22, 202287mitTypeScript
:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
Morgan7,363247,3968,170a month ago26March 20, 202025mitJavaScript
HTTP request logger middleware for node.js
Nodejs Learning Guide6,484
a year ago9otherRuby
Tinyhttp2,401318 days ago284September 15, 20228mitTypeScript
🦄 0-legacy, tiny & fast web framework as a replacement of Express
Serverless Http1,59144213711 days ago45April 04, 202258otherJavaScript
Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
5 months ago32otherC++
Cross-platform, efficient, customizable, and robust asynchronous HTTP/WebSocket server C++14 library with the right balance between performance and ease of use
3 years ago19mitJavaScript
🚀 Framework for building universal web app and static website in Vue.js (beta)
Alternatives To Node Mocks Http
Select To Compare

Alternative Project Comparisons

node-mocks-http logo

NPM version Build Status Gitter chat

Mock 'http' objects for testing Express and Koa 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.


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');


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 = response._getJSONData(); // short-hand for JSON.parse( response._getData() );
    test.equal("Bob Dog",;
    test.equal(42, data.age);
    test.equal("[email protected]",;

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






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

option description default value
method request HTTP method 'GET'
url request URL ''
originalUrl request original URL url
baseUrl request base URL url
path request path ''
params object hash with params {}
session object hash with session values undefined
cookies object hash with request cookies {}
socket object hash with request socket {}
signedCookies object hash with signed cookies undefined
headers object hash with request headers {}
body object hash with body {}
query object hash with query values {}
files object hash with values {}

The object returned from this function also supports the Express request functions (.accepts(), .is(), .get(), .range(), etc.). Please send a PR for any missing functions.



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

option description default value
locals object that contains response local variables {}
eventEmitter event emitter used by response object mockEventEmitter
writableStream writable stream used by response object mockWritableStream
req Request object being responded to null

NOTE: The out-of-the-box mock event emitter included with node-mocks-http is not a functional event emitter and as such does not actually emit events. If you wish to test your event handlers you will need to bring your own event emitter.

Here's an example:

var httpMocks = require('node-mocks-http');
var res = httpMocks.createResponse({
  eventEmitter: require('events').EventEmitter

// ...
  it('should do something', function(done) {
    res.on('end', function() {
// ...

This is an example to send request body and trigger it's 'data' and 'end' events:

var httpMocks = require('node-mocks-http');
var req = httpMocks.createRequest();
var res = httpMocks.createResponse({
  eventEmitter: require('events').EventEmitter

// ...
  it('should do something', function(done) {
    res.on('end', function() {
      expect(response._getData()).to.equal('data sent in request');


    req.send('data sent in request');

  function route(req,res){
    var data= [];
    req.on("data", chunk => {
    req.on("end", () => {
        data = Buffer.concat(data)
// ...


httpMocks.createMocks(reqOptions, resOptions)

Merges createRequest and createResponse. Passes given options object to each constructor. Returns an object with properties req and res.

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.

See the Release History for details.


Licensed under MIT.

Popular Express Projects
Popular Http Projects
Popular Frameworks Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.