test: isolate proxy creds between context

This commit is contained in:
Max Schmitt 2024-07-05 11:35:07 +02:00
parent 1c69d3e175
commit 084b853383

View file

@ -14,7 +14,9 @@
* limitations under the License.
*/
import http from 'http';
import { browserTest as it, expect } from '../config/browserTest';
import type net from 'net';
it.skip(({ mode }) => mode.startsWith('service'));
@ -413,3 +415,43 @@ it('does launch without a port', async ({ contextFactory }) => {
});
await context.close();
});
it('should isolate proxy credentials between contexts on navigation', async ({ contextFactory, browserName }) => {
it.fixme(browserName === 'firefox', 'https://github.com/microsoft/playwright/issues/31525');
const server = http.createServer((req, res) => {
const authHeader = req.headers['proxy-authorization'];
if (!authHeader) {
res.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="proxy"' });
res.end('Proxy authorization required');
return;
}
const [username,] = Buffer.from(authHeader.split(' ')[1], 'base64').toString().split(':');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`Hello <div data-testid=user>${username}</div>!\n`);
});
const serverURL = await new Promise<string>(resolve => server.listen(0, 'localhost', () => {
resolve(`http://localhost:${(server.address() as net.AddressInfo).port}`);
}));
const context1 = await contextFactory({
proxy: { server: serverURL, username: 'user1', password: 'secret1' }
});
const page1 = await context1.newPage();
await page1.goto('http://non-existent.com/target.html');
await expect(page1.getByTestId('user')).toHaveText('user1');
const context2 = await contextFactory({
proxy: { server: serverURL, username: 'user2', password: 'secret2' }
});
const page2 = await context2.newPage();
await page2.goto('http://non-existent.com/target.html');
await expect(page2.getByTestId('user')).toHaveText('user2');
await page1.goto('http://non-existent.com/target.html');
await expect(page1.getByTestId('user')).toHaveText('user1');
await new Promise<void>(resolve => server.close(() => resolve()));
});