import { type WebPluginFn } from '../testing/web_plugin.js'; import { TestContext } from '../testing/test_context.js'; import type { NetworkMockOptions, NetworkMatch, NetworkRespondPayload, NetworkRespondDynamic } from './types.js'; import type { NetworkInterceptor } from './network_interceptor.js'; export type { NetworkMockOptions, NetworkMatch, CapturedRequest, NetworkRespondPayload, NetworkRespondDynamic, } from './types.js'; import { bypass } from './constants.js'; export type { NetworkInterceptor }; export type { NetworkPollingOptions } from './network_assert.js'; /** * Network-level error types which can be thrown when fulfilling network request. * Based on Playwright's NetworkError enum. */ export declare enum NetworkError { /** * An operation was aborted (due to user action). */ Aborted = "aborted", /** * Permission to access a resource, other than the network, was denied. */ AccessDenied = "accessdenied", /** * The IP address is unreachable. This usually means that there is no route to the specified host or network. */ AddressUnreachable = "addressunreachable", /** * The client chose to block the request. */ BlockedByClient = "blockedbyclient", /** * The request failed because the response was delivered along with requirements which are not met * (e.g., 'X-Frame-Options' or 'Content-Security-Policy'). */ BlockedByResponse = "blockedbyresponse", /** * A connection timed out as a result of not receiving an ACK for data sent. */ ConnectionAborted = "connectionaborted", /** * A connection was closed (corresponding to a TCP FIN). */ ConnectionClosed = "connectionclosed", /** * A connection attempt failed. */ ConnectionFailed = "connectionfailed", /** * A connection attempt was refused. */ ConnectionRefused = "connectionrefused", /** * A connection was reset (corresponding to a TCP RST). */ ConnectionReset = "connectionreset", /** * The Internet connection has been lost. */ InternetDisconnected = "internetdisconnected", /** * The host name could not be resolved. */ NameNotResolved = "namenotresolved", /** * An operation timed out. */ TimedOut = "timedout", /** * A generic failure occurred. */ Failed = "failed" } /** * The developer-facing API for network interception and mocking. * Available in browser tests via the `network` fixture on the `TestContext`. */ export declare class Network { private context; constructor(context: TestContext); /** * A sentinel value to indicate that a request should not be intercepted and should be allowed to proceed normally. * * You can use this value in dynamic response handlers to selectively bypass the mock and let the request * go through to the actual network. * * @example * ```ts * test('bypasses network', async ({ network }) => { * const mock = await network.mock('/api/data', () => { * // your logic to decide whether to bypass the mock * return network.bypass * }) * }) * ``` */ get bypass(): typeof bypass; /** * Collection of network error types which can be used to simulate network errors. * * @example * ```ts * test('simulates a network connection failure', async ({ network }) => { * const mock = await network.mock('/api/data', () => { * return { error: network.error.ConnectionFailed } * }) * }) * ``` */ get error(): typeof NetworkError; /** * Tells the network interceptor to automatically bypass CORS rules by intercepting * preflight OPTIONS requests and injecting `Access-Control-Allow-Origin: *` headers * into any fulfilled network mock. * * This setting automatically reverts at the end of the test. * * @example * ```ts * test('ignores CORS', async ({ network, assert }) => { * await network.ignoreCors() * const res = await fetch('https://example.com/api/data') * assert.equal(res.headers.get('access-control-allow-origin'), '*') * }) * ``` * * @param ignore Whether to ignore CORS or enforce it. Defaults to true. */ ignoreCors(ignore?: boolean): Promise; /** * Toggles the offline state of the browser context. * * > [!IMPORTANT] * > **Automatic Cleanup**: Unlike `emulation.setOffline(...)`, this method is scoped to the current test. * > The network offline state will automatically revert back to online mode at the end of the test. * * @example * ```ts * test('simulates offline behavior', async ({ network }) => { * await network.setOffline(true) * // verify application handles network failure... * }) * ``` * * @param offline Whether to set the network offline. */ setOffline(offline: boolean): Promise; /** * Registers a new network mock to intercept requests matching the provided criteria. * Intercepted requests can be bypassed, stubbed with static payloads, or handled by dynamic closures. * * Note: All mocks created during a test are automatically restored when the test finishes. * * @param match The matching criteria (e.g. URI string or request options). * @param respond The static payload or dynamic closure used to fulfill the matched request. * @returns A `NetworkInterceptor` to manage the mock and assert against captured requests. * * @example * ```ts * test('handles 500 error', async ({ network }) => { * const mock = await network.mock('/api/users', { * status: 500, * body: { error: 'Internal Server Error' } * }) * * button.click() * await mock.assert.calledOnce() * }) * ``` */ mock(match: NetworkMatch, respond: NetworkRespondPayload | NetworkRespondDynamic): Promise; /** * Registers a new network mock to intercept requests using a single configuration object. * Intercepted requests can be bypassed, stubbed with static payloads, or handled by dynamic closures. * * Note: All mocks created during a test are automatically restored when the test finishes. * * @param options The configuration defining the matching criteria and response behavior. * @returns A `NetworkInterceptor` to manage the mock and assert against captured requests. * * @example * ```ts * test('handles 500 error', async ({ network }) => { * const mock = await network.mock({ * match: '/api/users', * respond: { status: 500, body: { error: 'Internal Server Error' } } * }) * * button.click() * await mock.assert.calledOnce() * }) * ``` */ mock(options: NetworkMockOptions): Promise; } /** * Browser test plugin for network mocking support. * * Usage in config: * ```typescript * testPlugins: ['@pawel-up/lupa/network'] * ``` */ declare const setup: WebPluginFn; declare module '../testing/test_context.js' { interface TestContext { network: Network; } } export default setup;