🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

node-mocks-http

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-mocks-http

Mock 'http' objects for testing Express, Next.js and Koa routing functions

1.17.1
Source
npm
Version published
Weekly downloads
936K
-17.35%
Maintainers
1
Weekly downloads
 
Created

What is node-mocks-http?

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.

What are node-mocks-http's main functionalities?

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'

Other packages similar to node-mocks-http

Keywords

mock

FAQs

Package last updated on 01 May 2025

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