Low-level network interception library. WebSocket (the WebSocket class in Undici and within the browser). While there are plenty of community mocking libraries, they have a tendency to make use of request interception as an implementation detail, providing you with a excessive-stage API that includes request matching, timeouts, recording, and so forth. This library is a barebones implementation that gives as little abstraction as attainable to execute arbitrary logic upon any request. It’s primarily designed as an underlying part for top-degree API mocking options equivalent to Mock Service Worker. How is that this library different? The core philosophy of Interceptors is to run as a lot of the underlying community code as potential. Strange for a network mocking library, is not it? Seems, respecting the system’s integrity and executing more of the community code results in more resilient assessments and likewise helps to uncover bugs within the code that would in any other case go unnoticed. Interceptors closely depend on class extension as an alternative of perform and module overrides.
By extending the native network code, it can surgically insert the interception and mocking pieces solely where crucial, leaving the remainder of the system intact. The request interception algorithms differ dramatically primarily based on the request API. Interceptors accommodate for them all, bringing the intercepted requests to a common ground-the Fetch API Request instance. The identical applies for rental agreement responses, where a Fetch API Response instance is translated to the appropriate response format. This library aims to offer full specification compliance with the APIs and protocols it extends. Once extended, it intercepts and normalizes all requests to the Fetch API Request cases. This fashion, irrespective of the request source (http.ClientRequest, XMLHttpRequest, window.Request, and many others), you all the time get a specification-compliant request instance to work with. You’ll be able to respond to the intercepted HTTP request by constructing a Fetch API Response instance. Instead of designing customized abstractions, this library respects the Fetch API specification and takes the accountability to coerce a single response declaration to the suitable response codecs primarily based on the request-issuing modules (like http.OutgoingMessage to reply to http.ClientRequest, or updating XMLHttpRequest response-related properties).
Doesn’t handle requests by default. This limitation is intrinsic to the interception algorithm used by the library. To ensure that it to emit the join event on the socket, the library must know if you’ve got handled the request in any means (e.g. responded with a mocked response or errored it). For that, it emits the request occasion on the interceptor the place you can handle the request. Since you can eat the request stream within the request event, it waits until the request physique stream is full (i.e. until req.end() known as). This creates a catch 22 that causes this limitation. To use this library you want to decide on one or multiple interceptors to apply. FetchInterceptor to spy on fetch. You may combine multiple interceptors to seize requests from different request-issuing modules directly. Note that you can use pre-outlined presets that cowl all the request sources for a given atmosphere sort. When utilizing BatchInterceptor, you possibly can provide a pre-outlined preset to its “interceptors” option to capture all request for that atmosphere.
This preset combines ClientRequestInterceptor, XMLHttpRequestInterceptor and is supposed to be utilized in Node.js. This preset combines XMLHttpRequestInterceptor and FetchInterceptor and is supposed to be used in a browser. All HTTP request interceptors emit a “request” event. Within the listener to this event, they expose a request reference, which is a Fetch API Request instance. There are some ways to explain a request in Node.js however this library coerces completely different request definitions to a single specification-compliant Request instance to make the dealing with constant. Because the exposed request instance implements the Fetch API specification, you may operate with it just as you do with the common browser request. Do not forget to clone the request before reading its body! Request representations are readonly. This restriction is done so that the library would not must unnecessarily synchronize the precise request occasion and its Fetch API request representation. As of now, this library will not be meant for use as a full-scale proxy.
Although this library can be utilized purely for request introspection functions, you can even affect request decision by responding to any intercepted request throughout the “request” event. We use Fetch API Response class as the middle-floor for mocked response definition. This library then coerces the response instance to the suitable response format (e.g. to http.OutgoingMessage in the case of http.ClientRequest). Note that a single request can solely be dealt with as soon as. You might wish to introduce conditional logic, like routing, in your request listener however it is typically suggested to use a better-stage library like Mock Service Worker that does request matching for you. Requests have to be responded to within the identical tick as the request listener. This implies you can not respond to a request utilizing setTimeout, as this may delegate the callback to the subsequent tick. When you wish to introduce asynchronous side-results within the listener, consider making it an async function, awaiting any aspect-effects you need.
Comment (0)