import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http'; import type { Http2ServerRequest } from 'node:http2'; export interface MockRequest { url: string; filePath?: string; ignoreSearch?: boolean; /** * Match a specific HTTP method (any method matches when omitted). * A method-specific mock is preferred over a method-less one for the same url. */ method?: string; } /** * Handlers receive the plain http response type so header/writeHead overloads * resolve cleanly. Under https/http2 the compat response supports the same api. */ export type MockHandlerResponse = ServerResponse & { error?: Error; }; export type MockResponseHandler = (req: Req, res: MockHandlerResponse) => void; export interface MockResponse { body: string | Record; /** * Delay responding by `delay` ms (ignored for handler responses) */ delay?: number; hang?: boolean; headers?: Record; error?: boolean; missing?: boolean; offline?: boolean; status?: number; } /** * A request that matched a registered mock */ export interface MockRequestCall { /** * Raw request body ("undefined" when the request had none) */ body: string | undefined; headers: IncomingHttpHeaders; method: string | undefined; params?: Record; url: string; } /** * Handle returned when registering a mock response. * Call it to remove the mock; inspect "calls" to assert matched requests. */ export interface MockedResponse { (): void; /** * Requests that matched this mock, in call order */ calls: Array; } export interface MockPushStream { url: string; type: string; filePath?: string; ignoreSearch?: boolean; protocol?: string; } export interface MockPushEventOptions { delay?: number; connect?: boolean; event?: string; id?: string; namespace?: string; } export interface MockPushEvent { name: string; message?: Buffer | ArrayBuffer | ArrayBufferView | string | Record; sequence?: Array; options?: MockPushEventOptions; } export interface PushEvent { message: Buffer | ArrayBuffer | ArrayBufferView | string | Record; options?: PushEventOptions; } export interface PushEventOptions { id?: string; event?: string; namespace?: string; protocol?: string; } export declare const testBrowser: { /** * Disable all external network connections * and optionally reroute all external requests to this server */ disableNetwork(rerouteAllRequests?: boolean): void; /** * Re-enable all external network connections */ enableNetwork(): void; /** * Add mock response for "req". * Returns a handle that removes the mock when called, * and exposes matched requests via its "calls" array. */ mockResponse(req: string | MockRequest, res?: MockResponse | MockResponseHandler, once?: boolean, onMockCallback?: () => void): MockedResponse; /** * Register a mock stream at "url", returning a handle exposing live * connections for request/reply, per-connection send, and close */ mockStream(url: string, options?: MockStreamOptions): MockStream; /** * Register mock push "events" for "stream" */ mockPushEvents(stream: string | MockPushStream, events: MockPushEvent | Array, onSendCallback?: (data: any) => void): () => void; /** * Trigger EventSource/WebSocket event */ pushEvent(stream: string, event: string | { message: string | object; options: { event: string; id: string; }; }): Promise; }; /** * Browser variants of the node MockStream types. * Same names/shape/semantics, minus what the browser environment * can't provide: request headers are not readable in-page, and * ping frames / ping-retry intervals are invisible to page script. */ export interface MockStreamOptions { /** * Inspect the connection (`Sec-WebSocket-Protocol` entries, url) and return * `false` to reject it (default: always authorized). Rejection surfaces as * a client `error` event (the browser can't answer with a `401` in-page). */ authorize?: (context: MockStreamContext) => boolean; /** * Called with a connection handle each time a client connects. */ onConnection?: (connection: MockStreamConnection) => void; } export interface MockStreamContext { /** * Always empty in the browser (request headers are not readable in-page). */ headers: Record; /** * Protocols passed to the `WebSocket` constructor (empty for EventSource). */ protocols: Array; url: URL; } export interface MockStreamConnection { /** * Unique per-connection id (new on every (re)connect). */ id: string; type: 'ws' | 'es'; url: URL; /** * Always empty in the browser (request headers are not readable in-page). */ headers: Record; protocols: Array; readonly closed: boolean; /** * Send a message on this connection only (same encoding rules as `pushEvent`). */ send(message: PushEvent['message'], options?: PushEventOptions): void; /** * Close this connection. For WebSocket, browsers only permit close codes * `1000`/`3000-4999`; other codes fall back to a plain close. */ close(code?: number, reason?: string): void; on(event: 'message', handler: (data: string | Blob | ArrayBuffer | ArrayBufferView) => void): this; on(event: 'close', handler: (event: { code?: number; reason?: string; }) => void): this; once(event: 'message', handler: (data: string | Blob | ArrayBuffer | ArrayBufferView) => void): this; once(event: 'close', handler: (event: { code?: number; reason?: string; }) => void): this; off(event: 'message', handler: (data: string | Blob | ArrayBuffer | ArrayBufferView) => void): this; off(event: 'close', handler: (event: { code?: number; reason?: string; }) => void): this; } export interface MockStream { url: URL; type: 'ws' | 'es'; /** * Live connections in connect order. */ connections: Array; /** * Broadcast to all connections (same semantics as `pushEvent`). */ pushEvent(event: string | PushEvent): Promise; /** * Close all connections and unregister the stream. */ destroy(): void; } export type ContentType = 'css' | 'html' | 'js'; export type Req = (IncomingMessage | Http2ServerRequest) & { filePath: string; type?: ContentType; url: string; params?: Record; context?: RequestContext; contextUrl?: string; }; export interface RequestContext { assert: ImportAssertionType; dynamic: boolean; filePath?: string; href: string; imported: boolean; type?: ContentType; } export type ImportAssertionType = 'css' | 'json' | undefined; declare global { interface Window { dvlp: typeof testBrowser; } }