Implementation:Getgauge Taiko Intercept Redirect
Overview
Intercept Redirect is the request redirection behavior of Taiko's intercept() function, which transparently forwards matching HTTP requests to an alternative URL.
Description
When intercept() is called with a URL pattern and a string as the second parameter, it registers a redirect interceptor. Any matching request is continued using the CDP Fetch.continueRequest method with the URL overridden to the specified redirect target. The redirect URL is validated before registration to ensure it is a well-formed URL.
The redirect is transparent to the web application — the response from the redirect target is delivered as if it came from the original URL. All other request properties (method, headers, body) are preserved during the redirect.
Usage
Request redirection is used when tests need to substitute one endpoint for another without the application being aware of the substitution. This is common when pointing production APIs to local mock servers or when replacing remote resources with local alternatives.
Code Reference
Source Location
- Redirect logic:
lib/handlers/fetchHandler.js:L102-111(string action branch inhandleInterceptor)
The relevant code path in handleInterceptor:
// When action is a string, validate and redirect
if (typeof action === 'string') {
// URL validation
new URL(action);
fetch.continueRequest({
requestId: event.requestId,
url: action
});
return;
}
Signature
intercept(requestUrl, redirectUrl)
Import
const { intercept } = require('taiko');
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
requestUrl |
string or RegExp |
Yes | URL pattern to match against outgoing requests. |
redirectUrl |
string |
Yes | The target URL to redirect matching requests to. Must be a valid URL. |
Outputs
| Return Type | Description |
|---|---|
Promise<void> |
Resolves when the redirect interceptor has been registered. |
Side Effect: Matching requests are forwarded to redirectUrl via Fetch.continueRequest with the URL override. The response from the redirect target is returned to the application as if it came from the original URL.
Usage Examples
Redirect API to staging server:
const { openBrowser, goto, intercept, closeBrowser } = require('taiko');
(async () => {
await openBrowser();
// Redirect production API calls to staging
await intercept('https://api.production.com/v1/users',
'https://api.staging.com/v1/users');
await goto('https://example.com');
await closeBrowser();
})();
Replace CDN resource with local version:
// Serve a modified JavaScript file from a local server
await intercept('https://cdn.example.com/app.js',
'http://localhost:8080/modified-app.js');
await goto('https://example.com');
Redirect to mock API server:
// Point all API requests to a local mock server
await intercept('https://api.example.com/v2/products',
'http://localhost:3001/mock/products');
await intercept('https://api.example.com/v2/orders',
'http://localhost:3001/mock/orders');
await goto('https://example.com');
Test API version migration:
// Verify application works with v2 API by redirecting v1 calls
await intercept('https://api.example.com/v1/data',
'https://api.example.com/v2/data');
await goto('https://example.com');
// Assert that the application still functions correctly