diff --git a/test/proxy.spec.ts b/test/proxy.spec.ts index 59fb030bff..35a74092a6 100644 --- a/test/proxy.spec.ts +++ b/test/proxy.spec.ts @@ -14,10 +14,40 @@ * limitations under the License. */ -import { it, expect } from './fixtures'; +import { folio as baseFolio } from './fixtures'; import socks from 'socksv5'; +const builder = baseFolio.extend<{}, { + socksPort: number, +}>(); + +builder.socksPort.init(async ({ testWorkerIndex }, run) => { + const server = socks.createServer((info, accept, deny) => { + let socket; + if ((socket = accept(true))) { + // Catch and ignore ECONNRESET errors. + socket.on('error', () => {}); + const body = 'Served by the SOCKS proxy'; + socket.end([ + 'HTTP/1.1 200 OK', + 'Connection: close', + 'Content-Type: text/html', + 'Content-Length: ' + Buffer.byteLength(body), + '', + body + ].join('\r\n')); + } + }); + const socksPort = 9107 + testWorkerIndex * 2; + server.listen(socksPort, 'localhost'); + server.useAuth(socks.auth.None()); + await run(socksPort); + server.close(); +}, { scope: 'worker' }); + +const { it, expect } = builder.build(); + it('should throw for bad server value', async ({browserType, browserOptions}) => { const error = await browserType.launch({ ...browserOptions, @@ -41,6 +71,26 @@ it('should use proxy', async ({browserType, browserOptions, server}) => { await browser.close(); }); +it('should use proxy for second page', async ({browserType, browserOptions, server}) => { + server.setRoute('/target.html', async (req, res) => { + res.end('Served by the proxy'); + }); + const browser = await browserType.launch({ + ...browserOptions, + proxy: { server: `localhost:${server.PORT}` } + }); + + const page = await browser.newPage(); + await page.goto('http://non-existent.com/target.html'); + expect(await page.title()).toBe('Served by the proxy'); + + const page2 = await browser.newPage(); + await page2.goto('http://non-existent.com/target.html'); + expect(await page2.title()).toBe('Served by the proxy'); + + await browser.close(); +}); + it('should work with IP:PORT notion', async ({browserType, browserOptions, server}) => { server.setRoute('/target.html', async (req, res) => { res.end('Served by the proxy'); @@ -121,27 +171,7 @@ it('should exclude patterns', (test, { browserName, headful }) => { it('should use socks proxy', (test, { browserName, platform }) => { test.flaky(platform === 'darwin' && browserName === 'webkit', 'Intermittent page.goto: The network connection was lost error on bots'); -}, async ({ browserType, browserOptions, testWorkerIndex }) => { - const server = socks.createServer((info, accept, deny) => { - let socket; - if ((socket = accept(true))) { - // Catch and ignore ECONNRESET errors. - socket.on('error', () => {}); - const body = 'Served by the SOCKS proxy'; - socket.end([ - 'HTTP/1.1 200 OK', - 'Connection: close', - 'Content-Type: text/html', - 'Content-Length: ' + Buffer.byteLength(body), - '', - body - ].join('\r\n')); - } - }); - const socksPort = 9107 + testWorkerIndex * 2; - server.listen(socksPort, 'localhost'); - server.useAuth(socks.auth.None()); - +}, async ({ browserType, browserOptions, socksPort }) => { const browser = await browserType.launch({ ...browserOptions, proxy: { server: `socks5://localhost:${socksPort}` } @@ -150,7 +180,25 @@ it('should use socks proxy', (test, { browserName, platform }) => { await page.goto('http://non-existent.com'); expect(await page.title()).toBe('Served by the SOCKS proxy'); await browser.close(); - server.close(); +}); + +it('should use socks proxy in second page', (test, { browserName, platform }) => { + test.flaky(platform === 'darwin' && browserName === 'webkit', 'Intermittent page.goto: The network connection was lost error on bots'); +}, async ({ browserType, browserOptions, socksPort }) => { + const browser = await browserType.launch({ + ...browserOptions, + proxy: { server: `socks5://localhost:${socksPort}` } + }); + + const page = await browser.newPage(); + await page.goto('http://non-existent.com'); + expect(await page.title()).toBe('Served by the SOCKS proxy'); + + const page2 = await browser.newPage(); + await page2.goto('http://non-existent.com'); + expect(await page2.title()).toBe('Served by the SOCKS proxy'); + + await browser.close(); }); it('does launch without a port', async ({ browserType, browserOptions }) => {