2021-07-02 17:58:32 +00:00
|
|
|
/**
|
|
|
|
* 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';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2021-07-02 17:58:32 +00:00
|
|
|
import expect from 'expect';
|
2022-06-15 10:09:22 +00:00
|
|
|
import {
|
|
|
|
ActionResult,
|
|
|
|
HTTPRequest,
|
|
|
|
InterceptResolutionAction,
|
2023-03-15 16:51:34 +00:00
|
|
|
} from 'puppeteer-core/internal/api/HTTPRequest.js';
|
|
|
|
import {ConsoleMessage} from 'puppeteer-core/internal/common/ConsoleMessage.js';
|
2021-07-02 17:58:32 +00:00
|
|
|
|
2023-07-03 12:01:29 +00:00
|
|
|
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
|
2023-04-25 13:02:25 +00:00
|
|
|
import {isFavicon, waitEvent} from './utils.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2021-07-02 17:58:32 +00:00
|
|
|
describe('request interception', function () {
|
2023-07-03 12:01:29 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.setRequestInterception', function () {
|
2021-07-02 17:58:32 +00:00
|
|
|
const expectedActions: ActionResult[] = ['abort', 'continue', 'respond'];
|
|
|
|
while (expectedActions.length > 0) {
|
|
|
|
const expectedAction = expectedActions.pop();
|
|
|
|
it(`should cooperatively ${expectedAction} by priority`, async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
const actionResults: ActionResult[] = [];
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.url().endsWith('.css')) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue(
|
2022-06-22 13:25:44 +00:00
|
|
|
{headers: {...request.headers(), xaction: 'continue'}},
|
2021-07-02 17:58:32 +00:00
|
|
|
expectedAction === 'continue' ? 1 : 0
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.url().endsWith('.css')) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.respond(
|
2022-06-22 13:25:44 +00:00
|
|
|
{headers: {xaction: 'respond'}},
|
2021-07-02 17:58:32 +00:00
|
|
|
expectedAction === 'respond' ? 1 : 0
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.url().endsWith('.css')) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.abort('aborted', expectedAction === 'abort' ? 1 : 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('response', response => {
|
|
|
|
const {xaction} = response!.headers();
|
2022-06-15 10:09:22 +00:00
|
|
|
if (response!.url().endsWith('.css') && !!xaction) {
|
2021-07-02 17:58:32 +00:00
|
|
|
actionResults.push(xaction as ActionResult);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestfailed', request => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.url().endsWith('.css')) {
|
|
|
|
actionResults.push('abort');
|
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await (async () => {
|
2021-07-02 17:58:32 +00:00
|
|
|
if (expectedAction === 'continue') {
|
|
|
|
const [serverRequest, response] = await Promise.all([
|
|
|
|
server.waitForRequest('/one-style.css'),
|
|
|
|
page.goto(server.PREFIX + '/one-style.html'),
|
|
|
|
]);
|
2022-06-15 10:09:22 +00:00
|
|
|
actionResults.push(
|
|
|
|
serverRequest.headers['xaction'] as ActionResult
|
|
|
|
);
|
2021-07-02 17:58:32 +00:00
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
return await page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
}
|
2022-06-15 10:09:22 +00:00
|
|
|
})())!;
|
2021-07-02 17:58:32 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(actionResults).toHaveLength(1);
|
|
|
|
expect(actionResults[0]).toBe(expectedAction);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.ok()).toBe(true);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should intercept', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-04-25 13:02:25 +00:00
|
|
|
if (isFavicon(request)) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
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);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(request.frame()!.url()).toBe('about:blank');
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
|
|
|
expect(response!.ok()).toBe(true);
|
|
|
|
expect(response!.remoteAddress().port).toBe(server.PORT);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
// @see https://github.com/puppeteer/puppeteer/pull/3105
|
|
|
|
it('should work when POST is redirected with 302', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
server.setRedirect('/rredirect', '/empty.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
await page.setContent(`
|
|
|
|
<form action='/rredirect' method='post'>
|
|
|
|
<input type="hidden" id="foo" name="foo" value="FOOBAR">
|
|
|
|
</form>
|
|
|
|
`);
|
|
|
|
await Promise.all([
|
2022-06-22 13:25:44 +00:00
|
|
|
page.$eval('form', form => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (form as HTMLFormElement).submit();
|
|
|
|
}),
|
2021-07-02 17:58:32 +00:00
|
|
|
page.waitForNavigation(),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/3973
|
|
|
|
it('should work when header manipulation headers with redirect', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
server.setRedirect('/rrredirect', '/empty.html');
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
const headers = Object.assign({}, request.headers(), {
|
|
|
|
foo: 'bar',
|
|
|
|
});
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({headers}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(request.continueRequestOverrides()).toEqual({headers});
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
// 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 () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
const headers = Object.assign({}, request.headers(), {
|
|
|
|
foo: 'bar',
|
|
|
|
origin: undefined, // remove "origin" header
|
|
|
|
});
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({headers}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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 () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-04-25 13:02:25 +00:00
|
|
|
if (!isFavicon(request)) {
|
2022-06-14 11:55:35 +00:00
|
|
|
requests.push(request);
|
|
|
|
}
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[1]!.url()).toContain('/one-style.css');
|
|
|
|
expect(requests[1]!.headers()['referer']).toContain('/one-style.html');
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should properly return navigation response when URL has cookies', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
// Setup cookie.
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setCookie({name: 'foo', value: 'bar'});
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
// Setup request interception.
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
const response = await page.reload();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should stop intercepting', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.once('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setRequestInterception(false);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
});
|
|
|
|
it('should show custom HTTP headers', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setExtraHTTPHeaders({
|
|
|
|
foo: 'bar',
|
|
|
|
});
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(request.headers()['foo']).toBe('bar');
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.ok()).toBe(true);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/4337
|
|
|
|
it('should work with redirect inside sync XHR', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
server.setRedirect('/logo.png', '/pptr.png');
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
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 () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setExtraHTTPHeaders({referer: server.EMPTY_PAGE});
|
2021-07-02 17:58:32 +00:00
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.ok()).toBe(true);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should be abortable', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.url().endsWith('.css')) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.abort('failed', 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
let failedRequests = 0;
|
2022-06-15 10:09:22 +00:00
|
|
|
page.on('requestfailed', () => {
|
|
|
|
return ++failedRequests;
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/one-style.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.ok()).toBe(true);
|
|
|
|
expect(response!.request().failure()).toBe(null);
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(failedRequests).toBe(1);
|
|
|
|
});
|
|
|
|
it('should be able to access the error reason', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.abort('failed', 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
let abortReason = null;
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
abortReason = request.abortErrorReason();
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE).catch(() => {});
|
|
|
|
expect(abortReason).toBe('Failed');
|
|
|
|
});
|
|
|
|
it('should be abortable with custom error codes', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.abort('internetdisconnected', 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2023-04-25 13:02:25 +00:00
|
|
|
|
|
|
|
const [failedRequest] = await Promise.all([
|
|
|
|
waitEvent<HTTPRequest>(page, 'requestfailed'),
|
|
|
|
page.goto(server.EMPTY_PAGE).catch(() => {}),
|
|
|
|
]);
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(failedRequest).toBeTruthy();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(failedRequest.failure()!.errorText).toBe(
|
2021-07-02 17:58:32 +00:00
|
|
|
'net::ERR_INTERNET_DISCONNECTED'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('should send referer', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setExtraHTTPHeaders({
|
|
|
|
referer: 'http://google.com/',
|
|
|
|
});
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
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 () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, isChrome} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.abort('failed', 0);
|
|
|
|
});
|
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE).catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(error).toBeTruthy();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
|
|
|
expect(error.message).toContain('net::ERR_FAILED');
|
|
|
|
} else {
|
|
|
|
expect(error.message).toContain('NS_ERROR_FAILURE');
|
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should work with redirects', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
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'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
|
|
|
expect(response!.url()).toContain('empty.html');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(5);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[2]!.resourceType()).toBe('document');
|
2021-07-02 17:58:32 +00:00
|
|
|
// Check redirect chain
|
2022-06-15 10:09:22 +00:00
|
|
|
const redirectChain = response!.request().redirectChain();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(redirectChain).toHaveLength(4);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(redirectChain[0]!.url()).toContain('/non-existing-page.html');
|
|
|
|
expect(redirectChain[2]!.url()).toContain('/non-existing-page-3.html');
|
2021-07-02 17:58:32 +00:00
|
|
|
for (let i = 0; i < redirectChain.length; ++i) {
|
2022-06-15 10:09:22 +00:00
|
|
|
const request = redirectChain[i]!;
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(request.isNavigationRequest()).toBe(true);
|
|
|
|
expect(request.redirectChain().indexOf(request)).toBe(i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('should work with redirects for subresources', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2023-04-25 13:02:25 +00:00
|
|
|
if (!isFavicon(request)) {
|
2022-06-14 11:55:35 +00:00
|
|
|
requests.push(request);
|
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
server.setRedirect('/one-style.css', '/two-style.css');
|
|
|
|
server.setRedirect('/two-style.css', '/three-style.css');
|
|
|
|
server.setRedirect('/three-style.css', '/four-style.css');
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/four-style.css', (_req, res) => {
|
|
|
|
return res.end('body {box-sizing: border-box; }');
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
const response = await page.goto(server.PREFIX + '/one-style.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
|
|
|
expect(response!.url()).toContain('one-style.html');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(5);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[0]!.resourceType()).toBe('document');
|
|
|
|
expect(requests[1]!.resourceType()).toBe('stylesheet');
|
2021-07-02 17:58:32 +00:00
|
|
|
// Check redirect chain
|
2022-06-15 10:09:22 +00:00
|
|
|
const redirectChain = requests[1]!.redirectChain();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(redirectChain).toHaveLength(3);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(redirectChain[0]!.url()).toContain('/one-style.css');
|
|
|
|
expect(redirectChain[2]!.url()).toContain('/three-style.css');
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should be able to abort redirects', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, isChrome} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
server.setRedirect('/non-existing.json', '/non-existing-2.json');
|
|
|
|
server.setRedirect('/non-existing-2.json', '/simple.html');
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.url().includes('non-existing-2')) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.abort('failed', 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const result = await page.evaluate(async () => {
|
|
|
|
try {
|
2022-06-15 10:09:22 +00:00
|
|
|
return await fetch('/non-existing.json');
|
2021-07-02 17:58:32 +00:00
|
|
|
} catch (error) {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error as Error).message;
|
2021-07-02 17:58:32 +00:00
|
|
|
}
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
|
|
|
expect(result).toContain('Failed to fetch');
|
|
|
|
} else {
|
|
|
|
expect(result).toContain('NetworkError');
|
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should work with equal requests', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let responseCount = 1;
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/zzz', (_req, res) => {
|
|
|
|
return res.end(responseCount++ * 11 + '');
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
await page.setRequestInterception(true);
|
|
|
|
|
|
|
|
let spinner = false;
|
|
|
|
// Cancel 2nd request.
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-04-25 13:02:25 +00:00
|
|
|
if (isFavicon(request)) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-08-28 11:01:52 +00:00
|
|
|
void (spinner ? request.abort('failed', 0) : request.continue({}, 0));
|
2021-07-02 17:58:32 +00:00
|
|
|
spinner = !spinner;
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const results = await page.evaluate(() => {
|
|
|
|
return Promise.all([
|
2021-07-02 17:58:32 +00:00
|
|
|
fetch('/zzz')
|
2022-06-22 13:25:44 +00:00
|
|
|
.then(response => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return response!.text();
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return 'FAILED';
|
|
|
|
}),
|
2021-07-02 17:58:32 +00:00
|
|
|
fetch('/zzz')
|
2022-06-22 13:25:44 +00:00
|
|
|
.then(response => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return response!.text();
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return 'FAILED';
|
|
|
|
}),
|
2021-07-02 17:58:32 +00:00
|
|
|
fetch('/zzz')
|
2022-06-22 13:25:44 +00:00
|
|
|
.then(response => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return response!.text();
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return 'FAILED';
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(results).toEqual(['11', 'FAILED', '22']);
|
|
|
|
});
|
|
|
|
it('should navigate to dataURL and fire dataURL requests', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
requests.push(request);
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const dataURL = 'data:text/html,<div>yo</div>';
|
|
|
|
const response = await page.goto(dataURL);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[0]!.url()).toBe(dataURL);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should be able to fetch dataURL and fire dataURL requests', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-04-25 13:02:25 +00:00
|
|
|
!isFavicon(request) && requests.push(request);
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const dataURL = 'data:text/html,<div>yo</div>';
|
2022-06-15 10:09:22 +00:00
|
|
|
const text = await page.evaluate((url: string) => {
|
2022-06-22 13:25:44 +00:00
|
|
|
return fetch(url).then(r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return r.text();
|
|
|
|
});
|
|
|
|
}, dataURL);
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(text).toBe('<div>yo</div>');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[0]!.url()).toBe(dataURL);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should navigate to URL with hash and fire requests without hash', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
requests.push(request);
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE + '#hash');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
|
|
|
expect(response!.url()).toBe(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[0]!.url()).toBe(server.EMPTY_PAGE);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should work with encoded server', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
// The requestWillBeSent will report encoded URL, whereas interception will
|
|
|
|
// report URL as-is. @see crbug.com/759388
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
const response = await page.goto(
|
|
|
|
server.PREFIX + '/some nonexisting page'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(404);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should work with badly encoded server', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/malformed?rnd=%911', (_req, res) => {
|
|
|
|
return res.end();
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/malformed?rnd=%911');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should work with encoded server - 2', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
// The requestWillBeSent will report URL as-is, whereas interception will
|
|
|
|
// report encoded URL for stylesheet. @see crbug.com/759388
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
requests.push(request);
|
|
|
|
});
|
|
|
|
const response = await page.goto(
|
|
|
|
`data:text/html,<link rel="stylesheet" href="${server.PREFIX}/fonts?helvetica|arial"/>`
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(2);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[1]!.response()!.status()).toBe(404);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should not throw "Invalid Interception Id" if the request was cancelled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setContent('<iframe></iframe>');
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-15 10:09:22 +00:00
|
|
|
let request!: HTTPRequest;
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', async r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (request = r);
|
|
|
|
});
|
2023-08-28 11:01:52 +00:00
|
|
|
void (page.$eval(
|
2021-07-02 17:58:32 +00:00
|
|
|
'iframe',
|
2022-06-15 10:09:22 +00:00
|
|
|
(frame, url) => {
|
|
|
|
return ((frame as HTMLIFrameElement).src = url as string);
|
|
|
|
},
|
2021-07-02 17:58:32 +00:00
|
|
|
server.EMPTY_PAGE
|
|
|
|
),
|
2023-08-28 11:01:52 +00:00
|
|
|
// Wait for request interception.
|
|
|
|
await waitEvent(page, 'request'));
|
2021-07-02 17:58:32 +00:00
|
|
|
// Delete frame to cause request to be canceled.
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.$eval('iframe', frame => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return frame.remove();
|
|
|
|
});
|
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await request.continue({}, 0).catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error).toBeUndefined();
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should throw if interception is not enabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', async request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
try {
|
|
|
|
await request.continue({}, 0);
|
|
|
|
} catch (error_) {
|
2022-06-15 10:09:22 +00:00
|
|
|
error = error_ as Error;
|
2021-07-02 17:58:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(error.message).toContain('Request Interception is not enabled');
|
|
|
|
});
|
|
|
|
it('should work with file URLs', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
const urls = new Set();
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
urls.add(request.url().split('/').pop());
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
await page.goto(
|
2022-06-15 10:05:25 +00:00
|
|
|
pathToFileURL(path.join(__dirname, '../assets', 'one-style.html'))
|
2021-07-02 17:58:32 +00:00
|
|
|
);
|
|
|
|
expect(urls.size).toBe(2);
|
|
|
|
expect(urls.has('one-style.html')).toBe(true);
|
|
|
|
expect(urls.has('one-style.css')).toBe(true);
|
|
|
|
});
|
|
|
|
it('should not cache if cache disabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
// Load and re-load to make sure it's cached.
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
await page.setCacheEnabled(false);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const cached: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestservedfromcache', r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return cached.push(r);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.reload();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(cached).toHaveLength(0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should cache if cache enabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
// Load and re-load to make sure it's cached.
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
await page.setCacheEnabled(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const cached: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestservedfromcache', r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return cached.push(r);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.reload();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(cached).toHaveLength(1);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should load fonts if cache enabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
await page.setCacheEnabled(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style-font.html');
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.waitForResponse(r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return r.url().endsWith('/one-style.woff');
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Request.continue', function () {
|
2021-07-02 17:58:32 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue({}, 0);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
});
|
|
|
|
it('should amend HTTP headers', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
const headers = Object.assign({}, request.headers());
|
|
|
|
headers['FOO'] = 'bar';
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({headers}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/sleep.zzz'),
|
2022-06-15 10:09:22 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
return fetch('/sleep.zzz');
|
|
|
|
}),
|
2021-07-02 17:58:32 +00:00
|
|
|
]);
|
|
|
|
expect(request.headers['foo']).toBe('bar');
|
|
|
|
});
|
|
|
|
it('should redirect in a way non-observable to page', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
const redirectURL = request.url().includes('/empty.html')
|
|
|
|
? server.PREFIX + '/consolelog.html'
|
|
|
|
: undefined;
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({url: redirectURL}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2023-04-25 13:02:25 +00:00
|
|
|
|
|
|
|
const [consoleMessage] = await Promise.all([
|
|
|
|
waitEvent<ConsoleMessage>(page, 'console'),
|
|
|
|
page.goto(server.EMPTY_PAGE),
|
|
|
|
]);
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(consoleMessage.text()).toBe('yellow');
|
|
|
|
});
|
|
|
|
it('should amend method', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({method: 'POST'}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/sleep.zzz'),
|
2022-06-15 10:09:22 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
return fetch('/sleep.zzz');
|
|
|
|
}),
|
2021-07-02 17:58:32 +00:00
|
|
|
]);
|
|
|
|
expect(request.method).toBe('POST');
|
|
|
|
});
|
|
|
|
it('should amend post data', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({postData: 'doggo'}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/sleep.zzz'),
|
2022-06-15 10:09:22 +00:00
|
|
|
page.evaluate(() => {
|
2022-06-22 13:25:44 +00:00
|
|
|
return fetch('/sleep.zzz', {method: 'POST', body: 'birdy'});
|
2022-06-15 10:09:22 +00:00
|
|
|
}),
|
2021-07-02 17:58:32 +00:00
|
|
|
]);
|
|
|
|
expect(await serverRequest.postBody).toBe('doggo');
|
|
|
|
});
|
|
|
|
it('should amend both post data and method on navigation', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({method: 'POST', postData: 'doggo'}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
page.goto(server.EMPTY_PAGE),
|
|
|
|
]);
|
|
|
|
expect(serverRequest.method).toBe('POST');
|
|
|
|
expect(await serverRequest.postBody).toBe('doggo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Request.respond', function () {
|
2021-07-02 17:58:32 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.respond(
|
2021-07-02 17:58:32 +00:00
|
|
|
{
|
|
|
|
status: 201,
|
|
|
|
headers: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
body: 'Yo, page!',
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(201);
|
|
|
|
expect(response!.headers()['foo']).toBe('bar');
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.body.textContent;
|
|
|
|
})
|
|
|
|
).toBe('Yo, page!');
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should be able to access the response', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.respond(
|
2021-07-02 17:58:32 +00:00
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
body: 'Yo, page!',
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
let response = null;
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
response = request.responseForRequest();
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(response).toEqual({status: 200, body: 'Yo, page!'});
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should work with status code 422', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.respond(
|
2021-07-02 17:58:32 +00:00
|
|
|
{
|
|
|
|
status: 422,
|
|
|
|
body: 'Yo, page!',
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(422);
|
|
|
|
expect(response!.statusText()).toBe('Unprocessable Entity');
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.body.textContent;
|
|
|
|
})
|
|
|
|
).toBe('Yo, page!');
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should redirect', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
if (!request.url().includes('rrredirect')) {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue({}, 0);
|
2021-07-02 17:58:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.respond(
|
2021-07-02 17:58:32 +00:00
|
|
|
{
|
|
|
|
status: 302,
|
|
|
|
headers: {
|
|
|
|
location: server.EMPTY_PAGE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/rrredirect');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(response!.request().redirectChain()).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.request().redirectChain()[0]!.url()).toBe(
|
2021-07-02 17:58:32 +00:00
|
|
|
server.PREFIX + '/rrredirect'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.url()).toBe(server.EMPTY_PAGE);
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
it('should allow mocking binary responses', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-07-02 17:58:32 +00:00
|
|
|
const imageBuffer = fs.readFileSync(
|
2022-06-15 10:05:25 +00:00
|
|
|
path.join(__dirname, '../assets', 'pptr.png')
|
2021-07-02 17:58:32 +00:00
|
|
|
);
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.respond(
|
2021-07-02 17:58:32 +00:00
|
|
|
{
|
|
|
|
contentType: 'image/png',
|
|
|
|
body: imageBuffer,
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.evaluate(PREFIX => {
|
2021-07-02 17:58:32 +00:00
|
|
|
const img = document.createElement('img');
|
|
|
|
img.src = PREFIX + '/does-not-exist.png';
|
|
|
|
document.body.appendChild(img);
|
2022-06-22 13:25:44 +00:00
|
|
|
return new Promise(fulfill => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (img.onload = fulfill);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
}, server.PREFIX);
|
2023-08-30 10:02:59 +00:00
|
|
|
using img = (await page.$('img'))!;
|
2021-07-02 17:58:32 +00:00
|
|
|
expect(await img.screenshot()).toBeGolden('mock-binary-response.png');
|
|
|
|
});
|
|
|
|
it('should stringify intercepted request response headers', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-07-02 17:58:32 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.respond(
|
2021-07-02 17:58:32 +00:00
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
foo: true,
|
|
|
|
},
|
|
|
|
body: 'Yo, page!',
|
|
|
|
},
|
|
|
|
0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(response!.status()).toBe(200);
|
|
|
|
const headers = response!.headers();
|
|
|
|
expect(headers['foo']).toBe('true');
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.body.textContent;
|
|
|
|
})
|
|
|
|
).toBe('Yo, page!');
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
2021-12-09 14:57:40 +00:00
|
|
|
it('should indicate already-handled if an intercept has been handled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-12-07 07:48:42 +00:00
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue();
|
2021-12-07 07:48:42 +00:00
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2021-12-07 07:48:42 +00:00
|
|
|
expect(request.isInterceptResolutionHandled()).toBeTruthy();
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
|
|
|
const {action} = request.interceptResolutionState();
|
2021-12-09 14:57:40 +00:00
|
|
|
expect(action).toBe(InterceptResolutionAction.AlreadyHandled);
|
2021-12-07 07:48:42 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
});
|
2021-07-02 17:58:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function pathToFileURL(path: string): string {
|
|
|
|
let pathName = path.replace(/\\/g, '/');
|
|
|
|
// Windows drive letter must be prefixed with a slash.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!pathName.startsWith('/')) {
|
|
|
|
pathName = '/' + pathName;
|
|
|
|
}
|
2021-07-02 17:58:32 +00:00
|
|
|
return 'file://' + pathName;
|
|
|
|
}
|