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

This commit is contained in:
Johannes Loher 2022-08-10 21:48:37 +02:00 committed by GitHub
parent d0030a7434
commit 3e67a7c836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View File

@ -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> { async function httpStatusCode(url: URL, ignoreHTTPSErrors: boolean, onStdErr: Reporter['onStdErr']): Promise<number> {
const commonRequestOptions = { headers: { Accept: '*/*' } };
const isHttps = url.protocol === 'https:'; const isHttps = url.protocol === 'https:';
const requestOptions = isHttps ? { const requestOptions = isHttps ? {
...commonRequestOptions,
rejectUnauthorized: !ignoreHTTPSErrors, rejectUnauthorized: !ignoreHTTPSErrors,
} : {}; } : commonRequestOptions;
return new Promise(resolve => { return new Promise(resolve => {
debugWebServer(`HTTP GET: ${url}`); debugWebServer(`HTTP GET: ${url}`);
(isHttps ? https : http).get(url, requestOptions, res => { (isHttps ? https : http).get(url, requestOptions, res => {

View File

@ -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)); 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 port = workerIndex + 10500;
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => { const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>'); 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)); 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 port = workerIndex + 10500;
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => { const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>'); 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({ const result = await runInlineTest({
'test.spec.js': ` 'test.spec.js': `
const { test } = pwt; const { test } = pwt;
@ -429,6 +429,34 @@ test(`should suport self signed certificate`, async ({ runInlineTest, httpsServe
expect(result.exitCode).toBe(0); 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 }) => { test('should create multiple servers', async ({ runInlineTest }, { workerIndex }) => {
const port = workerIndex + 10500; const port = workerIndex + 10500;
const result = await runInlineTest({ const result = await runInlineTest({