| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications 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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.route('**/*', route => route.continue()); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should amend HTTP headers', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     const headers = Object.assign({}, route.request().headers()); | 
					
						
							|  |  |  |     headers['FOO'] = 'bar'; | 
					
						
							|  |  |  |     route.continue({ headers }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							|  |  |  |     server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |     page.evaluate(() => fetch('/sleep.zzz')) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.headers['foo']).toBe('bar'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should amend method', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const sRequest = server.waitForRequest('/sleep.zzz'); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue({ method: 'POST' })); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							|  |  |  |     server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |     page.evaluate(() => fetch('/sleep.zzz')) | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.method).toBe('POST'); | 
					
						
							|  |  |  |   expect((await sRequest).method).toBe('POST'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should override request url', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |   const request = server.waitForRequest('/global-var.html'); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   await page.route('**/foo', route => { | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |     route.continue({ url: server.PREFIX + '/global-var.html' }); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |   const [response] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('response'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/foo'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(response.url()).toBe(server.PREFIX + '/foo'); | 
					
						
							|  |  |  |   expect(await page.evaluate(() => window['globalVar'])).toBe(123); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   expect((await request).method).toBe('GET'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should not allow changing protocol when overriding url', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  |   let resolve; | 
					
						
							|  |  |  |   const errorPromise = new Promise<Error|null>(f => resolve = f); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await route.continue({ url: 'file:///tmp/foo' }); | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  |       resolve(null); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |     } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  |       resolve(e); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  |   page.goto(server.EMPTY_PAGE).catch(() => {}); | 
					
						
							|  |  |  |   const error = await errorPromise; | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   expect(error).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2021-02-24 15:11:34 -08:00
										 |  |  |   expect(error.message).toContain('New URL must have same protocol as overridden URL'); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 15:13:15 -08:00
										 |  |  | it('should not throw when continuing while page is closing', async ({ page, server }) => { | 
					
						
							|  |  |  |   let done; | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     done = Promise.all([ | 
					
						
							|  |  |  |       route.continue(), | 
					
						
							|  |  |  |       page.close(), | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const error = await page.goto(server.EMPTY_PAGE).catch(e => e); | 
					
						
							|  |  |  |   await done; | 
					
						
							|  |  |  |   expect(error).toBeInstanceOf(Error); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should not throw when continuing after page is closed', async ({ page, server }) => { | 
					
						
							|  |  |  |   let done; | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     await page.close(); | 
					
						
							|  |  |  |     done = route.continue(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const error = await page.goto(server.EMPTY_PAGE).catch(e => e); | 
					
						
							|  |  |  |   await done; | 
					
						
							|  |  |  |   expect(error).toBeInstanceOf(Error); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should override method along with url', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   const request = server.waitForRequest('/empty.html'); | 
					
						
							|  |  |  |   await page.route('**/foo', route => { | 
					
						
							|  |  |  |     route.continue({ | 
					
						
							|  |  |  |       url: server.EMPTY_PAGE, | 
					
						
							|  |  |  |       method: 'POST' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/foo'); | 
					
						
							|  |  |  |   expect((await request).method).toBe('POST'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should amend method on main request', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const request = server.waitForRequest('/empty.html'); | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue({ method: 'POST' })); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect((await request).method).toBe('POST'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 09:53:42 -08:00
										 |  |  | it.describe('post data', () => { | 
					
						
							| 
									
										
										
										
											2021-04-30 13:26:13 -07:00
										 |  |  |   it.fixme(({ isAndroid }) => isAndroid, 'Post data does not work'); | 
					
						
							| 
									
										
										
										
											2021-04-04 19:32:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should amend post data', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.route('**/*', route => { | 
					
						
							|  |  |  |       route.continue({ postData: 'doggo' }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |       server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |       page.evaluate(() => fetch('/sleep.zzz', { method: 'POST', body: 'birdy' })) | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect((await serverRequest.postBody).toString('utf8')).toBe('doggo'); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should amend method and post data', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.route('**/*', route => { | 
					
						
							|  |  |  |       route.continue({ method: 'POST', postData: 'doggo' }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |       server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |       page.evaluate(() => fetch('/sleep.zzz', { method: 'GET' })) | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(serverRequest.method).toBe('POST'); | 
					
						
							|  |  |  |     expect((await serverRequest.postBody).toString('utf8')).toBe('doggo'); | 
					
						
							| 
									
										
										
										
											2020-10-18 23:03:07 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should amend utf8 post data', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.route('**/*', route => { | 
					
						
							|  |  |  |       route.continue({ postData: 'пушкин' }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |       server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |       page.evaluate(() => fetch('/sleep.zzz', { method: 'POST', body: 'birdy' })) | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(serverRequest.method).toBe('POST'); | 
					
						
							|  |  |  |     expect((await serverRequest.postBody).toString('utf8')).toBe('пушкин'); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should amend longer post data', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.route('**/*', route => { | 
					
						
							|  |  |  |       route.continue({ postData: 'doggo-is-longer-than-birdy' }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |       server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |       page.evaluate(() => fetch('/sleep.zzz', { method: 'POST', body: 'birdy' })) | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(serverRequest.method).toBe('POST'); | 
					
						
							|  |  |  |     expect((await serverRequest.postBody).toString('utf8')).toBe('doggo-is-longer-than-birdy'); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should amend binary post data', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     const arr = Array.from(Array(256).keys()); | 
					
						
							|  |  |  |     await page.route('**/*', route => { | 
					
						
							|  |  |  |       route.continue({ postData: Buffer.from(arr) }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |       server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |       page.evaluate(() => fetch('/sleep.zzz', { method: 'POST', body: 'birdy' })) | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(serverRequest.method).toBe('POST'); | 
					
						
							|  |  |  |     const buffer = await serverRequest.postBody; | 
					
						
							|  |  |  |     expect(buffer.length).toBe(arr.length); | 
					
						
							|  |  |  |     for (let i = 0; i < arr.length; ++i) | 
					
						
							|  |  |  |       expect(arr[i]).toBe(buffer[i]); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-09-28 09:56:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should work with Cross-Origin-Opener-Policy', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.fail(browserName === 'webkit', 'https://github.com/microsoft/playwright/issues/8796'); | 
					
						
							|  |  |  |   let serverHeaders; | 
					
						
							|  |  |  |   const serverRequests = []; | 
					
						
							|  |  |  |   server.setRoute('/empty.html', (req, res) => { | 
					
						
							|  |  |  |     serverRequests.push(req.url); | 
					
						
							|  |  |  |     serverHeaders ??= req.headers; | 
					
						
							|  |  |  |     res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); | 
					
						
							|  |  |  |     res.end(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const intercepted = []; | 
					
						
							|  |  |  |   await page.route('**/*', (route, req) => { | 
					
						
							|  |  |  |     intercepted.push(req.url()); | 
					
						
							|  |  |  |     route.continue({ | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         foo: 'bar' | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const requests = new Set(); | 
					
						
							|  |  |  |   const events = []; | 
					
						
							|  |  |  |   page.on('request', r => { | 
					
						
							|  |  |  |     events.push('request'); | 
					
						
							|  |  |  |     requests.add(r); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   page.on('requestfailed', r => { | 
					
						
							|  |  |  |     events.push('requestfailed'); | 
					
						
							|  |  |  |     requests.add(r); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   page.on('requestfinished', r => { | 
					
						
							|  |  |  |     events.push('requestfinished'); | 
					
						
							|  |  |  |     requests.add(r); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   page.on('response', r => { | 
					
						
							|  |  |  |     events.push('response'); | 
					
						
							|  |  |  |     requests.add(r.request()); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(intercepted).toEqual([server.EMPTY_PAGE]); | 
					
						
							|  |  |  |   // There should be only one request to the server.
 | 
					
						
							|  |  |  |   if (browserName === 'webkit') | 
					
						
							|  |  |  |     expect(serverRequests).toEqual(['/empty.html', '/empty.html']); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(serverRequests).toEqual(['/empty.html']); | 
					
						
							|  |  |  |   expect(serverHeaders['foo']).toBe('bar'); | 
					
						
							|  |  |  |   expect(page.url()).toBe(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await response.finished(); | 
					
						
							|  |  |  |   expect(events).toEqual(['request', 'response', 'requestfinished']); | 
					
						
							|  |  |  |   expect(requests.size).toBe(1); | 
					
						
							|  |  |  |   expect(response.request().failure()).toBeNull(); | 
					
						
							|  |  |  | }); |