Skip to content

Commit

Permalink
test(proxy): add a test for a second page against same proxy (#4185)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Oct 20, 2020
1 parent 9c160f2 commit efac743
Showing 1 changed file with 69 additions and 21 deletions.
90 changes: 69 additions & 21 deletions test/proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<html><title>Served by the SOCKS proxy</title></html>';
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,
Expand All @@ -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('<html><title>Served by the proxy</title></html>');
});
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('<html><title>Served by the proxy</title></html>');
Expand Down Expand Up @@ -121,36 +171,34 @@ 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 = '<html><title>Served by the SOCKS proxy</title></html>';
socket.end([
'HTTP/1.1 200 OK',
'Connection: close',
'Content-Type: text/html',
'Content-Length: ' + Buffer.byteLength(body),
'',
body
].join('\r\n'));
}
}, async ({ browserType, browserOptions, socksPort }) => {
const browser = await browserType.launch({
...browserOptions,
proxy: { server: `socks5://localhost:${socksPort}` }
});
const socksPort = 9107 + testWorkerIndex * 2;
server.listen(socksPort, 'localhost');
server.useAuth(socks.auth.None());
const page = await browser.newPage();
await page.goto('http://non-existent.com');
expect(await page.title()).toBe('Served by the SOCKS proxy');
await browser.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();
server.close();
});

it('does launch without a port', async ({ browserType, browserOptions }) => {
Expand Down

0 comments on commit efac743

Please sign in to comment.