UNPKG

mentoss

Version:

A utility to mock fetch requests and responses.

128 lines (127 loc) 3.96 kB
/** * A class for mocking HTTP requests in undici. * Implements the undici Dispatcher interface. */ export class MockAgent { /** * Creates a new instance. * @param {MockAgentOptions} options Options for the instance. */ constructor({ servers, CustomRequest, CustomResponse, }: MockAgentOptions); /** * Dispatches an HTTP request through the mock servers. * Note: This method returns immediately (fire-and-forget pattern) and processes * the request asynchronously. Errors are reported through handler.onError. * @param {DispatchOptions} options The dispatch options. * @param {DispatchHandler} handler The handler for the response. * @returns {boolean} Returns true if the request was dispatched successfully. */ dispatch(options: DispatchOptions, handler: DispatchHandler): boolean; /** * Closes the agent. No new requests will be accepted after this. * @returns {Promise<void>} A promise that resolves when the agent is closed. */ close(): Promise<void>; /** * Destroys the agent immediately. This is an alias for close(). * @returns {Promise<void>} A promise that resolves when the agent is destroyed. */ destroy(): Promise<void>; /** * Determines if a request was made. * @param {string|RequestPattern} request The request to match. * @returns {boolean} `true` if the request was made, `false` otherwise. */ called(request: string | RequestPattern): boolean; /** * Determines if all routes were called. * @returns {boolean} `true` if all routes were called, `false` otherwise. */ allRoutesCalled(): boolean; /** * Gets the uncalled routes. * @return {string[]} The uncalled routes. */ get uncalledRoutes(): string[]; /** * Asserts that all routes were called. * @returns {void} * @throws {Error} When not all routes were called. */ assertAllRoutesCalled(): void; /** * Clears all data from the servers. * @returns {void} */ clearAll(): void; #private; } export type MockServer = import("./mock-server.js").MockServer; export type RequestPattern = import("./types.js").RequestPattern; export type DispatchOptions = { /** * The origin of the request. */ origin: string | URL; /** * The path of the request. */ path: string; /** * The HTTP method. */ method?: string | undefined; /** * The request body. */ body?: any; /** * The request headers. */ headers?: string[] | [string, string][] | Record<string, string | string[]> | undefined; }; export type DispatchHandler = { /** * Callback when connection is established. */ onConnect?: ((abort: () => void) => void) | undefined; /** * Callback when headers are received. */ onHeaders?: ((statusCode: number, headers: string[], resume: () => void) => void) | undefined; /** * Callback when data is received. */ onData?: ((chunk: any) => void) | undefined; /** * Callback when request is complete. */ onComplete?: ((trailers: string[]) => void) | undefined; /** * Callback when an error occurs. */ onError?: ((err: Error) => void) | undefined; }; export type MockAgentOptions = { /** * The servers to use. */ servers: MockServer[]; /** * The Request constructor to use. */ CustomRequest?: { new (input: RequestInfo | URL, init?: RequestInit): Request; prototype: Request; } | undefined; /** * The Response constructor to use. */ CustomResponse?: { new (body?: BodyInit | null, init?: ResponseInit): Response; prototype: Response; error(): Response; json(data: any, init?: ResponseInit): Response; redirect(url: string | URL, status?: number): Response; } | undefined; };