| 
									
										
										
										
											2021-08-19 15:16:46 -07: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'; | 
					
						
							| 
									
										
										
										
											2022-04-06 13:57:14 -08:00
										 |  |  |  | import type net from 'net'; | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  |  | import { contextTest, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2022-02-10 16:36:23 -08:00
										 |  |  |  | import type { Page, Browser } from 'playwright-core'; | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | class OutOfProcessPlaywrightServer { | 
					
						
							|  |  |  |  |   private _driverProcess: childProcess.ChildProcess; | 
					
						
							|  |  |  |  |   private _receivedPortPromise: Promise<string>; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   constructor(port: number, proxyPort: number) { | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  |  |     this._driverProcess = childProcess.fork(path.join(__dirname, '..', '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), ['run-server', '--port', port.toString()], { | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |       stdio: 'pipe', | 
					
						
							|  |  |  |  |       detached: true, | 
					
						
							|  |  |  |  |       env: { | 
					
						
							| 
									
										
										
										
											2022-04-15 12:11:38 -07:00
										 |  |  |  |         ...process.env | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07: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-19 18:08:55 -07:00
										 |  |  |  |       this._driverProcess.stderr.on('data', (data: Buffer) => { | 
					
						
							|  |  |  |  |         console.log(data.toString()); | 
					
						
							|  |  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07: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> }>({ | 
					
						
							| 
									
										
										
										
											2022-02-10 16:36:23 -08:00
										 |  |  |  |   pageFactory: async ({ browserType, browserName, channel }, run, testInfo) => { | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |     const playwrightServers: OutOfProcessPlaywrightServer[] = []; | 
					
						
							| 
									
										
										
										
											2022-02-10 16:36:23 -08:00
										 |  |  |  |     const browsers: Browser[] = []; | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |     await run(async (redirectPortForTest?: number): Promise<Page> => { | 
					
						
							|  |  |  |  |       const server = new OutOfProcessPlaywrightServer(0, 3200 + testInfo.workerIndex); | 
					
						
							|  |  |  |  |       playwrightServers.push(server); | 
					
						
							| 
									
										
										
										
											2022-02-10 16:36:23 -08:00
										 |  |  |  |       const browser = await browserType.connect({ | 
					
						
							| 
									
										
										
										
											2022-08-04 15:04:00 -07:00
										 |  |  |  |         wsEndpoint: await server.wsEndpoint() + '?proxy=*&browser=' + browserName, | 
					
						
							|  |  |  |  |         headers: { 'x-playwright-launch-options': JSON.stringify({ channel }) }, | 
					
						
							| 
									
										
										
										
											2022-02-14 15:10:58 -08:00
										 |  |  |  |         __testHookRedirectPortForwarding: redirectPortForTest, | 
					
						
							| 
									
										
										
										
											2022-02-10 16:36:23 -08:00
										 |  |  |  |       } as any); | 
					
						
							|  |  |  |  |       browsers.push(browser); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |       return await browser.newPage(); | 
					
						
							|  |  |  |  |     }); | 
					
						
							|  |  |  |  |     for (const playwrightServer of playwrightServers) | 
					
						
							|  |  |  |  |       await playwrightServer.kill(); | 
					
						
							| 
									
										
										
										
											2022-02-10 16:36:23 -08:00
										 |  |  |  |     await Promise.all(browsers.map(async browser => { | 
					
						
							|  |  |  |  |       if (browser.isConnected()) | 
					
						
							|  |  |  |  |         await new Promise(f => browser.once('disconnected', f)); | 
					
						
							|  |  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |   }, | 
					
						
							|  |  |  |  | }); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 16:56:06 -07:00
										 |  |  |  | it.fixme(({ platform, browserName }) => browserName === 'webkit' && platform === 'win32'); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07: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-01 19:40:47 -07:00
										 |  |  |  |   await new Promise<void>(resolve => server.listen(0, resolve)); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07: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); | 
					
						
							|  |  |  |  | }); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  |  | it('should proxy localhost requests @smoke', async ({ pageFactory, server, browserName, platform }, workerInfo) => { | 
					
						
							| 
									
										
										
										
											2021-08-19 16:56:06 -07:00
										 |  |  |  |   it.skip(browserName === 'webkit' && platform === 'darwin'); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07: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); | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |  |   await page.goto(`http://127.0.0.1:${examplePort}/foo.html`); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |   expect(await page.content()).toContain('from-retargeted-server'); | 
					
						
							|  |  |  |  |   expect(reachedOriginalTarget).toBe(false); | 
					
						
							|  |  |  |  |   stopTestServer(); | 
					
						
							|  |  |  |  | }); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 12:05:35 -08:00
										 |  |  |  | it('should proxy localhost requests from fetch api', async ({ pageFactory, server, browserName, platform }, workerInfo) => { | 
					
						
							|  |  |  |  |   it.skip(browserName === 'webkit' && platform === 'darwin'); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   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); | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |  |   const response = await page.request.get(`http://127.0.0.1:${examplePort}/foo.html`); | 
					
						
							| 
									
										
										
										
											2022-02-10 12:05:35 -08:00
										 |  |  |  |   expect(response.status()).toBe(200); | 
					
						
							|  |  |  |  |   expect(await response.text()).toContain('from-retargeted-server'); | 
					
						
							|  |  |  |  |   expect(reachedOriginalTarget).toBe(false); | 
					
						
							|  |  |  |  |   stopTestServer(); | 
					
						
							|  |  |  |  | }); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 16:56:06 -07: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-19 15:16:46 -07: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(); | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |  |   const error = await page.goto(`http://127.0.0.1:${examplePort}`).catch(e => e); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |   if (browserName === 'chromium') | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |  |     expect(error.message).toContain('net::ERR_SOCKS_CONNECTION_FAILED at http://127.0.0.1:20'); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  |   else if (browserName === 'webkit') | 
					
						
							|  |  |  |  |     expect(error.message).toBeTruthy(); | 
					
						
							|  |  |  |  |   else if (browserName === 'firefox') | 
					
						
							| 
									
										
										
										
											2021-09-03 12:53:21 +02:00
										 |  |  |  |     expect(error.message.includes('NS_ERROR_NET_RESET') || error.message.includes('NS_ERROR_CONNECTION_REFUSED')).toBe(true); | 
					
						
							| 
									
										
										
										
											2021-08-19 15:16:46 -07:00
										 |  |  |  | }); |