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

nock

Package Overview
Dependencies
Maintainers
1
Versions
435
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nock

HTTP Server mocking for Node.js

  • 0.12.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is nock?

The nock npm package is a powerful HTTP server mocking and expectations library for Node.js. It allows developers to test modules that perform HTTP requests in isolation. By intercepting outgoing HTTP requests and providing mock responses, nock enables a more controlled and predictable testing environment.

What are nock's main functionalities?

Intercepting HTTP Requests

This feature allows you to intercept an HTTP GET request to a specified URL and provide a custom response.

const nock = require('nock');
const http = require('http');

nock('http://example.com')
  .get('/resource')
  .reply(200, 'domain matched');

http.get('http://example.com/resource', (res) => {
  // This will receive the mocked response
});

Specifying Response Status and Body

With nock, you can specify the HTTP response status and body for a mocked endpoint, allowing you to simulate different server responses.

const nock = require('nock');

nock('http://example.com')
  .post('/login')
  .reply(401, { error: 'Unauthorized' });

Dynamic Response Functions

Nock can use functions to dynamically generate responses based on the incoming request data.

const nock = require('nock');

nock('http://example.com')
  .get('/data')
  .reply(200, (uri, requestBody) => {
    return { data: 'Dynamic response based on request' };
  });

Recording and Playback

Nock can record HTTP requests and responses and then play them back, which is useful for creating fixtures or reproducing issues.

const nock = require('nock');

nock.recorder.rec();
// Perform HTTP requests
// Nock will record the requests and responses
// Stop recording
nock.recorder.play();

Specifying Request Headers

This feature allows you to specify expected request headers and mock responses accordingly, which is useful for testing authentication and other header-dependent functionality.

const nock = require('nock');

nock('http://example.com', {
  reqheaders: {
    'authorization': 'Bearer token',
    'content-type': 'application/json'
  }
})
.get('/protected/resource')
.reply(200, 'Authenticated content');

Other packages similar to nock

FAQs

Package last updated on 13 Apr 2012

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