| 
									
										
										
										
											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'; | 
					
						
							| 
									
										
										
										
											2023-12-07 16:57:39 -08:00
										 |  |  | import type { Route } from 'playwright-core'; | 
					
						
							| 
									
										
										
										
											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'; | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ headers }); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   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'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 12:38:57 -07:00
										 |  |  | it('should delete header with undefined value', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/13106' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   server.setRoute('/something', (request, response) => { | 
					
						
							|  |  |  |     response.writeHead(200, { 'Access-Control-Allow-Origin': '*' }); | 
					
						
							|  |  |  |     response.end('done'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   let interceptedRequest; | 
					
						
							|  |  |  |   await page.route(server.PREFIX + '/something', async (route, request) => { | 
					
						
							|  |  |  |     interceptedRequest = request; | 
					
						
							|  |  |  |     const headers = await request.allHeaders(); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ | 
					
						
							| 
									
										
										
										
											2022-05-02 12:38:57 -07:00
										 |  |  |       headers: { | 
					
						
							|  |  |  |         ...headers, | 
					
						
							|  |  |  |         foo: undefined | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const [text, serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |     page.evaluate(async url => { | 
					
						
							|  |  |  |       const data = await fetch(url, { | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |           foo: 'a', | 
					
						
							|  |  |  |           bar: 'b', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return data.text(); | 
					
						
							|  |  |  |     }, server.PREFIX + '/something'), | 
					
						
							|  |  |  |     server.waitForRequest('/something') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(text).toBe('done'); | 
					
						
							| 
									
										
										
										
											2022-06-13 16:56:16 -08:00
										 |  |  |   expect(interceptedRequest.headers()['foo']).toEqual(undefined); | 
					
						
							| 
									
										
										
										
											2022-05-02 12:38:57 -07:00
										 |  |  |   expect(serverRequest.headers.foo).toBeFalsy(); | 
					
						
							|  |  |  |   expect(serverRequest.headers.bar).toBe('b'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 }) => { | 
					
						
							| 
									
										
										
										
											2022-09-04 10:52:20 -07:00
										 |  |  |   const serverRequest = server.waitForRequest('/global-var.html'); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   await page.route('**/foo', route => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ url: server.PREFIX + '/global-var.html' }); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-09-04 10:52:20 -07:00
										 |  |  |   const response = await page.goto(server.PREFIX + '/foo'); | 
					
						
							|  |  |  |   expect(response.request().url()).toBe(server.PREFIX + '/global-var.html'); | 
					
						
							|  |  |  |   expect(response.url()).toBe(server.PREFIX + '/global-var.html'); | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |   expect(await page.evaluate(() => window['globalVar'])).toBe(123); | 
					
						
							| 
									
										
										
										
											2022-09-04 10:52:20 -07:00
										 |  |  |   expect((await serverRequest).method).toBe('GET'); | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-05 10:26:17 -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
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 20:09:22 +02:00
										 |  |  | it('should not throw when continuing while page is closing', async ({ page, server, isWebView2 }) => { | 
					
						
							|  |  |  |   it.skip(isWebView2, 'Page.close() is not supported in WebView2'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 15:13:15 -08:00
										 |  |  |   let done; | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     done = Promise.all([ | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void route.continue(), | 
					
						
							| 
									
										
										
										
											2021-11-08 15:13:15 -08:00
										 |  |  |       page.close(), | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-06-30 13:08:18 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE).catch(e => e); | 
					
						
							| 
									
										
										
										
											2021-11-08 15:13:15 -08:00
										 |  |  |   await done; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 20:09:22 +02:00
										 |  |  | it('should not throw when continuing after page is closed', async ({ page, server, isWebView2 }) => { | 
					
						
							|  |  |  |   it.skip(isWebView2, 'Page.close() is not supported in WebView2'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 15:13:15 -08:00
										 |  |  |   let done; | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     await page.close(); | 
					
						
							|  |  |  |     done = route.continue(); | 
					
						
							| 
									
										
										
										
											2023-12-15 09:00:12 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-11-08 15:13:15 -08:00
										 |  |  |   const error = await page.goto(server.EMPTY_PAGE).catch(e => e); | 
					
						
							|  |  |  |   await done; | 
					
						
							|  |  |  |   expect(error).toBeInstanceOf(Error); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 16:57:39 -08:00
										 |  |  | it('should not throw if request was cancelled by the page', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/28490' }); | 
					
						
							|  |  |  |   let interceptCallback; | 
					
						
							|  |  |  |   const interceptPromise = new Promise<Route>(f => interceptCallback = f); | 
					
						
							|  |  |  |   await page.route('**/data.json', route => interceptCallback(route)); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   page.evaluate(url => { | 
					
						
							|  |  |  |     globalThis.controller = new AbortController(); | 
					
						
							|  |  |  |     return fetch(url, { signal: globalThis.controller.signal }); | 
					
						
							|  |  |  |   }, server.PREFIX + '/data.json').catch(() => {}); | 
					
						
							|  |  |  |   const route = await interceptPromise; | 
					
						
							|  |  |  |   const failurePromise = page.waitForEvent('requestfailed'); | 
					
						
							|  |  |  |   await page.evaluate(() => globalThis.controller.abort()); | 
					
						
							|  |  |  |   const cancelledRequest = await failurePromise; | 
					
						
							|  |  |  |   expect(cancelledRequest.failure().errorText).toMatch(/cancelled|aborted/i); | 
					
						
							|  |  |  |   await route.continue(); // Should not throw.
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ | 
					
						
							| 
									
										
										
										
											2020-11-16 09:59:00 -08:00
										 |  |  |       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-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 => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void route.continue({ postData: 'doggo' }); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     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
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-02 13:55:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-03 17:43:00 -07:00
										 |  |  |   it('should compute content-length from post data', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2022-08-02 13:55:52 -07:00
										 |  |  |     it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16027' }); | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     const data = 'a'.repeat(7500); | 
					
						
							|  |  |  |     await page.route('**/*', route => { | 
					
						
							|  |  |  |       const headers = route.request().headers(); | 
					
						
							|  |  |  |       headers['content-type'] =  'application/json'; | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void route.continue({ postData: data, headers }); | 
					
						
							| 
									
										
										
										
											2022-08-02 13:55:52 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |     const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |       server.waitForRequest('/sleep.zzz'), | 
					
						
							|  |  |  |       page.evaluate(() => fetch('/sleep.zzz', { method: 'PATCH', body: 'birdy' })) | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect((await serverRequest.postBody).toString('utf8')).toBe(data); | 
					
						
							|  |  |  |     expect(serverRequest.headers['content-length']).toBe(String(data.length)); | 
					
						
							|  |  |  |     expect(serverRequest.headers['content-type']).toBe('application/json'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											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 => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void route.continue({ method: 'POST', postData: 'doggo' }); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     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 => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void route.continue({ postData: 'пушкин' }); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     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 => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void route.continue({ postData: 'doggo-is-longer-than-birdy' }); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     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 => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |       void route.continue({ postData: Buffer.from(arr) }); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:29:20 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     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
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-23 08:28:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should use content-type from original request', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |     it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16736' }); | 
					
						
							| 
									
										
										
										
											2023-01-27 21:52:34 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 08:28:35 -07:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.route(`${server.PREFIX}/title.html`, route => route.continue({ postData: '{"b":2}' })); | 
					
						
							|  |  |  |     const [request] = await Promise.all([ | 
					
						
							|  |  |  |       server.waitForRequest('/title.html'), | 
					
						
							|  |  |  |       page.evaluate(async url => { | 
					
						
							|  |  |  |         await fetch(url, { | 
					
						
							|  |  |  |           method: 'POST', | 
					
						
							|  |  |  |           body: '{"a":1}', | 
					
						
							|  |  |  |           headers: { 'content-type': 'application/json' }, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }, `${server.PREFIX}/title.html`) | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |     expect(request.headers['content-type']).toBe('application/json'); | 
					
						
							|  |  |  |     expect((await request.postBody).toString('utf-8')).toBe('{"b":2}'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ | 
					
						
							| 
									
										
										
										
											2021-09-28 09:56:07 -07:00
										 |  |  |       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(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-05-02 12:38:33 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should delete the origin header', async ({ page, server, isAndroid, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/13106' }); | 
					
						
							|  |  |  |   it.skip(isAndroid, 'No cross-process on Android'); | 
					
						
							|  |  |  |   it.fail(browserName === 'webkit', 'Does not delete origin in webkit'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   server.setRoute('/something', (request, response) => { | 
					
						
							|  |  |  |     response.writeHead(200, { 'Access-Control-Allow-Origin': '*' }); | 
					
						
							|  |  |  |     response.end('done'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   let interceptedRequest; | 
					
						
							|  |  |  |   await page.route(server.CROSS_PROCESS_PREFIX + '/something', async (route, request) => { | 
					
						
							|  |  |  |     interceptedRequest = request; | 
					
						
							|  |  |  |     const headers = await request.allHeaders(); | 
					
						
							|  |  |  |     delete headers['origin']; | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ headers }); | 
					
						
							| 
									
										
										
										
											2022-05-02 12:38:33 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const [text, serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |     page.evaluate(async url => { | 
					
						
							|  |  |  |       const data = await fetch(url); | 
					
						
							|  |  |  |       return data.text(); | 
					
						
							|  |  |  |     }, server.CROSS_PROCESS_PREFIX + '/something'), | 
					
						
							|  |  |  |     server.waitForRequest('/something') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(text).toBe('done'); | 
					
						
							| 
									
										
										
										
											2022-06-13 16:56:16 -08:00
										 |  |  |   expect(interceptedRequest.headers()['origin']).toEqual(undefined); | 
					
						
							| 
									
										
										
										
											2022-05-02 12:38:33 -07:00
										 |  |  |   expect(serverRequest.headers.origin).toBeFalsy(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-08-30 10:35:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should continue preload link requests', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16745' }); | 
					
						
							|  |  |  |   let intercepted = false; | 
					
						
							|  |  |  |   await page.route('**/one-style.css', route => { | 
					
						
							|  |  |  |     intercepted = true; | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ | 
					
						
							| 
									
										
										
										
											2022-08-30 10:35:55 -07:00
										 |  |  |       headers: { | 
					
						
							|  |  |  |         ...route.request().headers(), | 
					
						
							|  |  |  |         'custom': 'value' | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |     server.waitForRequest('/one-style.css'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/preload.html') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(serverRequest.headers['custom']).toBe('value'); | 
					
						
							|  |  |  |   await page.waitForFunction(() => (window as any).preloadedStyles); | 
					
						
							|  |  |  |   expect(intercepted).toBe(true); | 
					
						
							|  |  |  |   const color = await page.evaluate(() => window.getComputedStyle(document.body).backgroundColor); | 
					
						
							|  |  |  |   expect(color).toBe('rgb(255, 192, 203)'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-01-26 14:20:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-22 14:31:41 -08:00
										 |  |  | it('continue should propagate headers to redirects', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/28758' }); | 
					
						
							| 
									
										
										
										
											2024-01-12 08:55:20 -08:00
										 |  |  |   it.fixme(browserName === 'firefox'); | 
					
						
							| 
									
										
										
										
											2023-12-22 14:31:41 -08:00
										 |  |  |   await server.setRedirect('/redirect', '/empty.html'); | 
					
						
							|  |  |  |   await page.route('**/redirect', route => { | 
					
						
							|  |  |  |     void route.continue({ | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         ...route.request().headers(), | 
					
						
							| 
									
										
										
										
											2024-01-12 08:55:20 -08:00
										 |  |  |         custom: 'value' | 
					
						
							| 
									
										
										
										
											2023-12-22 14:31:41 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const [serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |     server.waitForRequest('/empty.html'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/redirect') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(serverRequest.headers['custom']).toBe('value'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-21 17:44:58 -07:00
										 |  |  | it('redirected requests should report overridden headers', { | 
					
						
							|  |  |  |   annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/31351' } | 
					
						
							|  |  |  | }, async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.fixme(browserName === 'firefox'); | 
					
						
							|  |  |  |   await server.setRedirect('/redirect', '/empty.html'); | 
					
						
							|  |  |  |   await page.route('**/redirect', route => { | 
					
						
							|  |  |  |     const headers = route.request().headers(); | 
					
						
							|  |  |  |     headers['custom'] = 'value'; | 
					
						
							|  |  |  |     void route.fallback({ headers }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const [serverRequest, response] = await Promise.all([ | 
					
						
							|  |  |  |     server.waitForRequest('/empty.html'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/redirect') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(serverRequest.headers['custom']).toBe('value'); | 
					
						
							|  |  |  |   expect(response.request().url()).toBe(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(response.request().headers()['custom']).toBe('value'); | 
					
						
							|  |  |  |   expect((await response.request().allHeaders())['custom']).toBe('value'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 08:55:20 -08:00
										 |  |  | it('continue should delete headers on redirects', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/13106' }); | 
					
						
							|  |  |  |   it.fixme(browserName === 'firefox'); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   server.setRoute('/something', (request, response) => { | 
					
						
							|  |  |  |     response.writeHead(200, { 'Access-Control-Allow-Origin': '*' }); | 
					
						
							|  |  |  |     response.end('done'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await server.setRedirect('/redirect', '/something'); | 
					
						
							|  |  |  |   await page.route('**/redirect', route => { | 
					
						
							|  |  |  |     void route.continue({ | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         ...route.request().headers(), | 
					
						
							|  |  |  |         foo: undefined | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const [text, serverRequest] = await Promise.all([ | 
					
						
							|  |  |  |     page.evaluate(async url => { | 
					
						
							|  |  |  |       const data = await fetch(url, { | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |           foo: 'a', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return data.text(); | 
					
						
							|  |  |  |     }, server.PREFIX + '/redirect'), | 
					
						
							|  |  |  |     server.waitForRequest('/something') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(text).toBe('done'); | 
					
						
							|  |  |  |   expect(serverRequest.headers.foo).toBeFalsy(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 11:09:40 -08:00
										 |  |  | it('should intercept css variable with background url', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2023-01-26 14:20:24 -08:00
										 |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/19158' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   server.setRoute('/test.html', (request, response) => { | 
					
						
							|  |  |  |     response.setHeader('Content-Type', 'text/html'); | 
					
						
							|  |  |  |     response.end(`
 | 
					
						
							|  |  |  |     <style> | 
					
						
							|  |  |  |       @keyframes JNDzq { | 
					
						
							|  |  |  |         0% { background-position: 0 0 } | 
					
						
							|  |  |  |         to { background-position: 100 0 } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       div { | 
					
						
							|  |  |  |         --background: url(/pptr.png); | 
					
						
							|  |  |  |         background-image: var(--background); | 
					
						
							|  |  |  |         animation: JNDzq 1s linear infinite; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     </style> | 
					
						
							|  |  |  |     <div>Yo!</div>`);
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   let interceptCallback; | 
					
						
							|  |  |  |   const interceptPromise = new Promise(f => interceptCallback = f); | 
					
						
							|  |  |  |   let interceptedRequests = 0; | 
					
						
							|  |  |  |   await page.route(server.PREFIX + '/pptr.png', async (route, request) => { | 
					
						
							|  |  |  |     ++interceptedRequests; | 
					
						
							|  |  |  |     interceptCallback(); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2023-01-26 14:20:24 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/test.html'); | 
					
						
							|  |  |  |   expect(await page.locator('div').textContent()).toBe('Yo!'); | 
					
						
							|  |  |  |   await interceptPromise; | 
					
						
							|  |  |  |   await page.waitForTimeout(1000); | 
					
						
							|  |  |  |   expect(interceptedRequests).toBe(1); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2024-05-20 16:36:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('continue should not change multipart/form-data body', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/19158' }); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   server.setRoute('/upload', (request, response) => { | 
					
						
							|  |  |  |     response.writeHead(200, { 'Content-Type': 'text/plain' }); | 
					
						
							|  |  |  |     response.end('done'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   async function sendFormData() { | 
					
						
							|  |  |  |     const reqPromise = server.waitForRequest('/upload'); | 
					
						
							|  |  |  |     const status = await page.evaluate(async () => { | 
					
						
							|  |  |  |       const newFile = new File(['file content'], 'file.txt'); | 
					
						
							|  |  |  |       const formData = new FormData(); | 
					
						
							|  |  |  |       formData.append('file', newFile); | 
					
						
							|  |  |  |       const response = await fetch('/upload', { | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         credentials: 'include', | 
					
						
							|  |  |  |         body: formData, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return response.status; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const req = await reqPromise; | 
					
						
							|  |  |  |     expect(status).toBe(200); | 
					
						
							|  |  |  |     return req; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const reqBefore = await sendFormData(); | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     await route.continue(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const reqAfter = await sendFormData(); | 
					
						
							|  |  |  |   const fileContent = [ | 
					
						
							|  |  |  |     'Content-Disposition: form-data; name=\"file\"; filename=\"file.txt\"', | 
					
						
							|  |  |  |     'Content-Type: application/octet-stream', | 
					
						
							|  |  |  |     '', | 
					
						
							|  |  |  |     'file content', | 
					
						
							|  |  |  |     '------'].join('\r\n'); | 
					
						
							|  |  |  |   expect.soft((await reqBefore.postBody).toString('utf8')).toContain(fileContent); | 
					
						
							|  |  |  |   expect.soft((await reqAfter.postBody).toString('utf8')).toContain(fileContent); | 
					
						
							|  |  |  | }); |