UNPKG

mock-xmlhttprequest

Version:
84 lines (82 loc) 3.26 kB
/** * mock-xmlhttprequest v8.4.1 * (c) 2025 Bertrand Guay-Paquet * @license MIT */ import RequestData from './RequestData.mts'; import type { MockXhrResponseReceiver } from './MockXhrResponseReceiver.mts'; /** * A request produced by MockXhr.send() and methods to respond to it. * * Each call to MockXhr.send() on an instance creates a new instance of MockXhrRequest. When there * are multiple active MockXhrRequest instances for the same MockXhr instance, only the response to * the last one is considered. Responses to previous MockXhrRequests are ignored. */ export default class MockXhrRequest { private readonly _requestData; private readonly _responseReceiver; constructor(requestData: RequestData, responseReceiver: MockXhrResponseReceiver); get requestData(): RequestData; /** * @returns Request headers container */ get requestHeaders(): import("./HeadersContainer.mts").default; get method(): string; get url(): string; get body(): any; get withCredentials(): boolean; /** * Note: this isn't completely accurate for a multipart/form-data encoded FormData request body. * MockXhr not consider headers, encoding, and other factors that influence the request body size * of non-mocked XMLHttpRequest. You can consider the value returned by this method as a floor * value for the request body size. This can still be useful to simulate upload progress events. * * @returns Request body's total byte size */ getRequestBodySize(): number; /** * Fire a request upload progress event. * * @param transmitted Transmitted bytes */ uploadProgress(transmitted: number): void; /** * Complete response method that sets the response headers and body. Changes the request's * readyState to DONE. * * @param status Response HTTP status (default 200) * @param headers Name-value headers (optional) * @param body Response body (default null) * @param statusText Response HTTP status text (optional) */ respond(status?: number, headers?: Record<string, string> | null, body?: unknown, statusText?: string): void; /** * Set the response headers. Changes the request's readyState to HEADERS_RECEIVED. * * @param status Response HTTP status (default 200) * @param headers Name-value headers (optional) * @param statusText Response HTTP status text (optional) */ setResponseHeaders(status?: number, headers?: Record<string, string> | null, statusText?: string): void; /** * Fire a response progress event. Changes the request's readyState to LOADING. * * @param transmitted Transmitted bytes * @param length Body length in bytes */ downloadProgress(transmitted: number, length: number): void; /** * Set the response body. Changes the request's readyState to DONE. * * @param body Response body (default null) */ setResponseBody(body?: unknown): void; /** * Simulate a network error. Changes the request's readyState to DONE. */ setNetworkError(): void; /** * Simulate a request timeout. Changes the request's readyState to DONE. */ setRequestTimeout(): void; }