Skip to content

Commit

Permalink
feat(plugins): send accept header in webServer url checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost91- committed Aug 4, 2022
1 parent 9cc735b commit 3e8956f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/playwright-test/src/plugins/webServerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
37 changes: 34 additions & 3 deletions tests/playwright-test/web-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>');
Expand Down Expand Up @@ -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>');
Expand Down Expand Up @@ -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;
Expand All @@ -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('<html><body>hello</body></html>');
});
await new Promise<void>(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({
Expand Down

0 comments on commit 3e8956f

Please sign in to comment.