All Projects → mswjs → Interceptors

mswjs / Interceptors

Low-level HTTP/HTTPS/XHR/fetch request interception library.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Interceptors

electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (-55%)
Mutual labels:  xhr, https, request
Kakapo.js
🐦 Next generation mocking framework in Javascript
Stars: ✭ 535 (+435%)
Mutual labels:  interceptor, mock
mock-req-res
Extensible mock req / res objects for use in unit tests of Express controller and middleware functions.
Stars: ✭ 39 (-61%)
Mutual labels:  mock, request
Bypass
Bypass provides a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests.
Stars: ✭ 731 (+631%)
Mutual labels:  https, mock
wumpfetch
🚀🔗 A modern, lightweight, fast and easy to use Node.js HTTP client
Stars: ✭ 20 (-80%)
Mutual labels:  https, request
kras
Efficient server proxying and mocking in Node.js. 💪
Stars: ✭ 18 (-82%)
Mutual labels:  mock, interceptor
Phin
Node HTTP client
Stars: ✭ 449 (+349%)
Mutual labels:  request, https
Esa Restlight
ESA Restlight is a lightweight and rest-oriented web framework.
Stars: ✭ 67 (-33%)
Mutual labels:  interceptor, https
Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+938%)
Mutual labels:  request, xhr
Axios Module
Secure and easy axios integration with Nuxt.js
Stars: ✭ 998 (+898%)
Mutual labels:  request, interceptor
Stubmatic
Mock HTTP calls without coding. Designed specially for testing and testers.
Stars: ✭ 118 (+18%)
Mutual labels:  mock, https
Hooman
http interceptor to hoomanize cloudflare requests
Stars: ✭ 82 (-18%)
Mutual labels:  request, interceptor
servie
Standard, framework-agnostic HTTP interfaces for JavaScript servers and clients
Stars: ✭ 39 (-61%)
Mutual labels:  https, request
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (-23%)
Mutual labels:  mock, request
net
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.
Stars: ✭ 16 (-84%)
Mutual labels:  https, request
jmitm
Java版本的mitmproxy,对本地浏览器所有的Http(s)请求和响应进行拦截并「重制」;也可充当轻量级B/S版抓包软件;
Stars: ✭ 19 (-81%)
Mutual labels:  mock, https
esa-httpclient
An asynchronous event-driven HTTP client based on netty.
Stars: ✭ 82 (-18%)
Mutual labels:  https, interceptor
spydriver
🕵️ Lightweight utility to intercept WebDriver and WebElement method calls.
Stars: ✭ 24 (-76%)
Mutual labels:  mock, interceptor
Xhr
A small xhr wrapper
Stars: ✭ 801 (+701%)
Mutual labels:  request, xhr
Logginginterceptor
An OkHttp interceptor which has pretty logger for request and response. +Mock support
Stars: ✭ 1,149 (+1049%)
Mutual labels:  interceptor, mock

Latest version Build status

@mswjs/interceptors

Low-level HTTP/HTTPS/XHR/fetch request interception library.

Intercepts any requests issued by:

  • http.get/http.request
  • https.get/https.request
  • XMLHttpRequest
  • fetch
  • Any third-party libraries that use the modules above (i.e. request, node-fetch, etc.)

Motivation

While there are a lot of network communication mocking libraries, they tend to use request interception as an implementation detail, giving you a high-level API that includes request matching, timeouts, retries, and so forth.

This library is a strip-to-bone implementation that provides as little abstraction as possible to execute arbitrary logic upon any request. It's primarily designed as an underlying component for high-level API mocking solutions such as Mock Service Worker.

How is this library different?

As interception is often combined with request route matching, some libraries can determine whether a request should be mocked before it actually happens. This approach is not suitable for this library, as it rather intercepts all requests and then let's you decide which ones should be mocked. This affects the level at which interception happens, and also the way mocked/original responses are constructed, in comparison to other solutions.

What this library does

This library monkey-patches the following native modules:

  • http.get/http.request
  • https.get/https.request
  • XMLHttpRequest
  • fetch

Once patched, it provisions the interception of requests and normalizes them to something called isomorphic request instances. That normalization ensures the same request handling for the consumer of the library, while requests originating from different modules may differ internally.

In its mocking phase, this library accepts an isomorphic response instance that describes a module-agnostic mocked response. This allows you to respond to requests issued by different modules using the same response instance.

What this library doesn't do

  • Does not provide any request matching logic.
  • Does not decide how to handle requests.

Getting started

npm install @mswjs/interceptors

API

createInterceptor(options: CreateInterceptorOptions)

import { createInterceptor } from '@mswjs/interceptors'
import nodeInterceptors from '@mswjs/interceptors/lib/presets/node'

const interceptor = createInterceptor({
  modules: nodeInterceptors,
  resolver(request, ref) {
    // Optionally, return a mocked response.
  },
})

Using the /presets/node interceptors preset is the recommended way to ensure all requests get intercepted, regardless of their origin.

Interceptors

This library utilizes a concept of interceptors–functions that patch necessary modules, handle mocked responses, and restore patched modules.

List of interceptors:

  • /interceptors/ClientRequest
  • /interceptors/XMLHttpRequest
  • /interceptors/fetch

To use a single, or multiple interceptors, import and provide them to the RequestInterceptor constructor.

import { createInterceptor } from '@mswjs/interceptors'
import { interceptXMLHttpRequest } from '@mswjs/interceptors/lib/interceptors/XMLHttpRequest'

// This `interceptor` instance would handle only XMLHttpRequest,
// ignoring requests issued via `http`/`https` modules.
const interceptor = new createInterceptor({
  modules: [interceptXMLHttpRequest],
})

Interceptors are crucial in leveraging environment-specific module overrides. Certain environments (i.e. React Native) do not have access to native Node.js modules (like http). Importing such modules raises an exception, and must be avoided.

Methods

.apply(): void

Applies module patches and enables interception of the requests.

interceptor.apply()

.on(event, listener): boolean

Adds an event listener to one of the following supported events:

  • request, whenever a new request happens.
  • response, whenever a request library responds to a request.
interceptor.on('request', (request) => {
  console.log('[%s] %s', request.method, request.url.toString())
})

.restore(): void

Restores all patched modules and stops intercepting future requests.

interceptor.restore()

Special mention

The following libraries were used as an inspiration to write this low-level API:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].