Skip to content

Commit

Permalink
fix(webServer): follow relative redirects when checking the url (#22035)
Browse files Browse the repository at this point in the history
Fixes #22144
  • Loading branch information
ghost91- authored Apr 5, 2023
1 parent d7b3836 commit bd698ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/src/test-api/class-testconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ export default defineConfig({
- type: ?<[Object]|[Array]<[Object]>>
- `command` <[string]> Shell command to start. For example `npm run start`..
- `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.
- `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.
- `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.
- `ignoreHTTPSErrors` ?<[boolean]> Whether to ignore HTTPS errors when fetching the `url`. Defaults to `false`.
- `timeout` ?<[int]> How long to wait for the process to start up and be available in milliseconds. Defaults to 60000.
- `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.
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
const requestCallback = (res: http.IncomingMessage) => {
const statusCode = res.statusCode || 0;
if (statusCode >= 300 && statusCode < 400 && res.headers.location)
httpRequest({ ...params, url: res.headers.location }, onResponse, onError);
httpRequest({ ...params, url: new URL.URL(res.headers.location, params.url).toString() }, onResponse, onError);
else
onResponse(res);
};
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-test/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5911,7 +5911,8 @@ interface TestConfigWebServer {

/**
* 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.
* 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.
*/
url?: string;

Expand Down
26 changes: 26 additions & 0 deletions tests/playwright-test/web-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,32 @@ test('should send Accept header', async ({ runInlineTest, server }) => {
expect(acceptHeader).toBe('*/*');
});

test('should follow redirects', async ({ runInlineTest, server }) => {
server.setRedirect('/redirect', '/redirected-to');
server.setRoute('/redirected-to', (req, res) => {
res.end('<html><body>hello</body></html>');
});
const result = await runInlineTest({
'test.spec.ts': `
import { test, expect } from '@playwright/test';
test('connect to the server', async ({baseURL, page}) => {
await page.goto('http://localhost:${server.PORT}/redirect');
expect(await page.textContent('body')).toBe('hello');
});
`,
'playwright.config.ts': `
module.exports = {
webServer: {
command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${server.PORT}',
url: 'http://localhost:${server.PORT}/redirect',
reuseExistingServer: true,
}
};
`,
});
expect(result.exitCode).toBe(0);
});

test('should create multiple servers', async ({ runInlineTest }, { workerIndex }) => {
const port = workerIndex * 2 + 10500;
const result = await runInlineTest({
Expand Down

0 comments on commit bd698ef

Please sign in to comment.