2021-08-20 01:16:46 +03:00
|
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import childProcess from 'child_process';
|
|
|
|
|
import http from 'http';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import net from 'net';
|
|
|
|
|
|
|
|
|
|
import { contextTest, expect } from './config/browserTest';
|
2021-10-11 17:52:17 +03:00
|
|
|
|
import { PlaywrightClient } from 'playwright-core/lib/remote/playwrightClient';
|
|
|
|
|
import type { Page } from 'playwright-core';
|
2021-08-20 01:16:46 +03:00
|
|
|
|
|
|
|
|
|
class OutOfProcessPlaywrightServer {
|
|
|
|
|
private _driverProcess: childProcess.ChildProcess;
|
|
|
|
|
private _receivedPortPromise: Promise<string>;
|
|
|
|
|
|
|
|
|
|
constructor(port: number, proxyPort: number) {
|
2021-10-11 17:52:17 +03:00
|
|
|
|
this._driverProcess = childProcess.fork(path.join(__dirname, '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), ['run-server', port.toString()], {
|
2021-08-20 01:16:46 +03:00
|
|
|
|
stdio: 'pipe',
|
|
|
|
|
detached: true,
|
|
|
|
|
env: {
|
|
|
|
|
...process.env,
|
2021-08-20 04:08:55 +03:00
|
|
|
|
PW_SOCKS_PROXY_PORT: '1'
|
2021-08-20 01:16:46 +03:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this._driverProcess.unref();
|
|
|
|
|
this._receivedPortPromise = new Promise<string>((resolve, reject) => {
|
|
|
|
|
this._driverProcess.stdout.on('data', (data: Buffer) => {
|
|
|
|
|
const prefix = 'Listening on ';
|
|
|
|
|
const line = data.toString();
|
|
|
|
|
if (line.startsWith(prefix))
|
|
|
|
|
resolve(line.substr(prefix.length));
|
|
|
|
|
});
|
2021-08-20 04:08:55 +03:00
|
|
|
|
this._driverProcess.stderr.on('data', (data: Buffer) => {
|
|
|
|
|
console.log(data.toString());
|
|
|
|
|
});
|
2021-08-20 01:16:46 +03:00
|
|
|
|
this._driverProcess.on('exit', () => reject());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
async kill() {
|
|
|
|
|
const waitForExit = new Promise<void>(resolve => this._driverProcess.on('exit', () => resolve()));
|
|
|
|
|
this._driverProcess.kill('SIGKILL');
|
|
|
|
|
await waitForExit;
|
|
|
|
|
}
|
|
|
|
|
public async wsEndpoint(): Promise<string> {
|
|
|
|
|
return await this._receivedPortPromise;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const it = contextTest.extend<{ pageFactory: (redirectPortForTest?: number) => Promise<Page> }>({
|
|
|
|
|
pageFactory: async ({ browserName, browserOptions }, run, testInfo) => {
|
|
|
|
|
const playwrightServers: OutOfProcessPlaywrightServer[] = [];
|
|
|
|
|
await run(async (redirectPortForTest?: number): Promise<Page> => {
|
|
|
|
|
const server = new OutOfProcessPlaywrightServer(0, 3200 + testInfo.workerIndex);
|
|
|
|
|
playwrightServers.push(server);
|
|
|
|
|
const service = await PlaywrightClient.connect({
|
|
|
|
|
wsEndpoint: await server.wsEndpoint(),
|
|
|
|
|
});
|
|
|
|
|
const playwright = service.playwright();
|
|
|
|
|
playwright._enablePortForwarding(redirectPortForTest);
|
|
|
|
|
const browser = await playwright[browserName].launch(browserOptions);
|
|
|
|
|
return await browser.newPage();
|
|
|
|
|
});
|
|
|
|
|
for (const playwrightServer of playwrightServers)
|
|
|
|
|
await playwrightServer.kill();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-20 02:56:06 +03:00
|
|
|
|
it.fixme(({ platform, browserName }) => browserName === 'webkit' && platform === 'win32');
|
2021-08-20 01:16:46 +03:00
|
|
|
|
it.skip(({ mode }) => mode !== 'default');
|
|
|
|
|
|
|
|
|
|
async function startTestServer() {
|
|
|
|
|
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
|
|
|
|
|
res.end('<html><body>from-retargeted-server</body></html>');
|
|
|
|
|
});
|
2021-10-02 05:40:47 +03:00
|
|
|
|
await new Promise<void>(resolve => server.listen(0, resolve));
|
2021-08-20 01:16:46 +03:00
|
|
|
|
return {
|
|
|
|
|
testServerPort: (server.address() as net.AddressInfo).port,
|
|
|
|
|
stopTestServer: () => server.close()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it('should forward non-forwarded requests', async ({ pageFactory, server }) => {
|
|
|
|
|
let reachedOriginalTarget = false;
|
|
|
|
|
server.setRoute('/foo.html', async (req, res) => {
|
|
|
|
|
reachedOriginalTarget = true;
|
|
|
|
|
res.end('<html><body>original-target</body></html>');
|
|
|
|
|
});
|
|
|
|
|
const page = await pageFactory();
|
|
|
|
|
await page.goto(server.PREFIX + '/foo.html');
|
|
|
|
|
expect(await page.content()).toContain('original-target');
|
|
|
|
|
expect(reachedOriginalTarget).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-20 02:56:06 +03:00
|
|
|
|
it('should proxy localhost requests', async ({ pageFactory, server, browserName, platform }, workerInfo) => {
|
|
|
|
|
it.skip(browserName === 'webkit' && platform === 'darwin');
|
2021-08-20 01:16:46 +03:00
|
|
|
|
const { testServerPort, stopTestServer } = await startTestServer();
|
|
|
|
|
let reachedOriginalTarget = false;
|
|
|
|
|
server.setRoute('/foo.html', async (req, res) => {
|
|
|
|
|
reachedOriginalTarget = true;
|
|
|
|
|
res.end('<html><body></body></html>');
|
|
|
|
|
});
|
|
|
|
|
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
|
|
|
|
const page = await pageFactory(testServerPort);
|
|
|
|
|
await page.goto(`http://localhost:${examplePort}/foo.html`);
|
|
|
|
|
expect(await page.content()).toContain('from-retargeted-server');
|
|
|
|
|
expect(reachedOriginalTarget).toBe(false);
|
|
|
|
|
stopTestServer();
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-20 02:56:06 +03:00
|
|
|
|
it('should proxy local.playwright requests', async ({ pageFactory, server, browserName }, workerInfo) => {
|
|
|
|
|
const { testServerPort, stopTestServer } = await startTestServer();
|
|
|
|
|
let reachedOriginalTarget = false;
|
|
|
|
|
server.setRoute('/foo.html', async (req, res) => {
|
|
|
|
|
reachedOriginalTarget = true;
|
|
|
|
|
res.end('<html><body></body></html>');
|
|
|
|
|
});
|
|
|
|
|
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
|
|
|
|
const page = await pageFactory(testServerPort);
|
|
|
|
|
await page.goto(`http://local.playwright:${examplePort}/foo.html`);
|
|
|
|
|
expect(await page.content()).toContain('from-retargeted-server');
|
|
|
|
|
expect(reachedOriginalTarget).toBe(false);
|
|
|
|
|
stopTestServer();
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-20 01:16:46 +03:00
|
|
|
|
it('should lead to the error page for forwarded requests when the connection is refused', async ({ pageFactory, browserName }, workerInfo) => {
|
|
|
|
|
const examplePort = 20_000 + workerInfo.workerIndex * 3;
|
|
|
|
|
const page = await pageFactory();
|
|
|
|
|
const error = await page.goto(`http://localhost:${examplePort}`).catch(e => e);
|
|
|
|
|
if (browserName === 'chromium')
|
|
|
|
|
expect(error.message).toContain('net::ERR_SOCKS_CONNECTION_FAILED at http://localhost:20');
|
|
|
|
|
else if (browserName === 'webkit')
|
|
|
|
|
expect(error.message).toBeTruthy();
|
|
|
|
|
else if (browserName === 'firefox')
|
2021-09-03 13:53:21 +03:00
|
|
|
|
expect(error.message.includes('NS_ERROR_NET_RESET') || error.message.includes('NS_ERROR_CONNECTION_REFUSED')).toBe(true);
|
2021-08-20 01:16:46 +03:00
|
|
|
|
});
|