Skip to content

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

test: use less localhost for cookies test (#36508)

Max Schmitt3f15a6b6fce1
diff --git a/tests/config/proxy.ts b/tests/config/proxy.ts
index 67067f8885..9fdf336bc4 100644
--- a/tests/config/proxy.ts
+++ b/tests/config/proxy.ts
@@ -27,6 +27,7 @@ const kConnectHostsToIgnore = new Set([
]);
export class TestProxy {
+ readonly HOST: string;
readonly PORT: number;
readonly URL: string;
@@ -47,6 +48,7 @@ export class TestProxy {
private constructor(port: number) {
this.PORT = port;
this.URL = `http://localhost:${port}`;
+ this.HOST = new URL(this.URL).host;
this._server = createProxy();
this._server.on('connection', socket => this._onSocket(socket));
}
diff --git a/tests/config/testserver/index.ts b/tests/config/testserver/index.ts
index 44745d6e1b..ddd334e90e 100644
--- a/tests/config/testserver/index.ts
+++ b/tests/config/testserver/index.ts
@@ -55,6 +55,7 @@ export class TestServer {
readonly CROSS_PROCESS_PREFIX: string;
readonly EMPTY_PAGE: string;
readonly HOST: string;
+ readonly HOSTNAME: string;
static async create(dirPath: string, port: number, loopback?: string): Promise<TestServer> {
const server = new TestServer(dirPath, port, loopback);
@@ -120,6 +121,7 @@ export class TestServer {
this.CROSS_PROCESS_PREFIX = `${protocol}://${cross_origin}:${port}`;
this.EMPTY_PAGE = `${protocol}://${same_origin}:${port}/empty.html`;
this.HOST = new URL(this.EMPTY_PAGE).host;
+ this.HOSTNAME = new URL(this.EMPTY_PAGE).hostname;
}
async waitUntilReady() {
diff --git a/tests/library/browsercontext-add-cookies.spec.ts b/tests/library/browsercontext-add-cookies.spec.ts
index bf3a1a4d90..8dceb50122 100644
--- a/tests/library/browsercontext-add-cookies.spec.ts
+++ b/tests/library/browsercontext-add-cookies.spec.ts
@@ -300,7 +300,7 @@ it('should set cookie with reasonable defaults', async ({ context, server, defau
expect(cookies.sort((a, b) => a.name.localeCompare(b.name))).toEqual([{
name: 'defaults',
value: '123456',
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
@@ -312,7 +312,7 @@ it('should set cookie with reasonable defaults', async ({ context, server, defau
it('should set a cookie with a path', async ({ context, page, server, browserName, isWindows }) => {
await page.goto(server.PREFIX + '/grid.html');
await context.addCookies([{
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/grid.html',
name: 'gridcookie',
value: 'GRID',
@@ -321,7 +321,7 @@ it('should set a cookie with a path', async ({ context, page, server, browserNam
expect(await context.cookies()).toEqual([{
name: 'gridcookie',
value: 'GRID',
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/grid.html',
expires: -1,
httpOnly: false,
@@ -459,8 +459,8 @@ it('should set secure cookies on secure WebSocket', async ({ contextFactory, htt
const context = await contextFactory({ ignoreHTTPSErrors: true });
const page = await context.newPage();
await page.goto(httpsServer.EMPTY_PAGE);
- await context.addCookies([{ domain: 'localhost', path: '/', name: 'foo', value: 'bar', secure: true }]);
- await page.evaluate(port => new WebSocket(`wss://localhost:${port}/ws`), httpsServer.PORT);
+ await context.addCookies([{ domain: new URL(httpsServer.PREFIX).hostname, path: '/', name: 'foo', value: 'bar', secure: true }]);
+ await page.evaluate(hostname => new WebSocket(`wss://${hostname}/ws`), httpsServer.HOST);
const headers = await receivedWebSocketHeaders;
expect(headers.cookie).toBe('foo=bar');
});
diff --git a/tests/library/browsercontext-cookies-third-party.spec.ts b/tests/library/browsercontext-cookies-third-party.spec.ts
index fcb8083057..41d8b74f69 100644
--- a/tests/library/browsercontext-cookies-third-party.spec.ts
+++ b/tests/library/browsercontext-cookies-third-party.spec.ts
@@ -323,7 +323,7 @@ test(`add 'Partitioned;' cookie via API`, async ({ page, context, browserName, h
{
name: 'top-level-partitioned',
value: 'value',
- domain: 'localhost',
+ domain: httpsServer.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
@@ -335,7 +335,7 @@ test(`add 'Partitioned;' cookie via API`, async ({ page, context, browserName, h
{
name: 'top-level-non-partitioned',
value: 'value',
- domain: 'localhost',
+ domain: httpsServer.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
@@ -345,7 +345,7 @@ test(`add 'Partitioned;' cookie via API`, async ({ page, context, browserName, h
{
name: 'frame-partitioned',
value: 'value',
- domain: 'localhost',
+ domain: httpsServer.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
@@ -357,7 +357,7 @@ test(`add 'Partitioned;' cookie via API`, async ({ page, context, browserName, h
{
name: 'frame-non-partitioned',
value: 'value',
- domain: 'localhost',
+ domain: httpsServer.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
diff --git a/tests/library/browsercontext-cookies.spec.ts b/tests/library/browsercontext-cookies.spec.ts
index 1fe4651b8c..15962d68f2 100644
--- a/tests/library/browsercontext-cookies.spec.ts
+++ b/tests/library/browsercontext-cookies.spec.ts
@@ -31,7 +31,7 @@ it('should get a cookie @smoke', async ({ context, page, server, defaultSameSite
expect(await context.cookies()).toEqual([{
name: 'username',
value: 'John Doe',
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
@@ -55,7 +55,7 @@ it('should get a non-session cookie', async ({ context, page, server, defaultSam
expect(cookies[0]).toEqual({
name: 'username',
value: 'John Doe',
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/',
// We will check this separately.
expires: expect.anything(),
@@ -122,7 +122,7 @@ it('should get multiple cookies', async ({ context, page, server, defaultSameSit
{
name: 'password',
value: '1234',
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
@@ -132,7 +132,7 @@ it('should get multiple cookies', async ({ context, page, server, defaultSameSit
{
name: 'username',
value: 'John Doe',
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/',
expires: -1,
httpOnly: false,
@@ -391,7 +391,7 @@ it('should parse cookie with large Max-Age correctly', async ({ server, page, de
{
name: 'cookie1',
value: 'value1',
- domain: 'localhost',
+ domain: server.HOSTNAME,
path: '/',
expires: expect.any(Number),
httpOnly: false,
diff --git a/tests/library/browsercontext-proxy.spec.ts b/tests/library/browsercontext-proxy.spec.ts
index 7a68a3c318..6cf894f7b4 100644
--- a/tests/library/browsercontext-proxy.spec.ts
+++ b/tests/library/browsercontext-proxy.spec.ts
@@ -32,7 +32,7 @@ it('should work when passing the proxy only on the context level', async ({ brow
proxy: undefined,
});
const context = await browser.newContext({
- proxy: { server: `localhost:${proxyServer.PORT}` }
+ proxy: { server: proxyServer.HOST }
});
const page = await context.newPage();
@@ -55,7 +55,7 @@ it('should throw for bad server value', async ({ contextFactory }) => {
it('should use proxy', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}` }
+ proxy: { server: proxyServer.HOST }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
@@ -67,7 +67,7 @@ it('should use proxy', async ({ contextFactory, server, proxyServer }) => {
it('should send secure cookies to subdomain.localhost', async ({ contextFactory, browserName, server, isWindows, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}` },
+ proxy: { server: proxyServer.HOST },
});
server.setRoute('/set-cookie.html', async (req, res) => {
res.setHeader('Set-Cookie', [`non-secure=1; HttpOnly`, `secure=1; HttpOnly; Secure`]);
@@ -107,7 +107,7 @@ it('should set cookie for top-level domain', {
proxyServer.forwardTo(server.PORT, { allowConnectRequests: true });
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}` }
+ proxy: { server: proxyServer.HOST }
});
server.setRoute('/empty.html', (req, res) => {
res.setHeader('Set-Cookie', `name=val; Domain=codes; Path=/;`);
@@ -150,7 +150,7 @@ it.describe('should proxy local network requests', () => {
const url = `http://${params.target}:55555${path}`;
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}`, bypass: additionalBypass ? '1.non.existent.domain.for.the.test' : undefined }
+ proxy: { server: proxyServer.HOST, bypass: additionalBypass ? '1.non.existent.domain.for.the.test' : undefined }
});
const page = await context.newPage();
@@ -188,7 +188,7 @@ it('should use ipv6 proxy', async ({ contextFactory, server, proxyServer, browse
it('should use proxy twice', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}` }
+ proxy: { server: proxyServer.HOST }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
@@ -202,7 +202,7 @@ it('should use proxy twice', async ({ contextFactory, server, proxyServer }) =>
it('should use proxy for second page', async ({ contextFactory, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT);
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}` }
+ proxy: { server: proxyServer.HOST }
});
const page = await context.newPage();
@@ -226,7 +226,7 @@ it('should use proxy for https urls', async ({ contextFactory, httpsServer, prox
proxyServer.forwardTo(httpsServer.PORT, { allowConnectRequests: true });
const context = await contextFactory({
ignoreHTTPSErrors: true,
- proxy: { server: `localhost:${proxyServer.PORT}` }
+ proxy: { server: proxyServer.HOST }
});
const page = await context.newPage();
await page.goto('https://non-existent.com/target.html');
@@ -269,7 +269,7 @@ it('should authenticate', async ({ contextFactory, server, proxyServer }) => {
return !!auth;
});
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' }
+ proxy: { server: proxyServer.HOST, username: 'user', password: 'secret' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
@@ -287,7 +287,7 @@ it('should authenticate with empty password', async ({ contextFactory, server, p
return !!auth;
});
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: '' }
+ proxy: { server: proxyServer.HOST, username: 'user', password: '' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
@@ -305,7 +305,7 @@ it('should isolate proxy credentials between contexts', async ({ contextFactory,
});
{
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user1', password: 'secret1' }
+ proxy: { server: proxyServer.HOST, username: 'user1', password: 'secret1' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
@@ -316,7 +316,7 @@ it('should isolate proxy credentials between contexts', async ({ contextFactory,
auth = undefined;
{
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user2', password: 'secret2' }
+ proxy: { server: proxyServer.HOST, username: 'user2', password: 'secret2' }
});
const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
@@ -333,7 +333,7 @@ it('should exclude patterns', async ({ contextFactory, server, proxyServer }) =>
//
// @see https://gist.github.com/CollinChaffin/24f6c9652efb3d6d5ef2f5502720ef00
const context = await contextFactory({
- proxy: { server: `localhost:${proxyServer.PORT}`, bypass: '1.non.existent.domain.for.the.test, 2.non.existent.domain.for.the.test, .another.test' }
+ proxy: { server: proxyServer.HOST, bypass: '1.non.existent.domain.for.the.test, 2.non.existent.domain.for.the.test, .another.test' }
});
const nonFaviconUrls = () => {
diff --git a/tests/library/capabilities.spec.ts b/tests/library/capabilities.spec.ts
index 23cb4ac399..9533021d7e 100644
--- a/tests/library/capabilities.spec.ts
+++ b/tests/library/capabilities.spec.ts
@@ -39,14 +39,14 @@ it('Web Assembly should work @smoke', async ({ page, server, browserName, platfo
it('WebSocket should work @smoke', async ({ page, server }) => {
server.sendOnWebSocketConnection('incoming');
- const value = await page.evaluate(port => {
+ const value = await page.evaluate(host => {
let cb;
const result = new Promise(f => cb = f);
- const ws = new WebSocket('ws://localhost:' + port + '/ws');
+ const ws = new WebSocket('ws://' + host + '/ws');
ws.addEventListener('message', data => { ws.close(); cb(data.data); });
ws.addEventListener('error', error => cb('Error'));
return result;
- }, server.PORT);
+ }, server.HOST);
expect(value).toBe('incoming');
});