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

pretender

Package Overview
Dependencies
Maintainers
5
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pretender

Pretender is a mock server library for XMLHttpRequest and Fetch, that comes with an express/sinatra style syntax for defining routes and their handlers.

  • 3.4.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
334K
increased by11.68%
Maintainers
5
Weekly downloads
 
Created

What is pretender?

Pretender is a library for creating a mock server in the browser. It allows developers to intercept and handle HTTP requests, making it useful for testing and development without needing a real backend.

What are pretender's main functionalities?

Intercepting HTTP Requests

This feature allows you to intercept HTTP GET requests to a specified URL and return a custom response. In this example, a GET request to '/api/data' will return a 200 status code with a JSON response containing sample data.

const server = new Pretender(function() {
  this.get('/api/data', () => {
    return [200, { 'Content-Type': 'application/json' }, JSON.stringify({ data: 'sample data' })];
  });
});

Handling Different HTTP Methods

Pretender can handle various HTTP methods such as POST, PUT, DELETE, etc. This example demonstrates handling a POST request to '/api/data', parsing the request body, and returning a custom response.

const server = new Pretender(function() {
  this.post('/api/data', (request) => {
    let requestBody = JSON.parse(request.requestBody);
    return [201, { 'Content-Type': 'application/json' }, JSON.stringify({ message: 'Data received', data: requestBody })];
  });
});

Simulating Delays

You can simulate network delays to test how your application handles slow responses. In this example, a 1000ms delay is added to the GET request to '/api/data'.

const server = new Pretender(function() {
  this.get('/api/data', (request) => {
    return [200, { 'Content-Type': 'application/json' }, JSON.stringify({ data: 'sample data' })];
  }, 1000); // 1000ms delay
});

Other packages similar to pretender

FAQs

Package last updated on 04 Aug 2021

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