test(proxy): add a test for a second page against same proxy (#4185)

This commit is contained in:
Pavel Feldman 2020-10-19 18:35:37 -07:00 committed by GitHub
parent 9c160f2cc8
commit efac743695
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,
@ -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>');
@ -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 = '<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());
}, 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 }) => {