New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

stub-service

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stub-service

Simple Stub service

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-25%
Maintainers
0
Weekly downloads
 
Created
Source

Stub Service

A simple and flexible npm package for creating stub servers to mock HTTP endpoints. This package allows you to define custom routes, methods, headers, and responses for testing your applications.

Usage

To start the stub server, create an instance of StubService and call the start method with the desired port number. Call StubService.[HTTP Method] to mock requests.

//import package
import { StubService } from "stub-service";
// create server instance
const stubService = new StubService();
// start server
await stubService.start(3000);
// configure 
stubService.get("/api/users/1").reply(200, { id: 1, name: "John" });
stubService.get("/api/users/2").reply(200, { id: 2, name: "Jane" });
// sample tests
const { data } = await axios.get("http://localhost:3000/api/users/1");
expect(data).toEqual({ id: 1, name: "John" });

Stub service host

Host is localhost. The stub service will run on port provided to the .start() method.

Headers

stubService
  .get("/api/users/1", { headers: { authorization: `Bearer abc` } })
  .reply(200, { id: 1, name: "John" });
stubService
  .get("/api/users/1", { headers: { authorization: `Bearer xyz` } })
  .reply(401, { error: "Access denied" });

Request payload

stubService
  .post("/api/users", { name: "John" })
  .reply(200, { id: 1, name: "John" });

Stopping the Server

To stop the stub server, call the stop method.

stubService.stop();

Resetting the Server

To reset the stub server between tests, use the reset method. This clears all the defined routes and responses.

stubService.reset();

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 16 Aug 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