/** * Copyright 2021 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import fs from 'fs'; import path from 'path'; import utils from './utils.js'; import expect from 'expect'; import { getTestState, setupTestBrowserHooks, setupTestPageAndContextHooks, describeFailsFirefox, } from './mocha-utils'; // eslint-disable-line import/extensions import { ActionResult } from '../lib/cjs/puppeteer/api-docs-entry.js'; describe('request interception', function () { setupTestBrowserHooks(); setupTestPageAndContextHooks(); describeFailsFirefox('Page.setRequestInterception', function () { const expectedActions: ActionResult[] = ['abort', 'continue', 'respond']; while (expectedActions.length > 0) { const expectedAction = expectedActions.pop(); it(`should cooperatively ${expectedAction} by priority`, async () => { const { page, server } = getTestState(); const actionResults: ActionResult[] = []; await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('.css')) request.continue( { headers: { ...request.headers(), xaction: 'continue' } }, expectedAction === 'continue' ? 1 : 0 ); else request.continue({}, 0); }); page.on('request', (request) => { if (request.url().endsWith('.css')) request.respond( { headers: { xaction: 'respond' } }, expectedAction === 'respond' ? 1 : 0 ); else request.continue({}, 0); }); page.on('request', (request) => { if (request.url().endsWith('.css')) request.abort('aborted', expectedAction === 'abort' ? 1 : 0); else request.continue({}, 0); }); page.on('response', (response) => { const { xaction } = response.headers(); if (response.url().endsWith('.css') && !!xaction) actionResults.push(xaction as ActionResult); }); page.on('requestfailed', (request) => { if (request.url().endsWith('.css')) actionResults.push('abort'); }); const response = await (async () => { if (expectedAction === 'continue') { const [serverRequest, response] = await Promise.all([ server.waitForRequest('/one-style.css'), page.goto(server.PREFIX + '/one-style.html'), ]); actionResults.push(serverRequest.headers.xaction as ActionResult); return response; } else { return await page.goto(server.PREFIX + '/one-style.html'); } })(); expect(actionResults.length).toBe(1); expect(actionResults[0]).toBe(expectedAction); expect(response.ok()).toBe(true); }); } it('should intercept', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (utils.isFavicon(request)) { request.continue({}, 0); return; } expect(request.url()).toContain('empty.html'); expect(request.headers()['user-agent']).toBeTruthy(); expect(request.method()).toBe('GET'); expect(request.postData()).toBe(undefined); expect(request.isNavigationRequest()).toBe(true); expect(request.resourceType()).toBe('document'); expect(request.frame() === page.mainFrame()).toBe(true); expect(request.frame().url()).toBe('about:blank'); request.continue({}, 0); }); const response = await page.goto(server.EMPTY_PAGE); expect(response.ok()).toBe(true); expect(response.remoteAddress().port).toBe(server.PORT); }); // @see https://github.com/puppeteer/puppeteer/pull/3105 it('should work when POST is redirected with 302', async () => { const { page, server } = getTestState(); server.setRedirect('/rredirect', '/empty.html'); await page.goto(server.EMPTY_PAGE); await page.setRequestInterception(true); page.on('request', (request) => request.continue({}, 0)); await page.setContent(`
`); await Promise.all([ page.$eval('form', (form: HTMLFormElement) => form.submit()), page.waitForNavigation(), ]); }); // @see https://github.com/puppeteer/puppeteer/issues/3973 it('should work when header manipulation headers with redirect', async () => { const { page, server } = getTestState(); server.setRedirect('/rrredirect', '/empty.html'); await page.setRequestInterception(true); page.on('request', (request) => { const headers = Object.assign({}, request.headers(), { foo: 'bar', }); request.continue({ headers }, 0); expect(request.continueRequestOverrides()).toEqual({ headers }); }); // Make sure that the goto does not time out. await page.goto(server.PREFIX + '/rrredirect'); }); // @see https://github.com/puppeteer/puppeteer/issues/4743 it('should be able to remove headers', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { const headers = Object.assign({}, request.headers(), { foo: 'bar', origin: undefined, // remove "origin" header }); request.continue({ headers }, 0); }); const [serverRequest] = await Promise.all([ server.waitForRequest('/empty.html'), page.goto(server.PREFIX + '/empty.html'), ]); expect(serverRequest.headers.origin).toBe(undefined); }); it('should contain referer header', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); const requests = []; page.on('request', (request) => { if (!utils.isFavicon(request)) requests.push(request); request.continue({}, 0); }); 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'); }); it('should properly return navigation response when URL has cookies', async () => { const { page, server } = getTestState(); // Setup cookie. await page.goto(server.EMPTY_PAGE); await page.setCookie({ name: 'foo', value: 'bar' }); // Setup request interception. await page.setRequestInterception(true); page.on('request', (request) => request.continue({}, 0)); const response = await page.reload(); expect(response.status()).toBe(200); }); it('should stop intercepting', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); page.once('request', (request) => request.continue({}, 0)); await page.goto(server.EMPTY_PAGE); await page.setRequestInterception(false); await page.goto(server.EMPTY_PAGE); }); it('should show custom HTTP headers', async () => { const { page, server } = getTestState(); await page.setExtraHTTPHeaders({ foo: 'bar', }); await page.setRequestInterception(true); page.on('request', (request) => { expect(request.headers()['foo']).toBe('bar'); request.continue({}, 0); }); const response = await page.goto(server.EMPTY_PAGE); expect(response.ok()).toBe(true); }); // @see https://github.com/puppeteer/puppeteer/issues/4337 it('should work with redirect inside sync XHR', async () => { const { page, server } = getTestState(); await page.goto(server.EMPTY_PAGE); server.setRedirect('/logo.png', '/pptr.png'); await page.setRequestInterception(true); page.on('request', (request) => request.continue({}, 0)); const status = await page.evaluate(async () => { 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); }); it('should work with custom referer headers', async () => { const { page, server } = getTestState(); await page.setExtraHTTPHeaders({ referer: server.EMPTY_PAGE }); await page.setRequestInterception(true); page.on('request', (request) => { expect(request.headers()['referer']).toBe(server.EMPTY_PAGE); request.continue({}, 0); }); const response = await page.goto(server.EMPTY_PAGE); expect(response.ok()).toBe(true); }); it('should be abortable', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('.css')) request.abort('failed', 0); else request.continue({}, 0); }); let failedRequests = 0; page.on('requestfailed', () => ++failedRequests); const response = await page.goto(server.PREFIX + '/one-style.html'); expect(response.ok()).toBe(true); expect(response.request().failure()).toBe(null); expect(failedRequests).toBe(1); }); it('should be able to access the error reason', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { request.abort('failed', 0); }); let abortReason = null; page.on('request', (request) => { abortReason = request.abortErrorReason(); request.continue({}, 0); }); await page.goto(server.EMPTY_PAGE).catch(() => {}); expect(abortReason).toBe('Failed'); }); it('should be abortable with custom error codes', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { request.abort('internetdisconnected', 0); }); let failedRequest = null; page.on('requestfailed', (request) => (failedRequest = request)); await page.goto(server.EMPTY_PAGE).catch(() => {}); expect(failedRequest).toBeTruthy(); expect(failedRequest.failure().errorText).toBe( 'net::ERR_INTERNET_DISCONNECTED' ); }); it('should send referer', async () => { const { page, server } = getTestState(); await page.setExtraHTTPHeaders({ referer: 'http://google.com/', }); await page.setRequestInterception(true); page.on('request', (request) => request.continue({}, 0)); const [request] = await Promise.all([ server.waitForRequest('/grid.html'), page.goto(server.PREFIX + '/grid.html'), ]); expect(request.headers['referer']).toBe('http://google.com/'); }); it('should fail navigation when aborting main resource', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => request.abort('failed', 0)); let error = null; await page.goto(server.EMPTY_PAGE).catch((error_) => (error = error_)); expect(error).toBeTruthy(); if (isChrome) expect(error.message).toContain('net::ERR_FAILED'); else expect(error.message).toContain('NS_ERROR_FAILURE'); }); it('should work with redirects', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); const requests = []; page.on('request', (request) => { request.continue({}, 0); requests.push(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(requests.length).toBe(5); expect(requests[2].resourceType()).toBe('document'); // Check redirect chain const redirectChain = response.request().redirectChain(); expect(redirectChain.length).toBe(4); expect(redirectChain[0].url()).toContain('/non-existing-page.html'); expect(redirectChain[2].url()).toContain('/non-existing-page-3.html'); for (let i = 0; i < redirectChain.length; ++i) { const request = redirectChain[i]; expect(request.isNavigationRequest()).toBe(true); expect(request.redirectChain().indexOf(request)).toBe(i); } }); it('should work with redirects for subresources', async () => { const { page, server } = getTestState(); await page.setRequestInterception(true); const requests = []; page.on('request', (request) => { request.continue({}, 0); if (!utils.isFavicon(request)) requests.push(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(requests.length).toBe(5); expect(requests[0].resourceType()).toBe('document'); expect(requests[1].resourceType()).toBe('stylesheet'); // Check redirect chain const redirectChain = requests[1].redirectChain(); expect(redirectChain.length).toBe(3); expect(redirectChain[0].url()).toContain('/one-style.css'); expect(redirectChain[2].url()).toContain('/three-style.css'); }); it('should be able to abort redirects', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); server.setRedirect('/non-existing.json', '/non-existing-2.json'); server.setRedirect('/non-existing-2.json', '/simple.html'); page.on('request', (request) => { if (request.url().includes('non-existing-2')) request.abort('failed', 0); else request.continue({}, 0); }); await page.goto(server.EMPTY_PAGE); const result = await page.evaluate(async () => { try { await fetch('/non-existing.json'); } catch (error) { return error.message; } }); if (isChrome) expect(result).toContain('Failed to fetch'); else expect(result).toContain('NetworkError'); }); it('should work with equal requests', async () => { const { page, server } = getTestState(); await page.goto(server.EMPTY_PAGE); let responseCount = 1; server.setRoute('/zzz', (req, res) => res.end(responseCount++ * 11 + '')); await page.setRequestInterception(true); let spinner = false; // Cancel 2nd request. page.on('request', (request) => { if (utils.isFavicon(request)) { request.continue({}, 0); return; } spinner ? request.abort('failed', 0) : request.continue({}, 0); spinner = !spinner; }); const results = await page.evaluate(() => Promise.all([ fetch('/zzz') .then((response) => response.text()) .catch(() => 'FAILED'), fetch('/zzz') .then((response) => response.text()) .catch(() => 'FAILED'), fetch('/zzz') .then((response) => response.text()) .catch(() => 'FAILED'), ]) ); expect(results).toEqual(['11', 'FAILED', '22']); }); it('should navigate to dataURL and fire dataURL requests', async () => { const { page } = getTestState(); await page.setRequestInterception(true); const requests = []; page.on('request', (request) => { requests.push(request); request.continue({}, 0); }); const dataURL = 'data:text/html,