| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-04 16:38:25 -07:00
										 |  |  | import os from 'os'; | 
					
						
							| 
									
										
										
										
											2022-04-06 13:57:14 -08:00
										 |  |  | import type { Route } from 'playwright-core'; | 
					
						
							| 
									
										
										
										
											2021-05-06 07:08:22 -07:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  | it('should intercept @smoke', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   let intercepted = false; | 
					
						
							|  |  |  |   await page.route('**/empty.html', (route, request) => { | 
					
						
							|  |  |  |     expect(route.request()).toBe(request); | 
					
						
							|  |  |  |     expect(request.url()).toContain('empty.html'); | 
					
						
							|  |  |  |     expect(request.headers()['user-agent']).toBeTruthy(); | 
					
						
							|  |  |  |     expect(request.method()).toBe('GET'); | 
					
						
							|  |  |  |     expect(request.postData()).toBe(null); | 
					
						
							|  |  |  |     expect(request.isNavigationRequest()).toBe(true); | 
					
						
							|  |  |  |     expect(request.resourceType()).toBe('document'); | 
					
						
							|  |  |  |     expect(request.frame() === page.mainFrame()).toBe(true); | 
					
						
							|  |  |  |     expect(request.frame().url()).toBe('about:blank'); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     intercepted = true; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  |   expect(intercepted).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should unroute', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   let intercepted = []; | 
					
						
							| 
									
										
										
										
											2021-07-13 20:22:01 +02:00
										 |  |  |   await page.route('**/*', route => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     intercepted.push(1); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.fallback(); | 
					
						
							| 
									
										
										
										
											2021-07-13 20:22:01 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.route('**/empty.html', route => { | 
					
						
							|  |  |  |     intercepted.push(2); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.fallback(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   await page.route('**/empty.html', route => { | 
					
						
							|  |  |  |     intercepted.push(3); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.fallback(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-07-13 20:22:01 +02:00
										 |  |  |   const handler4 = route => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     intercepted.push(4); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.fallback(); | 
					
						
							| 
									
										
										
										
											2021-07-13 20:22:01 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2023-05-17 16:27:32 -07:00
										 |  |  |   await page.route(/empty.html/, handler4); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2022-06-10 08:06:39 -08:00
										 |  |  |   expect(intercepted).toEqual([4, 3, 2, 1]); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   intercepted = []; | 
					
						
							| 
									
										
										
										
											2023-05-17 16:27:32 -07:00
										 |  |  |   await page.unroute(/empty.html/, handler4); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2022-06-10 08:06:39 -08:00
										 |  |  |   expect(intercepted).toEqual([3, 2, 1]); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   intercepted = []; | 
					
						
							|  |  |  |   await page.unroute('**/empty.html'); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-07-13 20:22:01 +02:00
										 |  |  |   expect(intercepted).toEqual([1]); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-28 16:33:04 -07:00
										 |  |  | it('should support ? in glob pattern', async ({ page, server }) => { | 
					
						
							|  |  |  |   server.setRoute('/index', (req, res) => res.end('index-no-hello')); | 
					
						
							|  |  |  |   server.setRoute('/index123hello', (req, res) => res.end('index123hello')); | 
					
						
							|  |  |  |   server.setRoute('/index?hello', (req, res) => res.end('index?hello')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.route('**/index?hello', async (route, request) => { | 
					
						
							|  |  |  |     await route.fulfill({ body: 'intercepted any character' }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.route('**/index\\?hello', async (route, request) => { | 
					
						
							|  |  |  |     await route.fulfill({ body: 'intercepted question mark' }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/index?hello'); | 
					
						
							|  |  |  |   expect(await page.content()).toContain('intercepted question mark'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/index'); | 
					
						
							|  |  |  |   expect(await page.content()).toContain('index-no-hello'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/index1hello'); | 
					
						
							|  |  |  |   expect(await page.content()).toContain('intercepted any character'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/index123hello'); | 
					
						
							|  |  |  |   expect(await page.content()).toContain('index123hello'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work when POST is redirected with 302', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   server.setRedirect('/rredirect', '/empty.html'); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue()); | 
					
						
							|  |  |  |   await page.setContent(`
 | 
					
						
							|  |  |  |     <form action='/rredirect' method='post'> | 
					
						
							|  |  |  |       <input type="hidden" id="foo" name="foo" value="FOOBAR"> | 
					
						
							|  |  |  |     </form> | 
					
						
							|  |  |  |   `);
 | 
					
						
							|  |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     page.$eval('form', form => form.submit()), | 
					
						
							|  |  |  |     page.waitForNavigation() | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-06-13 16:56:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | // @see https://github.com/GoogleChrome/puppeteer/issues/3973
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work when header manipulation headers with redirect', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   server.setRedirect('/rrredirect', '/empty.html'); | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     const headers = Object.assign({}, route.request().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.PREFIX + '/rrredirect'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-06-13 16:56:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | // @see https://github.com/GoogleChrome/puppeteer/issues/4743
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should be able to remove headers', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-10-15 10:31:07 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-09-02 20:48:23 -07:00
										 |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     const headers = { ...route.request().headers() }; | 
					
						
							|  |  |  |     delete headers['foo']; | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ headers }); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const [serverRequest] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-10-15 10:31:07 -07:00
										 |  |  |     server.waitForRequest('/title.html'), | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     page.evaluate(url => fetch(url, { headers: { foo: 'bar' } }), server.PREFIX + '/title.html') | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2020-10-15 10:31:07 -07:00
										 |  |  |   expect(serverRequest.headers.foo).toBe(undefined); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should contain referer header', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const requests = []; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     requests.push(route.request()); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/one-style.html'); | 
					
						
							|  |  |  |   expect(requests[1].url()).toContain('/one-style.css'); | 
					
						
							|  |  |  |   expect(requests[1].headers().referer).toContain('/one-style.html'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-21 00:43:26 +02:00
										 |  |  | it('should properly return navigation response when URL has cookies', async ({ page, server, isAndroid, isElectron, electronMajorVersion }) => { | 
					
						
							| 
									
										
										
										
											2021-04-09 07:59:09 -07:00
										 |  |  |   it.skip(isAndroid, 'No isolated context'); | 
					
						
							| 
									
										
										
										
											2024-06-21 00:43:26 +02:00
										 |  |  |   it.skip(isElectron && electronMajorVersion < 30, 'error: Browser context management is not supported.'); | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   // Setup cookie.
 | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await page.context().addCookies([{ url: server.EMPTY_PAGE, name: 'foo', value: 'bar' }]); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Setup request interception.
 | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue()); | 
					
						
							|  |  |  |   const response = await page.reload(); | 
					
						
							|  |  |  |   expect(response.status()).toBe(200); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 14:06:25 -07:00
										 |  |  | it('should override cookie header', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16773' }); | 
					
						
							|  |  |  |   it.fail(browserName !== 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.evaluate(() => document.cookie = 'original=value'); | 
					
						
							|  |  |  |   let cookieValueInRoute; | 
					
						
							|  |  |  |   await page.route('**', async route => { | 
					
						
							|  |  |  |     const headers = await route.request().allHeaders(); | 
					
						
							|  |  |  |     cookieValueInRoute = headers['cookie']; | 
					
						
							|  |  |  |     headers['cookie'] = 'overridden=value'; | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue({ headers }); | 
					
						
							| 
									
										
										
										
											2022-08-23 14:06:25 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const [serverReq] = await Promise.all([ | 
					
						
							|  |  |  |     server.waitForRequest('/empty.html'), | 
					
						
							|  |  |  |     page.goto(server.EMPTY_PAGE), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(cookieValueInRoute).toBe('original=value'); | 
					
						
							|  |  |  |   expect(serverReq.headers['cookie']).toBe('overridden=value'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should show custom HTTP headers', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.setExtraHTTPHeaders({ | 
					
						
							|  |  |  |     foo: 'bar' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     expect(route.request().headers()['foo']).toBe('bar'); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-06-13 16:56:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | // @see https://github.com/GoogleChrome/puppeteer/issues/4337
 | 
					
						
							| 
									
										
										
										
											2023-12-05 09:58:15 -08:00
										 |  |  | it('should work with redirect inside sync XHR', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/28461' }); | 
					
						
							|  |  |  |   it.fixme(browserName === 'webkit', 'No Network.requestIntercepted for the request'); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   server.setRedirect('/logo.png', '/pptr.png'); | 
					
						
							| 
									
										
										
										
											2023-12-05 09:58:15 -08:00
										 |  |  |   let continuePromise; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     continuePromise = route.continue(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   const status = await page.evaluate(async () => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     const request = new XMLHttpRequest(); | 
					
						
							|  |  |  |     request.open('GET', '/logo.png', false);  // `false` makes the request synchronous
 | 
					
						
							|  |  |  |     request.send(null); | 
					
						
							|  |  |  |     return request.status; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(status).toBe(200); | 
					
						
							| 
									
										
										
										
											2023-12-05 09:58:15 -08:00
										 |  |  |   expect(continuePromise).toBeTruthy(); | 
					
						
							|  |  |  |   await continuePromise; | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should pause intercepted XHR until continue', async ({ page, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   it.fixme(browserName === 'webkit', 'Redirected request is not paused in WebKit'); | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 19:09:18 -08:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let resolveRoute; | 
					
						
							|  |  |  |   const routePromise = new Promise(r => resolveRoute = r); | 
					
						
							|  |  |  |   await page.route('**/global-var.html', async route => resolveRoute(route)); | 
					
						
							|  |  |  |   let xhrFinished = false; | 
					
						
							|  |  |  |   const statusPromise = page.evaluate(async () => { | 
					
						
							|  |  |  |     const request = new XMLHttpRequest(); | 
					
						
							|  |  |  |     request.open('GET', '/global-var.html', false);  // `false` makes the request synchronous
 | 
					
						
							|  |  |  |     request.send(null); | 
					
						
							|  |  |  |     return request.status; | 
					
						
							|  |  |  |   }).then(r => { | 
					
						
							|  |  |  |     xhrFinished = true; | 
					
						
							|  |  |  |     return r; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const route = await routePromise; | 
					
						
							|  |  |  |   // Check that intercepted request is actually paused.
 | 
					
						
							|  |  |  |   await new Promise(r => setTimeout(r, 500)); | 
					
						
							|  |  |  |   expect(xhrFinished).toBe(false); | 
					
						
							|  |  |  |   const [status] = await Promise.all([ | 
					
						
							|  |  |  |     statusPromise, | 
					
						
							|  |  |  |     (route as any).continue() | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(status).toBe(200); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should pause intercepted fetch request until continue', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-11-18 19:09:18 -08:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let resolveRoute; | 
					
						
							|  |  |  |   const routePromise = new Promise(r => resolveRoute = r); | 
					
						
							|  |  |  |   await page.route('**/global-var.html', async route => resolveRoute(route)); | 
					
						
							|  |  |  |   let fetchFinished = false; | 
					
						
							|  |  |  |   const statusPromise = page.evaluate(async () => { | 
					
						
							|  |  |  |     const response = await fetch('/global-var.html'); | 
					
						
							|  |  |  |     return response.status; | 
					
						
							|  |  |  |   }).then(r => { | 
					
						
							|  |  |  |     fetchFinished = true; | 
					
						
							|  |  |  |     return r; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const route = await routePromise; | 
					
						
							|  |  |  |   // Check that intercepted request is actually paused.
 | 
					
						
							|  |  |  |   await new Promise(r => setTimeout(r, 500)); | 
					
						
							|  |  |  |   expect(fetchFinished).toBe(false); | 
					
						
							|  |  |  |   const [status] = await Promise.all([ | 
					
						
							|  |  |  |     statusPromise, | 
					
						
							|  |  |  |     (route as any).continue() | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(status).toBe(200); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with custom referer headers', async ({ page, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.setExtraHTTPHeaders({ 'referer': server.EMPTY_PAGE }); | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							| 
									
										
										
										
											2021-09-17 17:19:13 -07:00
										 |  |  |     // See https://github.com/microsoft/playwright/issues/8999
 | 
					
						
							|  |  |  |     if (browserName === 'chromium') | 
					
						
							|  |  |  |       expect(route.request().headers()['referer']).toBe(server.EMPTY_PAGE + ', ' + server.EMPTY_PAGE); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       expect(route.request().headers()['referer']).toBe(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should be abortable', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.route(/\.css$/, route => route.abort()); | 
					
						
							|  |  |  |   let failed = false; | 
					
						
							|  |  |  |   page.on('requestfailed', request => { | 
					
						
							|  |  |  |     if (request.url().includes('.css')) | 
					
						
							|  |  |  |       failed = true; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(server.PREFIX + '/one-style.html'); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  |   expect(response.request().failure()).toBe(null); | 
					
						
							|  |  |  |   expect(failed).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-04 16:38:25 -07:00
										 |  |  | it('should be abortable with custom error codes', async ({ page, server, browserName, isMac }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.route('**/*', route => route.abort('internetdisconnected')); | 
					
						
							|  |  |  |   let failedRequest = null; | 
					
						
							|  |  |  |   page.on('requestfailed', request => failedRequest = request); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE).catch(e => {}); | 
					
						
							|  |  |  |   expect(failedRequest).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2022-05-04 16:38:25 -07:00
										 |  |  |   const isFrozenWebKit = isMac && parseInt(os.release(), 10) < 20; | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   if (browserName === 'webkit') | 
					
						
							| 
									
										
										
										
											2022-05-04 16:38:25 -07:00
										 |  |  |     expect(failedRequest.failure().errorText).toBe(isFrozenWebKit ? 'Request intercepted' : 'Blocked by Web Inspector'); | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   else if (browserName === 'firefox') | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     expect(failedRequest.failure().errorText).toBe('NS_ERROR_OFFLINE'); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(failedRequest.failure().errorText).toBe('net::ERR_INTERNET_DISCONNECTED'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 16:57:39 -08:00
										 |  |  | it('should not throw if request was cancelled by the page', async ({ page, server }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/28490' }); | 
					
						
							|  |  |  |   let interceptCallback; | 
					
						
							|  |  |  |   const interceptPromise = new Promise<Route>(f => interceptCallback = f); | 
					
						
							| 
									
										
										
										
											2023-12-15 09:00:12 -08:00
										 |  |  |   await page.route('**/data.json', route => interceptCallback(route)); | 
					
						
							| 
									
										
										
										
											2023-12-07 16:57:39 -08:00
										 |  |  |   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.abort(); // Should not throw.
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should send referer', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.setExtraHTTPHeaders({ | 
					
						
							|  |  |  |     referer: 'http://google.com/' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue()); | 
					
						
							|  |  |  |   const [request] = await Promise.all([ | 
					
						
							|  |  |  |     server.waitForRequest('/grid.html'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/grid.html'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(request.headers['referer']).toBe('http://google.com/'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-04 16:38:25 -07:00
										 |  |  | it('should fail navigation when aborting main resource', async ({ page, server, browserName, isMac }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.route('**/*', route => route.abort()); | 
					
						
							|  |  |  |   let error = null; | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE).catch(e => error = e); | 
					
						
							|  |  |  |   expect(error).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2022-05-04 16:38:25 -07:00
										 |  |  |   const isFrozenWebKit = isMac && parseInt(os.release(), 10) < 20; | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   if (browserName === 'webkit') | 
					
						
							| 
									
										
										
										
											2022-05-04 16:38:25 -07:00
										 |  |  |     expect(error.message).toContain(isFrozenWebKit ? 'Request intercepted' : 'Blocked by Web Inspector'); | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   else if (browserName === 'firefox') | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     expect(error.message).toContain('NS_ERROR_FAILURE'); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(error.message).toContain('net::ERR_FAILED'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should not work with redirects', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const intercepted = []; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     intercepted.push(route.request()); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   server.setRedirect('/non-existing-page.html', '/non-existing-page-2.html'); | 
					
						
							|  |  |  |   server.setRedirect('/non-existing-page-2.html', '/non-existing-page-3.html'); | 
					
						
							|  |  |  |   server.setRedirect('/non-existing-page-3.html', '/non-existing-page-4.html'); | 
					
						
							|  |  |  |   server.setRedirect('/non-existing-page-4.html', '/empty.html'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const response = await page.goto(server.PREFIX + '/non-existing-page.html'); | 
					
						
							|  |  |  |   expect(response.status()).toBe(200); | 
					
						
							|  |  |  |   expect(response.url()).toContain('empty.html'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(intercepted.length).toBe(1); | 
					
						
							|  |  |  |   expect(intercepted[0].resourceType()).toBe('document'); | 
					
						
							|  |  |  |   expect(intercepted[0].isNavigationRequest()).toBe(true); | 
					
						
							|  |  |  |   expect(intercepted[0].url()).toContain('/non-existing-page.html'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const chain = []; | 
					
						
							|  |  |  |   for (let r = response.request(); r; r = r.redirectedFrom()) { | 
					
						
							|  |  |  |     chain.push(r); | 
					
						
							|  |  |  |     expect(r.isNavigationRequest()).toBe(true); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(chain.length).toBe(5); | 
					
						
							|  |  |  |   expect(chain[0].url()).toContain('/empty.html'); | 
					
						
							|  |  |  |   expect(chain[1].url()).toContain('/non-existing-page-4.html'); | 
					
						
							|  |  |  |   expect(chain[2].url()).toContain('/non-existing-page-3.html'); | 
					
						
							|  |  |  |   expect(chain[3].url()).toContain('/non-existing-page-2.html'); | 
					
						
							|  |  |  |   expect(chain[4].url()).toContain('/non-existing-page.html'); | 
					
						
							|  |  |  |   for (let i = 0; i < chain.length; i++) | 
					
						
							|  |  |  |     expect(chain[i].redirectedTo()).toBe(i ? chain[i - 1] : null); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 10:48:16 -07:00
										 |  |  | it('should chain fallback w/ dynamic URL', async ({ page, server }) => { | 
					
						
							|  |  |  |   const intercepted = []; | 
					
						
							|  |  |  |   await page.route('**/bar', route => { | 
					
						
							|  |  |  |     intercepted.push(1); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.fallback({ url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2022-06-24 10:48:16 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   await page.route('**/foo', route => { | 
					
						
							|  |  |  |     intercepted.push(2); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.fallback({ url: 'http://localhost/bar' }); | 
					
						
							| 
									
										
										
										
											2022-06-24 10:48:16 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.route('**/empty.html', route => { | 
					
						
							|  |  |  |     intercepted.push(3); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.fallback({ url: 'http://localhost/foo' }); | 
					
						
							| 
									
										
										
										
											2022-06-24 10:48:16 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(intercepted).toEqual([3, 2, 1]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with redirects for subresources', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const intercepted = []; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     intercepted.push(route.request()); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   server.setRedirect('/one-style.css', '/two-style.css'); | 
					
						
							|  |  |  |   server.setRedirect('/two-style.css', '/three-style.css'); | 
					
						
							|  |  |  |   server.setRedirect('/three-style.css', '/four-style.css'); | 
					
						
							|  |  |  |   server.setRoute('/four-style.css', (req, res) => res.end('body {box-sizing: border-box; }')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const response = await page.goto(server.PREFIX + '/one-style.html'); | 
					
						
							|  |  |  |   expect(response.status()).toBe(200); | 
					
						
							|  |  |  |   expect(response.url()).toContain('one-style.html'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(intercepted.length).toBe(2); | 
					
						
							|  |  |  |   expect(intercepted[0].resourceType()).toBe('document'); | 
					
						
							|  |  |  |   expect(intercepted[0].url()).toContain('one-style.html'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let r = intercepted[1]; | 
					
						
							|  |  |  |   for (const url of ['/one-style.css', '/two-style.css', '/three-style.css', '/four-style.css']) { | 
					
						
							|  |  |  |     expect(r.resourceType()).toBe('stylesheet'); | 
					
						
							|  |  |  |     expect(r.url()).toContain(url); | 
					
						
							|  |  |  |     r = r.redirectedTo(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(r).toBe(null); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with equal requests', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let responseCount = 1; | 
					
						
							|  |  |  |   server.setRoute('/zzz', (req, res) => res.end((responseCount++) * 11 + '')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let spinner = false; | 
					
						
							|  |  |  |   // Cancel 2nd request.
 | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void (spinner ? route.abort() : route.continue()); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     spinner = !spinner; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const results = []; | 
					
						
							|  |  |  |   for (let i = 0; i < 3; i++) | 
					
						
							|  |  |  |     results.push(await page.evaluate(() => fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'))); | 
					
						
							|  |  |  |   expect(results).toEqual(['11', 'FAILED', '22']); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should navigate to dataURL and not fire dataURL requests', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const requests = []; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     requests.push(route.request()); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const dataURL = 'data:text/html,<div>yo</div>'; | 
					
						
							|  |  |  |   const response = await page.goto(dataURL); | 
					
						
							|  |  |  |   expect(response).toBe(null); | 
					
						
							|  |  |  |   expect(requests.length).toBe(0); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should be able to fetch dataURL and not fire dataURL requests', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const requests = []; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     requests.push(route.request()); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const dataURL = 'data:text/html,<div>yo</div>'; | 
					
						
							|  |  |  |   const text = await page.evaluate(url => fetch(url).then(r => r.text()), dataURL); | 
					
						
							|  |  |  |   expect(text).toBe('<div>yo</div>'); | 
					
						
							|  |  |  |   expect(requests.length).toBe(0); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should navigate to URL with hash and and fire requests without hash', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const requests = []; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     requests.push(route.request()); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(server.EMPTY_PAGE + '#hash'); | 
					
						
							|  |  |  |   expect(response.status()).toBe(200); | 
					
						
							|  |  |  |   expect(response.url()).toBe(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(requests.length).toBe(1); | 
					
						
							|  |  |  |   expect(requests[0].url()).toBe(server.EMPTY_PAGE); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with encoded server', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   // The requestWillBeSent will report encoded URL, whereas interception will
 | 
					
						
							|  |  |  |   // report URL as-is. @see crbug.com/759388
 | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue()); | 
					
						
							|  |  |  |   const response = await page.goto(server.PREFIX + '/some nonexisting page'); | 
					
						
							|  |  |  |   expect(response.status()).toBe(404); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work with badly encoded server', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   server.setRoute('/malformed?rnd=%911', (req, res) => res.end()); | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue()); | 
					
						
							|  |  |  |   const response = await page.goto(server.PREFIX + '/malformed?rnd=%911'); | 
					
						
							|  |  |  |   expect(response.status()).toBe(200); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 01:17:33 -07:00
										 |  |  | it('should work with encoded server - 2', async ({ page, server, browserName, browserMajorVersion }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   // The requestWillBeSent will report URL as-is, whereas interception will
 | 
					
						
							|  |  |  |   // report encoded URL for stylesheet. @see crbug.com/759388
 | 
					
						
							|  |  |  |   const requests = []; | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     requests.push(route.request()); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(`data:text/html,<link rel="stylesheet" href="${server.PREFIX}/fonts?helvetica|arial"/>`); | 
					
						
							|  |  |  |   expect(response).toBe(null); | 
					
						
							| 
									
										
										
										
											2022-03-07 17:16:42 -07:00
										 |  |  |   if (browserName === 'firefox' && browserMajorVersion >= 97) | 
					
						
							| 
									
										
										
										
											2022-03-01 01:17:33 -07:00
										 |  |  |     expect(requests.length).toBe(2); // Firefox DevTools report to navigations in this case as well.
 | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     expect(requests.length).toBe(1); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   expect((await requests[0].response()).status()).toBe(404); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should not throw "Invalid Interception Id" if the request was cancelled', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.setContent('<iframe></iframe>'); | 
					
						
							|  |  |  |   let route = null; | 
					
						
							|  |  |  |   await page.route('**/*', async r => route = r); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |   void page.$eval('iframe', (frame, url) => frame.src = url, server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   // Wait for request interception.
 | 
					
						
							|  |  |  |   await page.waitForEvent('request'); | 
					
						
							|  |  |  |   // Delete frame to cause request to be canceled.
 | 
					
						
							|  |  |  |   await page.$eval('iframe', frame => frame.remove()); | 
					
						
							|  |  |  |   let error = null; | 
					
						
							|  |  |  |   await route.continue().catch(e => error = e); | 
					
						
							|  |  |  |   expect(error).toBe(null); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should intercept main resource during cross-process navigation', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let intercepted = false; | 
					
						
							|  |  |  |   await page.route(server.CROSS_PROCESS_PREFIX + '/empty.html', route => { | 
					
						
							|  |  |  |     intercepted = true; | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const response = await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  |   expect(intercepted).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should fulfill with redirect status', async ({ page, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   it.fixme(browserName === 'webkit', 'in WebKit the redirects are handled by the network stack and we intercept before'); | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |   await page.goto(server.PREFIX + '/title.html'); | 
					
						
							| 
									
										
										
										
											2020-11-19 12:10:44 -08:00
										 |  |  |   server.setRoute('/final', (req, res) => res.end('foo')); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.route('**/*', async (route, request) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     if (request.url() !== server.PREFIX + '/redirect_this') | 
					
						
							|  |  |  |       return route.continue(); | 
					
						
							|  |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       status: 301, | 
					
						
							|  |  |  |       headers: { | 
					
						
							| 
									
										
										
										
											2020-11-19 12:10:44 -08:00
										 |  |  |         'location': '/final', | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const text = await page.evaluate(async url => { | 
					
						
							|  |  |  |     const data = await fetch(url); | 
					
						
							|  |  |  |     return data.text(); | 
					
						
							|  |  |  |   }, server.PREFIX + '/redirect_this'); | 
					
						
							| 
									
										
										
										
											2020-11-19 12:10:44 -08:00
										 |  |  |   expect(text).toBe('foo'); | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should not fulfill with redirect status', async ({ page, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   it.skip(browserName !== 'webkit', 'we should support fulfill with redirect in webkit and delete this test'); | 
					
						
							| 
									
										
										
										
											2021-04-02 14:23:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let status; | 
					
						
							|  |  |  |   let fulfill; | 
					
						
							|  |  |  |   let reject; | 
					
						
							|  |  |  |   await page.route('**/*', async (route, request) => { | 
					
						
							|  |  |  |     if (request.url() !== server.PREFIX + '/redirect_this') | 
					
						
							|  |  |  |       return route.continue(); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await route.fulfill({ | 
					
						
							|  |  |  |         status, | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |           'location': '/empty.html', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2024-05-08 20:40:03 +02:00
										 |  |  |       reject('fulfill didn\'t throw'); | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |     } catch (e) { | 
					
						
							|  |  |  |       fulfill(e); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-15 09:00:12 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (status = 300; status < 310; status++) { | 
					
						
							| 
									
										
										
										
											2023-06-01 23:11:03 -07:00
										 |  |  |     const [, exception] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |       page.evaluate(url => location.href = url, server.PREFIX + '/redirect_this'), | 
					
						
							| 
									
										
										
										
											2023-06-01 23:11:03 -07:00
										 |  |  |       new Promise<Error>((f, r) => {fulfill = f; reject = r;}) | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2020-11-17 16:56:04 -08:00
										 |  |  |     expect(exception).toBeTruthy(); | 
					
						
							|  |  |  |     expect(exception.message.includes('Cannot fulfill with redirect status')).toBe(true); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should support cors with GET', async ({ page, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.route('**/cars*', async (route, request) => { | 
					
						
							| 
									
										
										
										
											2022-03-31 19:21:21 -07:00
										 |  |  |     const headers = { 'access-control-allow-origin': request.url().endsWith('allow') ? '*' : 'none' }; | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       contentType: 'application/json', | 
					
						
							|  |  |  |       headers, | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: JSON.stringify(['electric', 'gas']), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Should succeed
 | 
					
						
							|  |  |  |     const resp = await page.evaluate(async () => { | 
					
						
							|  |  |  |       const response = await fetch('https://example.com/cars?allow', { mode: 'cors' }); | 
					
						
							|  |  |  |       return response.json(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(resp).toEqual(['electric', 'gas']); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Should be rejected
 | 
					
						
							|  |  |  |     const error = await page.evaluate(async () => { | 
					
						
							|  |  |  |       const response = await fetch('https://example.com/cars?reject', { mode: 'cors' }); | 
					
						
							|  |  |  |       return response.json(); | 
					
						
							|  |  |  |     }).catch(e => e); | 
					
						
							| 
									
										
										
										
											2021-08-26 18:44:49 -07:00
										 |  |  |     if (browserName === 'chromium') | 
					
						
							|  |  |  |       expect(error.message).toContain('Failed'); | 
					
						
							|  |  |  |     if (browserName === 'webkit') | 
					
						
							|  |  |  |       expect(error.message).toContain('TypeError'); | 
					
						
							|  |  |  |     if (browserName === 'firefox') | 
					
						
							|  |  |  |       expect(error.message).toContain('NetworkError'); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 14:56:57 -07:00
										 |  |  | it('should add Access-Control-Allow-Origin by default when fulfill', async ({ page, server }) => { | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.route('**/cars', async route => { | 
					
						
							|  |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       contentType: 'application/json', | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: JSON.stringify(['electric', 'gas']), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const [result, response] = await Promise.all([ | 
					
						
							|  |  |  |     page.evaluate(async () => { | 
					
						
							|  |  |  |       const response = await fetch('https://example.com/cars', { | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         headers: { 'Content-Type': 'application/json' }, | 
					
						
							|  |  |  |         mode: 'cors', | 
					
						
							|  |  |  |         body: JSON.stringify({ 'number': 1 }) | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return response.json(); | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |     page.waitForResponse('https://example.com/cars') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(result).toEqual(['electric', 'gas']); | 
					
						
							|  |  |  |   expect(await response.headerValue('Access-Control-Allow-Origin')).toBe(server.PREFIX); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 19:21:21 -07:00
										 |  |  | it('should allow null origin for about:blank', async ({ page, server, browserName }) => { | 
					
						
							|  |  |  |   await page.route('**/something', async route => { | 
					
						
							|  |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       contentType: 'text/plain', | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: 'done', | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const [response, text] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForResponse(server.CROSS_PROCESS_PREFIX + '/something'), | 
					
						
							|  |  |  |     page.evaluate(async url => { | 
					
						
							|  |  |  |       const data = await fetch(url, { | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         headers: { 'X-PINGOTHER': 'pingpong' } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return data.text(); | 
					
						
							|  |  |  |     }, server.CROSS_PROCESS_PREFIX + '/something') | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(text).toBe('done'); | 
					
						
							|  |  |  |   expect(await response.headerValue('Access-Control-Allow-Origin')).toBe('null'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 05:34:05 -06:00
										 |  |  | it('should respect cors overrides', async ({ page, server, browserName, isAndroid }) => { | 
					
						
							|  |  |  |   it.fail(isAndroid, 'no cors error in android emulator'); | 
					
						
							| 
									
										
										
										
											2022-03-31 19:21:21 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2022-03-25 14:56:57 -07:00
										 |  |  |   server.setRoute('/something', (request, response) => { | 
					
						
							|  |  |  |     if (request.method === 'OPTIONS') { | 
					
						
							|  |  |  |       response.writeHead(204, { | 
					
						
							|  |  |  |         'Access-Control-Allow-Origin': '*', | 
					
						
							|  |  |  |         'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, DELETE', | 
					
						
							|  |  |  |         'Access-Control-Allow-Headers': '*', | 
					
						
							|  |  |  |         'Cache-Control': 'no-cache' | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       response.end(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     response.writeHead(404, { 'Access-Control-Allow-Origin': '*' }); | 
					
						
							|  |  |  |     response.end('NOT FOUND'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-03-31 19:21:21 -07:00
										 |  |  |   // Fetch request should fail when CORS header doesn't include the origin.
 | 
					
						
							| 
									
										
										
										
											2022-03-25 14:56:57 -07:00
										 |  |  |   { | 
					
						
							|  |  |  |     await page.route('**/something', async route => { | 
					
						
							|  |  |  |       await route.fulfill({ | 
					
						
							|  |  |  |         contentType: 'text/plain', | 
					
						
							|  |  |  |         status: 200, | 
					
						
							| 
									
										
										
										
											2022-03-31 19:21:21 -07:00
										 |  |  |         headers: { 'Access-Control-Allow-Origin': 'http://non-existent' }, | 
					
						
							| 
									
										
										
										
											2022-03-25 14:56:57 -07:00
										 |  |  |         body: 'done', | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const error = await page.evaluate(async url => { | 
					
						
							|  |  |  |       const data = await fetch(url, { | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         headers: { 'X-PINGOTHER': 'pingpong' } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return data.text(); | 
					
						
							|  |  |  |     }, server.CROSS_PROCESS_PREFIX + '/something').catch(e => e); | 
					
						
							|  |  |  |     if (browserName === 'chromium') | 
					
						
							|  |  |  |       expect(error.message).toContain('Failed to fetch'); | 
					
						
							|  |  |  |     else if (browserName === 'webkit') | 
					
						
							|  |  |  |       expect(error.message).toContain('Load failed'); | 
					
						
							|  |  |  |     else if (browserName === 'firefox') | 
					
						
							|  |  |  |       expect(error.message).toContain('NetworkError when attempting to fetch resource.'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-28 10:25:37 -07:00
										 |  |  | it('should not auto-intercept non-preflight OPTIONS', async ({ page, server, isAndroid, browserName }) => { | 
					
						
							| 
									
										
										
										
											2023-02-07 15:10:44 -08:00
										 |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/20469' }); | 
					
						
							| 
									
										
										
										
											2023-03-07 17:53:39 +01:00
										 |  |  |   it.fixme(isAndroid); | 
					
						
							| 
									
										
										
										
											2023-02-07 15:10:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let requests = []; | 
					
						
							|  |  |  |   server.setRoute('/something', (request, response) => { | 
					
						
							|  |  |  |     requests.push(request.method + ':' + request.url); | 
					
						
							|  |  |  |     if (request.method === 'OPTIONS') { | 
					
						
							|  |  |  |       response.writeHead(200, { | 
					
						
							|  |  |  |         'Access-Control-Allow-Origin': '*', | 
					
						
							|  |  |  |         'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, DELETE', | 
					
						
							|  |  |  |         'Access-Control-Allow-Headers': '*', | 
					
						
							|  |  |  |         'Cache-Control': 'no-cache' | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       response.end(`Hello`); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     response.writeHead(200, { 'Access-Control-Allow-Origin': '*' }); | 
					
						
							|  |  |  |     response.end('World'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Without interception.
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     requests = []; | 
					
						
							|  |  |  |     const [text1, text2] = await page.evaluate(async url => { | 
					
						
							|  |  |  |       const response1 = await fetch(url, { method: 'OPTIONS' }); | 
					
						
							|  |  |  |       const text1 = await response1.text(); | 
					
						
							|  |  |  |       const response2 = await fetch(url, { method: 'GET' }); | 
					
						
							|  |  |  |       const text2 = await response2.text(); | 
					
						
							|  |  |  |       return [text1, text2]; | 
					
						
							|  |  |  |     }, server.CROSS_PROCESS_PREFIX + '/something'); | 
					
						
							|  |  |  |     expect.soft(text1).toBe('Hello'); | 
					
						
							|  |  |  |     expect.soft(text2).toBe('World'); | 
					
						
							|  |  |  |     // Preflight for OPTIONS, then OPTIONS, then GET without preflight.
 | 
					
						
							|  |  |  |     expect.soft(requests).toEqual(['OPTIONS:/something', 'OPTIONS:/something', 'GET:/something']); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // With interception.
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     await page.route('**/something', route => route.continue()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     requests = []; | 
					
						
							|  |  |  |     const [text1, text2] = await page.evaluate(async url => { | 
					
						
							|  |  |  |       const response1 = await fetch(url, { method: 'OPTIONS' }); | 
					
						
							|  |  |  |       const text1 = await response1.text(); | 
					
						
							|  |  |  |       const response2 = await fetch(url, { method: 'GET' }); | 
					
						
							|  |  |  |       const text2 = await response2.text(); | 
					
						
							|  |  |  |       return [text1, text2]; | 
					
						
							|  |  |  |     }, server.CROSS_PROCESS_PREFIX + '/something'); | 
					
						
							|  |  |  |     expect.soft(text1).toBe('Hello'); | 
					
						
							|  |  |  |     expect.soft(text2).toBe('World'); | 
					
						
							|  |  |  |     // Preflight for OPTIONS is auto-fulfilled, then OPTIONS, then GET without preflight.
 | 
					
						
							| 
									
										
										
										
											2024-03-28 10:25:37 -07:00
										 |  |  |     if (browserName === 'firefox') | 
					
						
							|  |  |  |       expect.soft(requests).toEqual(['OPTIONS:/something', 'OPTIONS:/something', 'GET:/something']); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       expect.soft(requests).toEqual(['OPTIONS:/something', 'GET:/something']); | 
					
						
							| 
									
										
										
										
											2023-02-07 15:10:44 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should support cors with POST', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.route('**/cars', async route => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       contentType: 'application/json', | 
					
						
							|  |  |  |       headers: { 'Access-Control-Allow-Origin': '*' }, | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: JSON.stringify(['electric', 'gas']), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const resp = await page.evaluate(async () => { | 
					
						
							|  |  |  |     const response = await fetch('https://example.com/cars', { | 
					
						
							|  |  |  |       method: 'POST', | 
					
						
							|  |  |  |       headers: { 'Content-Type': 'application/json' }, | 
					
						
							|  |  |  |       mode: 'cors', | 
					
						
							|  |  |  |       body: JSON.stringify({ 'number': 1 }) | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return response.json(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(resp).toEqual(['electric', 'gas']); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should support cors with credentials', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-07 14:02:41 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.route('**/cars', async route => { | 
					
						
							| 
									
										
										
										
											2020-08-07 14:02:41 -07:00
										 |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       contentType: 'application/json', | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         'Access-Control-Allow-Origin': server.PREFIX, | 
					
						
							|  |  |  |         'Access-Control-Allow-Credentials': 'true' | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: JSON.stringify(['electric', 'gas']), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const resp = await page.evaluate(async () => { | 
					
						
							|  |  |  |     const response = await fetch('https://example.com/cars', { | 
					
						
							|  |  |  |       method: 'POST', | 
					
						
							|  |  |  |       headers: { 'Content-Type': 'application/json' }, | 
					
						
							|  |  |  |       mode: 'cors', | 
					
						
							|  |  |  |       body: JSON.stringify({ 'number': 1 }), | 
					
						
							|  |  |  |       credentials: 'include' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return response.json(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(resp).toEqual(['electric', 'gas']); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should reject cors with disallowed credentials', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-07 14:02:41 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.route('**/cars', async route => { | 
					
						
							| 
									
										
										
										
											2020-08-07 14:02:41 -07:00
										 |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       contentType: 'application/json', | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         'Access-Control-Allow-Origin': server.PREFIX, | 
					
						
							|  |  |  |         // Should fail without this line below!
 | 
					
						
							|  |  |  |         // 'Access-Control-Allow-Credentials': 'true'
 | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: JSON.stringify(['electric', 'gas']), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   let error = ''; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     await page.evaluate(async () => { | 
					
						
							| 
									
										
										
										
											2020-08-07 14:02:41 -07:00
										 |  |  |       const response = await fetch('https://example.com/cars', { | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         headers: { 'Content-Type': 'application/json' }, | 
					
						
							|  |  |  |         mode: 'cors', | 
					
						
							|  |  |  |         body: JSON.stringify({ 'number': 1 }), | 
					
						
							|  |  |  |         credentials: 'include' | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return response.json(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error).toBeTruthy(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should support cors for different methods', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.route('**/cars', async (route, request) => { | 
					
						
							|  |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       contentType: 'application/json', | 
					
						
							|  |  |  |       headers: { 'Access-Control-Allow-Origin': '*' }, | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: JSON.stringify([request.method(), 'electric', 'gas']), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // First POST
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     const resp = await page.evaluate(async () => { | 
					
						
							|  |  |  |       const response = await fetch('https://example.com/cars', { | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         headers: { 'Content-Type': 'application/json' }, | 
					
						
							|  |  |  |         mode: 'cors', | 
					
						
							|  |  |  |         body: JSON.stringify({ 'number': 1 }) | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return response.json(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(resp).toEqual(['POST', 'electric', 'gas']); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // Then DELETE
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     const resp = await page.evaluate(async () => { | 
					
						
							|  |  |  |       const response = await fetch('https://example.com/cars', { | 
					
						
							|  |  |  |         method: 'DELETE', | 
					
						
							|  |  |  |         headers: {}, | 
					
						
							|  |  |  |         mode: 'cors', | 
					
						
							|  |  |  |         body: '' | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       return response.json(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(resp).toEqual(['DELETE', 'electric', 'gas']); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-08-24 20:45:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should support the times parameter with route matching', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2021-08-24 20:45:50 +02:00
										 |  |  |   const intercepted = []; | 
					
						
							|  |  |  |   await page.route('**/empty.html', route => { | 
					
						
							|  |  |  |     intercepted.push(1); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   }, { times: 1 }); | 
					
						
							| 
									
										
										
										
											2021-08-24 20:45:50 +02:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(intercepted).toHaveLength(1); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-10-04 15:10:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 17:52:16 -08:00
										 |  |  | it('should work if handler with times parameter was removed from another handler', async ({ page, server }) => { | 
					
						
							|  |  |  |   const intercepted = []; | 
					
						
							|  |  |  |   const handler = async route => { | 
					
						
							|  |  |  |     intercepted.push('first'); | 
					
						
							|  |  |  |     void route.continue(); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   await page.route('**/*', handler, { times: 1 }); | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     intercepted.push('second'); | 
					
						
							|  |  |  |     await page.unroute('**/*', handler); | 
					
						
							|  |  |  |     await route.fallback(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(intercepted).toEqual(['second']); | 
					
						
							|  |  |  |   intercepted.length = 0; | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(intercepted).toEqual(['second']); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:55:46 -07:00
										 |  |  | it('should support async handler w/ times', async ({ page, server }) => { | 
					
						
							|  |  |  |   await page.route('**/empty.html', async route => { | 
					
						
							|  |  |  |     await new Promise(f => setTimeout(f, 100)); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     await route.fulfill({ | 
					
						
							| 
									
										
										
										
											2022-05-21 21:55:46 -07:00
										 |  |  |       body: '<html>intercepted</html>', | 
					
						
							|  |  |  |       contentType: 'text/html' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }, { times: 1 }); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await expect(page.locator('body')).toHaveText('intercepted'); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   await expect(page.locator('body')).not.toHaveText('intercepted'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 08:35:54 -08:00
										 |  |  | it('should contain raw request header', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2021-10-04 15:10:16 -08:00
										 |  |  |   let headers: any; | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     headers = await route.request().allHeaders(); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2021-10-04 15:10:16 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   expect(headers.accept).toBeTruthy(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-10-06 08:35:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should contain raw response header', async ({ page, server }) => { | 
					
						
							|  |  |  |   let request: any; | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     request = route.request(); | 
					
						
							| 
									
										
										
										
											2023-06-02 21:59:12 +02:00
										 |  |  |     void route.continue(); | 
					
						
							| 
									
										
										
										
											2021-10-06 08:35:54 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   const response = await request.response(); | 
					
						
							|  |  |  |   const headers = await response.allHeaders(); | 
					
						
							|  |  |  |   expect(headers['content-type']).toBeTruthy(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | it('should contain raw response header after fulfill', async ({ page, server }) => { | 
					
						
							|  |  |  |   let request: any; | 
					
						
							|  |  |  |   await page.route('**/*', async route => { | 
					
						
							|  |  |  |     request = route.request(); | 
					
						
							|  |  |  |     await route.fulfill({ | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: 'Hello', | 
					
						
							|  |  |  |       contentType: 'text/html', | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/empty.html'); | 
					
						
							|  |  |  |   const response = await request.response(); | 
					
						
							|  |  |  |   const headers = await response.allHeaders(); | 
					
						
							|  |  |  |   expect(headers['content-type']).toBeTruthy(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 11:30:51 -08:00
										 |  |  | for (const method of ['fulfill', 'continue', 'fallback', 'abort'] as const) { | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  |   it(`route.${method} should throw if called twice`, async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2022-06-15 15:15:45 -08:00
										 |  |  |     let resolve; | 
					
						
							|  |  |  |     const resolvePromise = new Promise<Route>(f => resolve = f); | 
					
						
							|  |  |  |     await page.route('**/*', resolve); | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  |     page.goto(server.PREFIX + '/empty.html').catch(() => {}); | 
					
						
							| 
									
										
										
										
											2022-06-15 15:15:45 -08:00
										 |  |  |     const route = await resolvePromise; | 
					
						
							| 
									
										
										
										
											2022-01-27 14:58:02 -08:00
										 |  |  |     await route[method](); | 
					
						
							|  |  |  |     const e = await route[method]().catch(e => e); | 
					
						
							|  |  |  |     expect(e.message).toContain('Route is already handled!'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-13 03:54:47 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | it('should intercept when postData is more than 1MB', async ({ page, server }) => { | 
					
						
							|  |  |  |   it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/22753' }); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   let interceptionCallback; | 
					
						
							|  |  |  |   const interceptionPromise = new Promise(x => interceptionCallback = x); | 
					
						
							|  |  |  |   const POST_BODY = '0'.repeat(2 * 1024 * 1024); // 2MB
 | 
					
						
							|  |  |  |   await page.route('**/404.html', async route => { | 
					
						
							|  |  |  |     await route.abort(); | 
					
						
							|  |  |  |     interceptionCallback(route.request().postData()); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.evaluate(POST_BODY => fetch('/404.html', { | 
					
						
							|  |  |  |     method: 'POST', | 
					
						
							|  |  |  |     body: POST_BODY, | 
					
						
							|  |  |  |   }).catch(e => {}), POST_BODY); | 
					
						
							|  |  |  |   expect(await interceptionPromise).toBe(POST_BODY); | 
					
						
							|  |  |  | }); |