| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-26 10:59:27 -07:00
										 |  |  | import { it, expect } from './fixtures'; | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-22 15:13:51 -07:00
										 |  |  | import { globToRegex } from '../lib/client/clientHelper'; | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  | import vm from 'vm'; | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should work with navigation', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const requests = new Map(); | 
					
						
							|  |  |  |   await page.route('**/*', route => { | 
					
						
							|  |  |  |     requests.set(route.request().url().split('/').pop(), route.request()); | 
					
						
							|  |  |  |     route.continue(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   server.setRedirect('/rrredirect', '/frames/one-frame.html'); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/rrredirect'); | 
					
						
							|  |  |  |   expect(requests.get('rrredirect').isNavigationRequest()).toBe(true); | 
					
						
							|  |  |  |   expect(requests.get('frame.html').isNavigationRequest()).toBe(true); | 
					
						
							|  |  |  |   expect(requests.get('script.js').isNavigationRequest()).toBe(false); | 
					
						
							|  |  |  |   expect(requests.get('style.css').isNavigationRequest()).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should work with ignoreHTTPSErrors', async ({browser, httpsServer}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const context = await browser.newContext({ ignoreHTTPSErrors: true }); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.route('**/*', route => route.continue()); | 
					
						
							|  |  |  |   const response = await page.goto(httpsServer.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(response.status()).toBe(200); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should intercept after a service worker', async ({browser, page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   await page.goto(server.PREFIX + '/serviceworkers/fetchdummy/sw.html'); | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   await page.evaluate(() => window['activationPromise']); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Sanity check.
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   const swResponse = await page.evaluate(() => window['fetchDummy']('foo')); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   expect(swResponse).toBe('responseFromServiceWorker:foo'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.route('**/foo', route => { | 
					
						
							|  |  |  |     const slash = route.request().url().lastIndexOf('/'); | 
					
						
							|  |  |  |     const name = route.request().url().substring(slash + 1); | 
					
						
							|  |  |  |     route.fulfill({ | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       contentType: 'text/css', | 
					
						
							|  |  |  |       body: 'responseFromInterception:' + name | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page route is applied after service worker fetch event.
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   const swResponse2 = await page.evaluate(() => window['fetchDummy']('foo')); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   expect(swResponse2).toBe('responseFromServiceWorker:foo'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page route is not applied to service worker initiated fetch.
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |   const nonInterceptedResponse = await page.evaluate(() => window['fetchDummy']('passthrough')); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   expect(nonInterceptedResponse).toBe('FAILURE: Not Found'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should work with glob', async () => { | 
					
						
							| 
									
										
										
										
											2020-08-19 13:27:58 -07:00
										 |  |  |   expect(globToRegex('**/*.js').test('https://localhost:8080/foo.js')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('**/*.css').test('https://localhost:8080/foo.js')).toBeFalsy(); | 
					
						
							|  |  |  |   expect(globToRegex('*.js').test('https://localhost:8080/foo.js')).toBeFalsy(); | 
					
						
							|  |  |  |   expect(globToRegex('https://**/*.js').test('https://localhost:8080/foo.js')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('http://localhost:8080/simple/path.js').test('http://localhost:8080/simple/path.js')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('http://localhost:8080/?imple/path.js').test('http://localhost:8080/Simple/path.js')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('**/{a,b}.js').test('https://localhost:8080/a.js')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('**/{a,b}.js').test('https://localhost:8080/b.js')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('**/{a,b}.js').test('https://localhost:8080/c.js')).toBeFalsy(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 13:27:58 -07:00
										 |  |  |   expect(globToRegex('**/*.{png,jpg,jpeg}').test('https://localhost:8080/c.jpg')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('**/*.{png,jpg,jpeg}').test('https://localhost:8080/c.jpeg')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('**/*.{png,jpg,jpeg}').test('https://localhost:8080/c.png')).toBeTruthy(); | 
					
						
							|  |  |  |   expect(globToRegex('**/*.{png,jpg,jpeg}').test('https://localhost:8080/c.css')).toBeFalsy(); | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 15:04:57 -08:00
										 |  |  | it('should intercept network activity from worker', async function({page, server}) { | 
					
						
							| 
									
										
										
										
											2020-11-23 15:03:34 -08:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   server.setRoute('/data_for_worker', (req, res) => res.end('failed to intercept')); | 
					
						
							|  |  |  |   const url = server.PREFIX + '/data_for_worker'; | 
					
						
							| 
									
										
										
										
											2020-12-10 15:04:57 -08:00
										 |  |  |   await page.route(url, route => { | 
					
						
							|  |  |  |     route.fulfill({ | 
					
						
							| 
									
										
										
										
											2020-11-23 15:03:34 -08:00
										 |  |  |       status: 200, | 
					
						
							|  |  |  |       body: 'intercepted', | 
					
						
							| 
									
										
										
										
											2020-12-10 15:04:57 -08:00
										 |  |  |     }).catch(e => null); | 
					
						
							| 
									
										
										
										
											2020-11-23 15:03:34 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const [msg] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('console'), | 
					
						
							|  |  |  |     page.evaluate(url => new Worker(URL.createObjectURL(new Blob([`
 | 
					
						
							|  |  |  |       fetch("${url}").then(response => response.text()).then(console.log); | 
					
						
							|  |  |  |     `], {type: 'application/javascript'}))), url),
 | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(msg.text()).toBe('intercepted'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 15:04:57 -08:00
										 |  |  | it('should intercept network activity from worker', async function({page, server}) { | 
					
						
							|  |  |  |   const url = server.PREFIX + '/worker/worker.js'; | 
					
						
							|  |  |  |   await page.route(url, route => { | 
					
						
							|  |  |  |     route.fulfill({ | 
					
						
							|  |  |  |       status: 200, | 
					
						
							|  |  |  |       body: 'console.log("intercepted");', | 
					
						
							|  |  |  |       contentType: 'application/javascript', | 
					
						
							|  |  |  |     }).catch(e => null); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const [msg] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('console'), | 
					
						
							|  |  |  |     page.goto(server.PREFIX + '/worker/worker.html'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   expect(msg.text()).toBe('intercepted'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  | it('should work with regular expression passed from a different context', async ({page, server}) => { | 
					
						
							| 
									
										
										
										
											2020-08-04 16:32:10 -07:00
										 |  |  |   const ctx = vm.createContext(); | 
					
						
							|  |  |  |   const regexp = vm.runInContext('new RegExp("empty\\.html")', ctx); | 
					
						
							|  |  |  |   let intercepted = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await page.route(regexp, (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'); | 
					
						
							|  |  |  |     route.continue(); | 
					
						
							|  |  |  |     intercepted = true; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const response = await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   expect(response.ok()).toBe(true); | 
					
						
							|  |  |  |   expect(intercepted).toBe(true); | 
					
						
							|  |  |  | }); |