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

mock-xmlhttprequest

Package Overview
Dependencies
Maintainers
0
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-xmlhttprequest

XMLHttpRequest mock for testing

  • 8.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created

What is mock-xmlhttprequest?

The mock-xmlhttprequest npm package is designed to mock XMLHttpRequest objects for testing purposes. It allows developers to simulate HTTP requests and responses, making it easier to test code that relies on XMLHttpRequest without making actual network requests.

What are mock-xmlhttprequest's main functionalities?

Mocking GET Requests

This feature allows you to mock a GET request and provide a custom response. The code sample demonstrates how to create a mock XMLHttpRequest, open a GET request, send it, and then respond with a 200 status and a JSON payload.

const MockXMLHttpRequest = require('mock-xmlhttprequest');

const xhr = new MockXMLHttpRequest();
xhr.open('GET', '/api/data');
xhr.send();

xhr.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ key: 'value' }));

console.log(xhr.responseText); // Output: {"key":"value"}

Mocking POST Requests

This feature allows you to mock a POST request and provide a custom response. The code sample demonstrates how to create a mock XMLHttpRequest, open a POST request, set request headers, send a JSON payload, and then respond with a 201 status and a JSON payload.

const MockXMLHttpRequest = require('mock-xmlhttprequest');

const xhr = new MockXMLHttpRequest();
xhr.open('POST', '/api/data');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ key: 'value' }));

xhr.respond(201, { 'Content-Type': 'application/json' }, JSON.stringify({ success: true }));

console.log(xhr.responseText); // Output: {"success":true}

Simulating Network Errors

This feature allows you to simulate network errors. The code sample demonstrates how to create a mock XMLHttpRequest, open a GET request, send it, and then simulate a network error.

const MockXMLHttpRequest = require('mock-xmlhttprequest');

const xhr = new MockXMLHttpRequest();
xhr.open('GET', '/api/data');
xhr.send();

xhr.error();

console.log(xhr.status); // Output: 0
console.log(xhr.statusText); // Output: ""

Other packages similar to mock-xmlhttprequest

Keywords

FAQs

Package last updated on 04 Nov 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