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

xhr-mock

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xhr-mock

Utility for mocking XMLHttpRequest.

  • 2.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
168K
decreased by-1.54%
Maintainers
1
Weekly downloads
 
Created

What is xhr-mock?

xhr-mock is a library for mocking XMLHttpRequests in JavaScript. It is particularly useful for testing purposes, allowing developers to simulate HTTP requests and responses without actually making network calls.

What are xhr-mock's main functionalities?

Mocking GET Requests

This feature allows you to mock GET requests. The code sample sets up the mock, defines a GET request handler that returns a mocked response, and then tears down the mock after the test.

const xhrMock = require('xhr-mock');
xhrMock.setup();
xhrMock.get('/api/data', (req, res) => {
  return res.status(200).body('Mocked GET response');
});
// Your test code here
xhrMock.teardown();

Mocking POST Requests

This feature allows you to mock POST requests. The code sample sets up the mock, defines a POST request handler that returns a mocked response, and then tears down the mock after the test.

const xhrMock = require('xhr-mock');
xhrMock.setup();
xhrMock.post('/api/data', (req, res) => {
  return res.status(201).body('Mocked POST response');
});
// Your test code here
xhrMock.teardown();

Mocking with Custom Headers

This feature allows you to mock requests with custom headers. The code sample sets up the mock, defines a GET request handler that returns a mocked response with custom headers, and then tears down the mock after the test.

const xhrMock = require('xhr-mock');
xhrMock.setup();
xhrMock.get('/api/data', (req, res) => {
  return res.status(200).header('Content-Type', 'application/json').body(JSON.stringify({ message: 'Mocked response with headers' }));
});
// Your test code here
xhrMock.teardown();

Other packages similar to xhr-mock

Keywords

FAQs

Package last updated on 08 Nov 2019

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