test: make borwsercontext-proxy tests use test proxy server (#8318)

This commit is contained in:
Yury Semikhatsky 2021-08-19 13:34:32 -07:00 committed by GitHub
parent 434d07e711
commit 356d69ae3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 91 deletions

View File

@ -18,6 +18,12 @@ import { browserTest as it, expect } from './config/browserTest';
it.use({ proxy: { server: 'per-context' } }); it.use({ proxy: { server: 'per-context' } });
it.beforeEach(({ server }) => {
server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
});
it('should throw for missing global proxy on Chromium Windows', async ({ browserName, platform, browserType, browserOptions, server }) => { it('should throw for missing global proxy on Chromium Windows', async ({ browserName, platform, browserType, browserOptions, server }) => {
it.skip(browserName !== 'chromium' || platform !== 'win32'); it.skip(browserName !== 'chromium' || platform !== 'win32');
@ -34,14 +40,12 @@ it('should throw for missing global proxy on Chromium Windows', async ({ browser
} }
}); });
it('should work when passing the proxy only on the context level', async ({browserName, platform, browserType, browserOptions, contextOptions, server}) => { it('should work when passing the proxy only on the context level', async ({browserName, platform, browserType, browserOptions, contextOptions, server, proxyServer}) => {
// Currently an upstream bug in the network stack of Chromium which leads that // Currently an upstream bug in the network stack of Chromium which leads that
// the wrong proxy gets used in the BrowserContext. // the wrong proxy gets used in the BrowserContext.
it.fixme(browserName === 'chromium' && platform === 'win32'); it.fixme(browserName === 'chromium' && platform === 'win32');
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
res.end('<html><title>Served by the proxy</title></html>');
});
let browser; let browser;
try { try {
browser = await browserType.launch({ browser = await browserType.launch({
@ -50,11 +54,12 @@ it('should work when passing the proxy only on the context level', async ({brows
}); });
const context = await browser.newContext({ const context = await browser.newContext({
...contextOptions, ...contextOptions,
proxy: { server: `localhost:${server.PORT}` } proxy: { server: `localhost:${proxyServer.PORT}` }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by the proxy');
} finally { } finally {
await browser.close(); await browser.close();
@ -69,47 +74,47 @@ it('should throw for bad server value', async ({ contextFactory }) => {
expect(error.message).toContain('proxy.server: expected string, got number'); expect(error.message).toContain('proxy.server: expected string, got number');
}); });
it('should use proxy', async ({ contextFactory, server }) => { it('should use proxy', async ({ contextFactory, server, proxyServer }) => {
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
res.end('<html><title>Served by the proxy</title></html>');
});
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}` } proxy: { server: `localhost:${proxyServer.PORT}` }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by the proxy');
await context.close(); await context.close();
}); });
it('should use proxy twice', async ({ contextFactory, server }) => { it('should use proxy twice', async ({ contextFactory, server, proxyServer }) => {
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
res.end('<html><title>Served by the proxy</title></html>');
});
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}` } proxy: { server: `localhost:${proxyServer.PORT}` }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
await page.goto('http://non-existent-2.com/target.html'); await page.goto('http://non-existent-2.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent-2.com/target.html');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by the proxy');
await context.close(); await context.close();
}); });
it('should use proxy for second page', async ({contextFactory, server}) => { it('should use proxy for second page', async ({contextFactory, server, proxyServer}) => {
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
res.end('<html><title>Served by the proxy</title></html>');
});
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}` } proxy: { server: `localhost:${proxyServer.PORT}` }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by the proxy');
const page2 = await context.newPage(); const page2 = await context.newPage();
proxyServer.requestUrls = [];
await page2.goto('http://non-existent.com/target.html'); await page2.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page2.title()).toBe('Served by the proxy'); expect(await page2.title()).toBe('Served by the proxy');
await context.close(); await context.close();
@ -117,33 +122,28 @@ it('should use proxy for second page', async ({contextFactory, server}) => {
it('should use proxy for https urls', async ({ contextFactory, server, httpsServer, proxyServer }) => { it('should use proxy for https urls', async ({ contextFactory, server, httpsServer, proxyServer }) => {
httpsServer.setRoute('/target.html', async (req, res) => { httpsServer.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>'); res.end('<html><title>Served by https server via proxy</title></html>');
});
let connectedToProxy = false;
proxyServer.onConnect(req => {
req.url = `localhost:${httpsServer.PORT}`;
connectedToProxy = true;
}); });
proxyServer.forwardTo(httpsServer.PORT);
const context = await contextFactory({ const context = await contextFactory({
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
proxy: { server: `localhost:${proxyServer.PORT}` } proxy: { server: `localhost:${proxyServer.PORT}` }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('https://non-existent.com/target.html'); await page.goto('https://non-existent.com/target.html');
expect(connectedToProxy).toBeTruthy(); expect(proxyServer.connectHosts).toContain('non-existent.com:443');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by https server via proxy');
await context.close(); await context.close();
}); });
it('should work with IP:PORT notion', async ({contextFactory, server}) => { it('should work with IP:PORT notion', async ({contextFactory, server, proxyServer}) => {
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
res.end('<html><title>Served by the proxy</title></html>');
});
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `127.0.0.1:${server.PORT}` } proxy: { server: `127.0.0.1:${proxyServer.PORT}` }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by the proxy');
await context.close(); await context.close();
}); });
@ -162,117 +162,112 @@ it('should throw for socks4 authentication', async ({contextFactory}) => {
expect(error.message).toContain('Socks4 proxy protocol does not support authentication'); expect(error.message).toContain('Socks4 proxy protocol does not support authentication');
}); });
it('should authenticate', async ({contextFactory, server}) => { it('should authenticate', async ({contextFactory, server, proxyServer}) => {
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
const auth = req.headers['proxy-authorization']; let auth;
if (!auth) { proxyServer.setAuthHandler(req => {
res.writeHead(407, 'Proxy Authentication Required', { auth = req.headers['proxy-authorization'];
'Proxy-Authenticate': 'Basic realm="Access to internal site"' return !!auth;
});
res.end();
} else {
res.end(`<html><title>${auth}</title></html>`);
}
}); });
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}`, username: 'user', password: 'secret' } proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Basic ' + Buffer.from('user:secret').toString('base64')); expect(proxyServer.requestUrls).toContain('http://non-existent.com/target.html');
expect(auth).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));
expect(await page.title()).toBe('Served by the proxy');
await context.close(); await context.close();
}); });
it('should authenticate with empty password', async ({contextFactory, server}) => { it('should authenticate with empty password', async ({contextFactory, server, proxyServer}) => {
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
const auth = req.headers['proxy-authorization']; let auth;
if (!auth) { proxyServer.setAuthHandler(req => {
res.writeHead(407, 'Proxy Authentication Required', { auth = req.headers['proxy-authorization'];
'Proxy-Authenticate': 'Basic realm="Access to internal site"' return !!auth;
});
res.end();
} else {
res.end(`<html><title>${auth}</title></html>`);
}
}); });
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}`, username: 'user', password: '' } proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: '' }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Basic ' + Buffer.from('user:').toString('base64')); expect(auth).toBe('Basic ' + Buffer.from('user:').toString('base64'));
expect(await page.title()).toBe('Served by the proxy');
await context.close(); await context.close();
}); });
it('should isolate proxy credentials between contexts', async ({contextFactory, server, browserName}) => { it('should isolate proxy credentials between contexts', async ({contextFactory, server, browserName, proxyServer}) => {
it.fixme(browserName === 'firefox', 'Credentials from the first context stick around'); it.fixme(browserName === 'firefox', 'Credentials from the first context stick around');
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
const auth = req.headers['proxy-authorization']; let auth;
if (!auth) { proxyServer.setAuthHandler(req => {
res.writeHead(407, 'Proxy Authentication Required', { auth = req.headers['proxy-authorization'];
'Proxy-Authenticate': 'Basic realm="Access to internal site"' return !!auth;
});
res.end();
} else {
res.end(`<html><title>${auth}</title></html>`);
}
}); });
{ {
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}`, username: 'user1', password: 'secret1' } proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user1', password: 'secret1' }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Basic ' + Buffer.from('user1:secret1').toString('base64')); expect(auth).toBe('Basic ' + Buffer.from('user1:secret1').toString('base64'));
expect(await page.title()).toBe('Served by the proxy');
await context.close(); await context.close();
} }
auth = undefined;
{ {
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}`, username: 'user2', password: 'secret2' } proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user2', password: 'secret2' }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://non-existent.com/target.html'); await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Basic ' + Buffer.from('user2:secret2').toString('base64')); expect(await page.title()).toBe('Served by the proxy');
expect(auth).toBe('Basic ' + Buffer.from('user2:secret2').toString('base64'));
await context.close(); await context.close();
} }
}); });
it('should exclude patterns', async ({contextFactory, server, browserName, headless}) => { it('should exclude patterns', async ({contextFactory, server, browserName, headless, proxyServer}) => {
it.fixme(browserName === 'chromium' && !headless, 'Chromium headed crashes with CHECK(!in_frame_tree_) in RenderFrameImpl::OnDeleteFrame.'); it.fixme(browserName === 'chromium' && !headless, 'Chromium headed crashes with CHECK(!in_frame_tree_) in RenderFrameImpl::OnDeleteFrame.');
server.setRoute('/target.html', async (req, res) => { proxyServer.forwardTo(server.PORT);
res.end('<html><title>Served by the proxy</title></html>');
});
// FYI: using long and weird domain names to avoid ATT DNS hijacking // FYI: using long and weird domain names to avoid ATT DNS hijacking
// that resolves everything to some weird search results page. // that resolves everything to some weird search results page.
// //
// @see https://gist.github.com/CollinChaffin/24f6c9652efb3d6d5ef2f5502720ef00 // @see https://gist.github.com/CollinChaffin/24f6c9652efb3d6d5ef2f5502720ef00
const context = await contextFactory({ const context = await contextFactory({
proxy: { server: `localhost:${server.PORT}`, bypass: '1.non.existent.domain.for.the.test, 2.non.existent.domain.for.the.test, .another.test' } proxy: { server: `localhost:${proxyServer.PORT}`, bypass: '1.non.existent.domain.for.the.test, 2.non.existent.domain.for.the.test, .another.test' }
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto('http://0.non.existent.domain.for.the.test/target.html'); await page.goto('http://0.non.existent.domain.for.the.test/target.html');
expect(proxyServer.requestUrls).toContain('http://0.non.existent.domain.for.the.test/target.html');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by the proxy');
proxyServer.requestUrls = [];
{ {
const error = await page.goto('http://1.non.existent.domain.for.the.test/target.html').catch(e => e); const error = await page.goto('http://1.non.existent.domain.for.the.test/target.html').catch(e => e);
expect(proxyServer.requestUrls).toEqual([]);
expect(error.message).toBeTruthy(); expect(error.message).toBeTruthy();
} }
{ {
const error = await page.goto('http://2.non.existent.domain.for.the.test/target.html').catch(e => e); const error = await page.goto('http://2.non.existent.domain.for.the.test/target.html').catch(e => e);
expect(proxyServer.requestUrls).toEqual([]);
expect(error.message).toBeTruthy(); expect(error.message).toBeTruthy();
} }
{ {
const error = await page.goto('http://foo.is.the.another.test/target.html').catch(e => e); const error = await page.goto('http://foo.is.the.another.test/target.html').catch(e => e);
expect(proxyServer.requestUrls).toEqual([]);
expect(error.message).toBeTruthy(); expect(error.message).toBeTruthy();
} }
{ {
await page.goto('http://3.non.existent.domain.for.the.test/target.html'); await page.goto('http://3.non.existent.domain.for.the.test/target.html');
expect(proxyServer.requestUrls).toContain('http://3.non.existent.domain.for.the.test/target.html');
expect(await page.title()).toBe('Served by the proxy'); expect(await page.title()).toBe('Served by the proxy');
} }

View File

@ -21,22 +21,25 @@ import createProxy from 'proxy';
export class TestProxy { export class TestProxy {
readonly PORT: number; readonly PORT: number;
readonly URL: string; readonly URL: string;
readonly server: Server;
connectHosts: string[] = [];
requestUrls: string[] = [];
private readonly _server: Server;
private readonly _sockets = new Set<Socket>(); private readonly _sockets = new Set<Socket>();
private _connectHandlers = []; private _handlers: { event: string, handler: (...args: any[]) => void }[] = [];
static async create(port: number): Promise<TestProxy> { static async create(port: number): Promise<TestProxy> {
const proxy = new TestProxy(port); const proxy = new TestProxy(port);
await new Promise(f => proxy.server.listen(port, f)); await new Promise(f => proxy._server.listen(port, f));
return proxy; return proxy;
} }
private constructor(port: number) { private constructor(port: number) {
this.PORT = port; this.PORT = port;
this.URL = `http://localhost:${port}`; this.URL = `http://localhost:${port}`;
this.server = createProxy(); this._server = createProxy();
this.server.on('connection', socket => this._onSocket(socket)); this._server.on('connection', socket => this._onSocket(socket));
} }
async stop(): Promise<void> { async stop(): Promise<void> {
@ -44,18 +47,44 @@ export class TestProxy {
for (const socket of this._sockets) for (const socket of this._sockets)
socket.destroy(); socket.destroy();
this._sockets.clear(); this._sockets.clear();
await new Promise(x => this.server.close(x)); await new Promise(x => this._server.close(x));
} }
onConnect(handler: (req: IncomingMessage) => void) { forwardTo(port: number) {
this._connectHandlers.push(handler); this._prependHandler('request', (req: IncomingMessage) => {
this.server.prependListener('connect', handler); this.requestUrls.push(req.url);
const url = new URL(req.url);
url.host = `localhost:${port}`;
req.url = url.toString();
});
this._prependHandler('connect', (req: IncomingMessage) => {
this.connectHosts.push(req.url);
req.url = `localhost:${port}`;
});
}
setAuthHandler(handler: (req: IncomingMessage) => boolean) {
(this._server as any).authenticate = (req: IncomingMessage, callback) => {
try {
callback(null, handler(req));
} catch (e) {
callback(e, false);
}
};
} }
reset() { reset() {
for (const handler of this._connectHandlers) this.connectHosts = [];
this.server.removeListener('connect', handler); this.requestUrls = [];
this._connectHandlers = []; for (const { event, handler } of this._handlers)
this._server.removeListener(event, handler);
this._handlers = [];
(this._server as any).authenticate = undefined;
}
private _prependHandler(event: string, handler: (...args: any[]) => void) {
this._handlers.push({ event, handler });
this._server.prependListener(event, handler);
} }
private _onSocket(socket: Socket) { private _onSocket(socket: Socket) {