Network
Tapsmith supports Playwright-style network interception. Route handlers let you mock, modify, or abort HTTP/HTTPS requests made by the app under test.
device.route(url, handler, options?): Promise<void>
Section titled “device.route(url, handler, options?): Promise<void>”Intercept network requests matching a URL pattern. Requires network tracing to be enabled (set trace to any mode other than 'off' with network: true, which is the default). Without it, the MITM proxy that intercepts traffic is not active and route handlers will never fire.
See also: device.unroute(), device.unrouteAll().
url:string | RegExp | ((url: URL) => boolean)— URL pattern (glob), regex, or predicatehandler:(route: Route) => Promise<void> | void— handler that decides how to handle the requestoptions.times?:number— how many times to intercept (then auto-remove)
await device.route('**/api/posts*', async (route) => { await route.fulfill({ json: [{ id: 1, title: 'Mocked' }] })})device.unroute(url, handler?): Promise<void>
Section titled “device.unroute(url, handler?): Promise<void>”Remove a previously registered route handler. If handler is omitted, all handlers for the pattern are removed.
device.unrouteAll(): Promise<void>
Section titled “device.unrouteAll(): Promise<void>”Remove all registered route handlers.
device.waitForRequest(urlOrPredicate, options?): Promise<TapsmithRequest>
Section titled “device.waitForRequest(urlOrPredicate, options?): Promise<TapsmithRequest>”Wait for a network request matching the pattern. Requires network tracing to be enabled (same prerequisite as device.route()).
urlOrPredicate:string | RegExp | ((request: TapsmithRequest) => boolean)options.timeout?:number— timeout in ms (default: device timeout)
device.waitForResponse(urlOrPredicate, options?): Promise<NetworkResponseEventData>
Section titled “device.waitForResponse(urlOrPredicate, options?): Promise<NetworkResponseEventData>”Wait for a network response matching the pattern. Requires network tracing to be enabled (same prerequisite as device.route()).
device.on(event, handler): void
Section titled “device.on(event, handler): void”Subscribe to network events: 'request' or 'response'.
device.on('request', (req) => console.log(req.url))device.on('response', (resp) => console.log(resp.status))device.off(event, handler): void
Section titled “device.off(event, handler): void”Unsubscribe from network events.
The Route object is passed to route handlers. It provides methods to decide how to handle the intercepted request.
route.request(): TapsmithRequest
Section titled “route.request(): TapsmithRequest”Returns the intercepted request.
route.abort(errorCode?): Promise<void>
Section titled “route.abort(errorCode?): Promise<void>”Abort the request. Optional errorCode: 'connectionrefused', 'connectionreset', 'timedout'.
route.continue(overrides?): Promise<void>
Section titled “route.continue(overrides?): Promise<void>”Continue the request to the server with optional modifications.
overrides.url?:string— override the request URL. Supports both same-origin path changes (e.g./v2/posts) and cross-origin redirection (e.g.https://staging.example.com/api/posts). When the host differs, theHostheader is automatically updated.overrides.method?:string— override the HTTP methodoverrides.headers?:Record<string, string>— override headersoverrides.postData?:string | Buffer— override request body
route.fulfill(options?): Promise<void>
Section titled “route.fulfill(options?): Promise<void>”Return a mock response without contacting the server.
options.status?:number— HTTP status code (default: 200)options.headers?:Record<string, string>— response headersoptions.body?:string | Buffer— response bodyoptions.contentType?:string— content-type headeroptions.json?:unknown— convenience: JSON-serializes and sets content-typeoptions.path?:string— read body from a file
route.fetch(overrides?): Promise<FetchedAPIResponse>
Section titled “route.fetch(overrides?): Promise<FetchedAPIResponse>”Fetch the actual response from the server. Returns a FetchedAPIResponse that you can inspect and modify before calling route.fulfill().
overrides.url?:string— override the URL to fetch from (supports cross-origin, same asroute.continue())overrides.method?:string— override the HTTP methodoverrides.headers?:Record<string, string>— override headersoverrides.postData?:string | Buffer— override request body
await device.route('**/api/users/*', async (route) => { const response = await route.fetch() const data = response.json() data.name = 'Modified' await route.fulfill({ json: data })})TapsmithRequest
Section titled “TapsmithRequest”Properties: method, url, headers, postData, isHttps.
FetchedAPIResponse
Section titled “FetchedAPIResponse”Returned by route.fetch(). Properties: status, headers. Methods: body(), text(), json().