Skip to content

Commit cb419e7

Browse files
cherry-pick(#22035): fix(webServer): follow relative redirects when checking the url (#22204)
This PR cherry-picks the following commits: - bd698ef Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent ce33ec2 commit cb419e7

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

docs/src/test-api/class-testconfig.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ export default defineConfig({
595595
- type: ?<[Object]|[Array]<[Object]>>
596596
- `command` <[string]> Shell command to start. For example `npm run start`..
597597
- `port` ?<[int]> The port that your http server is expected to appear on. It does wait until it accepts connections. Exactly one of `port` or `url` is required.
598-
- `url` ?<[string]> The url on your http server that is expected to return a 2xx, 3xx, 400, 401, 402, or 403 status code when the server is ready to accept connections. Exactly one of `port` or `url` is required.
598+
- `url` ?<[string]> The url on your http server that is expected to return a 2xx, 3xx, 400, 401, 402, or 403 status code when the server is ready to accept connections. Redirects (3xx status codes) are being followed and the new location is checked. Exactly one of `port` or `url` is required.
599599
- `ignoreHTTPSErrors` ?<[boolean]> Whether to ignore HTTPS errors when fetching the `url`. Defaults to `false`.
600600
- `timeout` ?<[int]> How long to wait for the process to start up and be available in milliseconds. Defaults to 60000.
601601
- `reuseExistingServer` ?<[boolean]> If true, it will re-use an existing server on the `port` or `url` when available. If no server is running on that `port` or `url`, it will run the command to start a new server. If `false`, it will throw if an existing process is listening on the `port` or `url`. This should be commonly set to `!process.env.CI` to allow the local dev server when running tests locally.

packages/playwright-core/src/utils/network.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
8080
const requestCallback = (res: http.IncomingMessage) => {
8181
const statusCode = res.statusCode || 0;
8282
if (statusCode >= 300 && statusCode < 400 && res.headers.location)
83-
httpRequest({ ...params, url: res.headers.location }, onResponse, onError);
83+
httpRequest({ ...params, url: new URL.URL(res.headers.location, params.url).toString() }, onResponse, onError);
8484
else
8585
onResponse(res);
8686
};

packages/playwright-test/types/test.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5887,7 +5887,8 @@ interface TestConfigWebServer {
58875887

58885888
/**
58895889
* The url on your http server that is expected to return a 2xx, 3xx, 400, 401, 402, or 403 status code when the
5890-
* server is ready to accept connections. Exactly one of `port` or `url` is required.
5890+
* server is ready to accept connections. Redirects (3xx status codes) are being followed and the new location is
5891+
* checked. Exactly one of `port` or `url` is required.
58915892
*/
58925893
url?: string;
58935894

tests/playwright-test/web-server.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,32 @@ test('should send Accept header', async ({ runInlineTest, server }) => {
457457
expect(acceptHeader).toBe('*/*');
458458
});
459459

460+
test('should follow redirects', async ({ runInlineTest, server }) => {
461+
server.setRedirect('/redirect', '/redirected-to');
462+
server.setRoute('/redirected-to', (req, res) => {
463+
res.end('<html><body>hello</body></html>');
464+
});
465+
const result = await runInlineTest({
466+
'test.spec.ts': `
467+
import { test, expect } from '@playwright/test';
468+
test('connect to the server', async ({baseURL, page}) => {
469+
await page.goto('http://localhost:${server.PORT}/redirect');
470+
expect(await page.textContent('body')).toBe('hello');
471+
});
472+
`,
473+
'playwright.config.ts': `
474+
module.exports = {
475+
webServer: {
476+
command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${server.PORT}',
477+
url: 'http://localhost:${server.PORT}/redirect',
478+
reuseExistingServer: true,
479+
}
480+
};
481+
`,
482+
});
483+
expect(result.exitCode).toBe(0);
484+
});
485+
460486
test('should create multiple servers', async ({ runInlineTest }, { workerIndex }) => {
461487
const port = workerIndex * 2 + 10500;
462488
const result = await runInlineTest({

0 commit comments

Comments
 (0)