Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(plugins): send accept header in webServer url checking (#16251)

Johannes Loher3e67a7c8363c
diff --git a/packages/playwright-test/src/plugins/webServerPlugin.ts b/packages/playwright-test/src/plugins/webServerPlugin.ts
index 7080a6fa1a..e867b5c8a2 100644
--- a/packages/playwright-test/src/plugins/webServerPlugin.ts
+++ b/packages/playwright-test/src/plugins/webServerPlugin.ts
@@ -158,10 +158,12 @@ async function isURLAvailable(url: URL, ignoreHTTPSErrors: boolean, onStdErr: Re
}
async function httpStatusCode(url: URL, ignoreHTTPSErrors: boolean, onStdErr: Reporter['onStdErr']): Promise<number> {
+ const commonRequestOptions = { headers: { Accept: '*/*' } };
const isHttps = url.protocol === 'https:';
const requestOptions = isHttps ? {
+ ...commonRequestOptions,
rejectUnauthorized: !ignoreHTTPSErrors,
- } : {};
+ } : commonRequestOptions;
return new Promise(resolve => {
debugWebServer(`HTTP GET: ${url}`);
(isHttps ? https : http).get(url, requestOptions, res => {
diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts
index 8e6f008bb8..e70b197b4d 100644
--- a/tests/playwright-test/web-server.spec.ts
+++ b/tests/playwright-test/web-server.spec.ts
@@ -312,7 +312,7 @@ test('should be able to specify a custom baseURL with the server', async ({ runI
await new Promise(resolve => server.close(resolve));
});
-test('should be able to use an existing server when reuseExistingServer:true ', async ({ runInlineTest }, { workerIndex }) => {
+test('should be able to use an existing server when reuseExistingServer:true', async ({ runInlineTest }, { workerIndex }) => {
const port = workerIndex + 10500;
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>');
@@ -345,7 +345,7 @@ test('should be able to use an existing server when reuseExistingServer:true ',
await new Promise(resolve => server.close(resolve));
});
-test('should throw when a server is already running on the given port and strict is true ', async ({ runInlineTest }, { workerIndex }) => {
+test('should throw when a server is already running on the given port and strict is true', async ({ runInlineTest }, { workerIndex }) => {
const port = workerIndex + 10500;
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>');
@@ -410,7 +410,7 @@ for (const host of ['localhost', '127.0.0.1', '0.0.0.0']) {
});
}
-test(`should suport self signed certificate`, async ({ runInlineTest, httpsServer }) => {
+test(`should support self signed certificate`, async ({ runInlineTest, httpsServer }) => {
const result = await runInlineTest({
'test.spec.js': `
const { test } = pwt;
@@ -429,6 +429,34 @@ test(`should suport self signed certificate`, async ({ runInlineTest, httpsServe
expect(result.exitCode).toBe(0);
});
+test('should send Accept header', async ({ runInlineTest, server }) => {
+ let acceptHeader: string | undefined | null = null;
+ server.setRoute('/hello', (req, res) => {
+ if (acceptHeader === null) acceptHeader = req.headers.accept;
+ res.end('<html><body>hello</body></html>');
+ });
+ const result = await runInlineTest({
+ 'test.spec.ts': `
+ const { test } = pwt;
+ test('connect to the server', async ({baseURL, page}) => {
+ await page.goto('http://localhost:${server.PORT}/hello');
+ 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}/hello',
+ reuseExistingServer: true,
+ }
+ };
+ `,
+ });
+ expect(result.exitCode).toBe(0);
+ expect(acceptHeader).toBe('*/*');
+});
+
test('should create multiple servers', async ({ runInlineTest }, { workerIndex }) => {
const port = workerIndex + 10500;
const result = await runInlineTest({