From 3e8956fdc8cbc0dabfd14e795ba5fde875e2129d Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Thu, 4 Aug 2022 14:39:02 +0200 Subject: [PATCH] feat(plugins): send accept header in webServer url checking --- .../src/plugins/webServerPlugin.ts | 4 +- tests/playwright-test/web-server.spec.ts | 37 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/playwright-test/src/plugins/webServerPlugin.ts b/packages/playwright-test/src/plugins/webServerPlugin.ts index 7080a6fa1a423f..e867b5c8a2dde6 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 { + 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 8e6f008bb8fdf4..fc818ed7d5dd97 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('hello'); @@ -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('hello'); @@ -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,37 @@ test(`should suport self signed certificate`, async ({ runInlineTest, httpsServe expect(result.exitCode).toBe(0); }); +test('should send Accept header', async ({ runInlineTest }, { workerIndex }) => { + const port = workerIndex + 10500; + let acceptHeader: string | undefined | null = null; + const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => { + if (acceptHeader === null) acceptHeader = req.headers.accept; + res.end('hello'); + }); + await new Promise(resolve => server.listen(port, resolve)); + const result = await runInlineTest({ + 'test.spec.ts': ` + const { test } = pwt; + test('connect to the server', async ({baseURL, page}) => { + await page.goto('http://localhost:${port}/hello'); + expect(await page.textContent('body')).toBe('hello'); + }); + `, + 'playwright.config.ts': ` + module.exports = { + webServer: { + command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}', + url: 'http://localhost:${port}/hello', + reuseExistingServer: true, + } + }; + `, + }); + expect(result.exitCode).toBe(0); + expect(acceptHeader).toBe('*/*'); + await new Promise(resolve => server.close(resolve)); +}); + test('should create multiple servers', async ({ runInlineTest }, { workerIndex }) => { const port = workerIndex + 10500; const result = await runInlineTest({