
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
node-mocks-http
Advanced tools
The node-mocks-http package is a tool for creating mock HTTP objects for testing purposes in Node.js applications. It allows developers to simulate HTTP requests and responses, making it easier to test Express.js middleware and route handlers without needing a real HTTP server.
Mocking HTTP Requests
This feature allows you to create a mock HTTP request object. You can specify the method, URL, and parameters to simulate different types of requests.
const httpMocks = require('node-mocks-http');
const request = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
params: {
id: '42'
}
});
console.log(request.method); // 'GET'
console.log(request.url); // '/user/42'
console.log(request.params.id); // '42'
Mocking HTTP Responses
This feature allows you to create a mock HTTP response object. You can set the status code and send data, and then inspect the response to verify its properties.
const httpMocks = require('node-mocks-http');
const response = httpMocks.createResponse();
response.status(200).send('OK');
console.log(response._getStatusCode()); // 200
console.log(response._getData()); // 'OK'
Mocking HTTP Headers
This feature allows you to create a mock HTTP request with specific headers. You can then inspect the headers to ensure they are set correctly.
const httpMocks = require('node-mocks-http');
const request = httpMocks.createRequest({
headers: {
'content-type': 'application/json'
}
});
console.log(request.headers['content-type']); // 'application/json'
Mocking HTTP Cookies
This feature allows you to create a mock HTTP request with cookies. You can then inspect the cookies to ensure they are set correctly.
const httpMocks = require('node-mocks-http');
const request = httpMocks.createRequest({
cookies: {
token: '12345'
}
});
console.log(request.cookies.token); // '12345'
Supertest is a popular library for testing HTTP endpoints. It provides a high-level abstraction for testing HTTP, making it easy to send requests and assert responses. Unlike node-mocks-http, Supertest actually sends HTTP requests to a server, which can be useful for end-to-end testing.
Nock is a library for HTTP mocking and expectations. It allows you to intercept HTTP requests and provide predefined responses. Nock is more focused on mocking external HTTP requests, whereas node-mocks-http is more focused on creating mock request and response objects for internal testing.
Sinon is a versatile library for creating spies, stubs, and mocks. While it is not specifically designed for HTTP mocking, it can be used in conjunction with other libraries to mock HTTP requests and responses. Sinon provides more general-purpose mocking capabilities compared to node-mocks-http.
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.
This project is available as a NPM package.
$ npm install node-mocks-http
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 = 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();
};
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.
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.
After making any changes, please verify your works.
Install jshint
globally.
npm install -g jshint
Navigate to the project folder and run npm install
to install the
project's dependencies.
Then simply run the tests.
./run-tests
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.
Licensed under MIT.
FAQs
Mock 'http' objects for testing Express, Next.js and Koa routing functions
The npm package node-mocks-http receives a total of 961,858 weekly downloads. As such, node-mocks-http popularity was classified as popular.
We found that node-mocks-http demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.