Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-mocks-http

Package Overview
Dependencies
Maintainers
2
Versions
64
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.16.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3M
decreased by-3.12%
Maintainers
2
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

FAQs

Package last updated on 04 Oct 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc