2018-03-20 03:00:12 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import fs from 'fs';
|
2023-02-15 23:09:31 +00:00
|
|
|
import {ServerResponse} from 'http';
|
2020-06-23 05:18:46 +00:00
|
|
|
import path from 'path';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2023-03-15 16:51:34 +00:00
|
|
|
import {HTTPRequest} from 'puppeteer-core/internal/api/HTTPRequest.js';
|
|
|
|
import {HTTPResponse} from 'puppeteer-core/internal/api/HTTPResponse.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-07-03 12:01:29 +00:00
|
|
|
import {getTestState, launch, setupTestBrowserHooks} from './mocha-utils.js';
|
2023-04-25 13:02:25 +00:00
|
|
|
import {attachFrame, isFavicon, waitEvent} from './utils.js';
|
2018-03-20 03:00:12 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('network', function () {
|
2023-07-03 12:01:29 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.Events.Request', function () {
|
|
|
|
it('should fire for navigation requests', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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-04 07:37:15 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(1);
|
2019-02-13 03:10:14 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should fire for iframes', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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-04 07:37:15 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(2);
|
2019-02-13 03:10:14 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fire for fetches', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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-04 07:37:15 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return fetch('/empty.html');
|
|
|
|
});
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(2);
|
2019-02-13 03:10:14 +00:00
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Request.frame', function () {
|
|
|
|
it('should work for main frame navigation request', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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-04 07:37:15 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(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]!.frame()).toBe(page.mainFrame());
|
2019-02-13 03:10:14 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work for subframe navigation request', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
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-04 07:37:15 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(1);
|
|
|
|
expect(requests[0]!.frame()).toBe(page.frames()[1]);
|
2019-02-13 03:10:14 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for fetch requests', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
let requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2023-04-04 07:37:15 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return fetch('/digits/1.png');
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
requests = requests.filter(request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return !request.url().includes('favicon');
|
|
|
|
});
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(requests[0]!.frame()).toBe(page.mainFrame());
|
2019-02-13 03:10:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Request.headers', function () {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should define Chrome as user agent header', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2022-03-04 06:50:09 +00:00
|
|
|
expect(response.request().headers()['user-agent']).toContain('Chrome');
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should define Firefox as user agent header', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2022-03-04 06:50:09 +00:00
|
|
|
expect(response.request().headers()['user-agent']).toContain('Firefox');
|
2019-02-13 08:55:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Response.headers', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/empty.html', (_req, res) => {
|
2019-02-13 08:55:56 +00:00
|
|
|
res.setHeader('foo', 'bar');
|
|
|
|
res.end();
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2019-02-13 08:55:56 +00:00
|
|
|
expect(response.headers()['foo']).toBe('bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Request.initiator', () => {
|
2022-04-19 13:24:53 +00:00
|
|
|
it('should return the initiator', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-09-29 15:14:21 +00:00
|
|
|
|
|
|
|
const initiators = new Map();
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return initiators.set(
|
|
|
|
request.url().split('/').pop(),
|
|
|
|
request.initiator()
|
|
|
|
);
|
|
|
|
});
|
2021-09-29 15:14:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/initiator.html');
|
|
|
|
|
|
|
|
expect(initiators.get('initiator.html').type).toBe('other');
|
|
|
|
expect(initiators.get('initiator.js').type).toBe('parser');
|
|
|
|
expect(initiators.get('initiator.js').url).toBe(
|
|
|
|
server.PREFIX + '/initiator.html'
|
|
|
|
);
|
|
|
|
expect(initiators.get('frame.html').type).toBe('parser');
|
|
|
|
expect(initiators.get('frame.html').url).toBe(
|
|
|
|
server.PREFIX + '/initiator.html'
|
|
|
|
);
|
|
|
|
expect(initiators.get('script.js').type).toBe('parser');
|
|
|
|
expect(initiators.get('script.js').url).toBe(
|
|
|
|
server.PREFIX + '/frames/frame.html'
|
|
|
|
);
|
|
|
|
expect(initiators.get('style.css').type).toBe('parser');
|
|
|
|
expect(initiators.get('style.css').url).toBe(
|
|
|
|
server.PREFIX + '/frames/frame.html'
|
|
|
|
);
|
|
|
|
expect(initiators.get('initiator.js').type).toBe('parser');
|
|
|
|
expect(initiators.get('injectedfile.js').type).toBe('script');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(initiators.get('injectedfile.js').stack.callFrames[0]!.url).toBe(
|
2021-09-29 15:14:21 +00:00
|
|
|
server.PREFIX + '/initiator.js'
|
|
|
|
);
|
|
|
|
expect(initiators.get('injectedstyle.css').type).toBe('script');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(initiators.get('injectedstyle.css').stack.callFrames[0]!.url).toBe(
|
2021-09-29 15:14:21 +00:00
|
|
|
server.PREFIX + '/initiator.js'
|
|
|
|
);
|
|
|
|
expect(initiators.get('initiator.js').url).toBe(
|
|
|
|
server.PREFIX + '/initiator.html'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Response.fromCache', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return |false| for non-cached content', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2019-02-13 03:59:54 +00:00
|
|
|
expect(response.fromCache()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:59:54 +00:00
|
|
|
const responses = new Map();
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('response', r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (
|
2023-04-04 07:37:15 +00:00
|
|
|
!isFavicon(r.request()) && responses.set(r.url().split('/').pop(), r)
|
2022-06-15 10:09:22 +00:00
|
|
|
);
|
|
|
|
});
|
2019-02-13 03:59:54 +00:00
|
|
|
|
|
|
|
// Load and re-load to make sure it's cached.
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
|
|
|
await page.reload();
|
|
|
|
|
|
|
|
expect(responses.size).toBe(2);
|
|
|
|
expect(responses.get('one-style.css').status()).toBe(200);
|
|
|
|
expect(responses.get('one-style.css').fromCache()).toBe(true);
|
|
|
|
expect(responses.get('one-style.html').status()).toBe(304);
|
|
|
|
expect(responses.get('one-style.html').fromCache()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Response.fromServiceWorker', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return |false| for non-service-worker content', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2019-02-13 03:59:54 +00:00
|
|
|
expect(response.fromServiceWorker()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Response.fromServiceWorker', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:59:54 +00:00
|
|
|
const responses = new Map();
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('response', r => {
|
2023-04-04 07:37:15 +00:00
|
|
|
return !isFavicon(r) && responses.set(r.url().split('/').pop(), r);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2019-02-13 03:59:54 +00:00
|
|
|
|
|
|
|
// Load and re-load to make sure serviceworker is installed and running.
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/fetch/sw.html', {
|
|
|
|
waitUntil: 'networkidle2',
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(async () => {
|
2023-09-01 07:49:33 +00:00
|
|
|
return (globalThis as any).activationPromise;
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2019-02-13 03:59:54 +00:00
|
|
|
await page.reload();
|
|
|
|
|
|
|
|
expect(responses.size).toBe(2);
|
|
|
|
expect(responses.get('sw.html').status()).toBe(200);
|
|
|
|
expect(responses.get('sw.html').fromServiceWorker()).toBe(true);
|
|
|
|
expect(responses.get('style.css').status()).toBe(200);
|
|
|
|
expect(responses.get('style.css').fromServiceWorker()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Request.postData', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/post', (_req, res) => {
|
|
|
|
return res.end();
|
|
|
|
});
|
2023-04-25 13:02:25 +00:00
|
|
|
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
waitEvent<HTTPRequest>(page, 'request', r => {
|
|
|
|
return !isFavicon(r);
|
|
|
|
}),
|
|
|
|
page.evaluate(() => {
|
|
|
|
return fetch('./post', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({foo: 'bar'}),
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
2019-02-13 21:11:50 +00:00
|
|
|
expect(request).toBeTruthy();
|
|
|
|
expect(request.postData()).toBe('{"foo":"bar"}');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be |undefined| when there is no post data', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2019-02-13 21:11:50 +00:00
|
|
|
expect(response.request().postData()).toBe(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Response.text', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/simple.json'))!;
|
2020-04-17 13:27:50 +00:00
|
|
|
const responseText = (await response.text()).trimEnd();
|
|
|
|
expect(responseText).toBe('{"foo": "bar"}');
|
2018-09-05 20:03:24 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return uncompressed text', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
server.enableGzip('/simple.json');
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/simple.json'))!;
|
2019-02-25 03:31:35 +00:00
|
|
|
expect(response.headers()['content-encoding']).toBe('gzip');
|
2020-04-17 13:27:50 +00:00
|
|
|
const responseText = (await response.text()).trimEnd();
|
|
|
|
expect(responseText).toBe('{"foo": "bar"}');
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw when requesting body of redirected response', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 03:22:18 +00:00
|
|
|
server.setRedirect('/foo.html', '/empty.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/foo.html'))!;
|
2018-04-11 03:22:18 +00:00
|
|
|
const redirectChain = response.request().redirectChain();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(redirectChain).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
const redirected = redirectChain[0]!.response()!;
|
2018-04-11 03:22:18 +00:00
|
|
|
expect(redirected.status()).toBe(302);
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await redirected.text().catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toContain(
|
|
|
|
'Response body is unavailable for redirect responses'
|
|
|
|
);
|
2018-04-11 03:22:18 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should wait until response completes', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2018-03-20 03:00:12 +00:00
|
|
|
// Setup server to trap request.
|
2022-06-15 10:09:22 +00:00
|
|
|
let serverResponse!: ServerResponse;
|
|
|
|
server.setRoute('/get', (_req, res) => {
|
2018-03-20 03:00:12 +00:00
|
|
|
serverResponse = res;
|
2019-02-25 03:31:35 +00:00
|
|
|
// In Firefox, |fetch| will be hanging until it receives |Content-Type| header
|
|
|
|
// from server.
|
|
|
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
2018-03-20 03:00:12 +00:00
|
|
|
res.write('hello ');
|
|
|
|
});
|
|
|
|
// Setup page to trap response.
|
|
|
|
let requestFinished = false;
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestfinished', r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (requestFinished = requestFinished || r.url().includes('/get'));
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
// send request and wait for server response
|
2019-02-25 03:31:35 +00:00
|
|
|
const [pageResponse] = await Promise.all([
|
2022-06-22 13:25:44 +00:00
|
|
|
page.waitForResponse(r => {
|
2023-04-04 07:37:15 +00:00
|
|
|
return !isFavicon(r.request());
|
2022-06-15 10:09:22 +00:00
|
|
|
}),
|
|
|
|
page.evaluate(() => {
|
2022-06-22 13:25:44 +00:00
|
|
|
return fetch('./get', {method: 'GET'});
|
2022-06-15 10:09:22 +00:00
|
|
|
}),
|
2019-02-25 03:31:35 +00:00
|
|
|
server.waitForRequest('/get'),
|
2018-03-20 03:00:12 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
expect(serverResponse).toBeTruthy();
|
|
|
|
expect(pageResponse).toBeTruthy();
|
|
|
|
expect(pageResponse.status()).toBe(200);
|
|
|
|
expect(requestFinished).toBe(false);
|
|
|
|
|
|
|
|
const responseText = pageResponse.text();
|
|
|
|
// Write part of the response and wait for it to be flushed.
|
2022-06-22 13:25:44 +00:00
|
|
|
await new Promise(x => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return serverResponse.write('wor', x);
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
// Finish response.
|
2022-06-22 13:25:44 +00:00
|
|
|
await new Promise<void>(x => {
|
2022-06-15 10:09:22 +00:00
|
|
|
serverResponse.end('ld!', () => {
|
|
|
|
return x();
|
|
|
|
});
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(await responseText).toBe('hello world!');
|
|
|
|
});
|
2019-02-25 03:31:35 +00:00
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Response.json', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/simple.json'))!;
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(await response.json()).toEqual({foo: 'bar'});
|
2019-02-25 03:31:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Response.buffer', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/pptr.png'))!;
|
2020-05-07 10:54:55 +00:00
|
|
|
const imageBuffer = fs.readFileSync(
|
2022-06-15 10:05:25 +00:00
|
|
|
path.join(__dirname, '../assets', 'pptr.png')
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2019-02-25 03:31:35 +00:00
|
|
|
const responseBuffer = await response.buffer();
|
|
|
|
expect(responseBuffer.equals(imageBuffer)).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with compression', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
server.enableGzip('/pptr.png');
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/pptr.png'))!;
|
2020-05-07 10:54:55 +00:00
|
|
|
const imageBuffer = fs.readFileSync(
|
2022-06-15 10:05:25 +00:00
|
|
|
path.join(__dirname, '../assets', 'pptr.png')
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2019-02-25 03:31:35 +00:00
|
|
|
const responseBuffer = await response.buffer();
|
|
|
|
expect(responseBuffer.equals(imageBuffer)).toBe(true);
|
|
|
|
});
|
2021-10-29 13:44:40 +00:00
|
|
|
it('should throw if the response does not have a body', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-10-29 13:44:40 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/empty.html');
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/test.html', (_req, res) => {
|
2021-10-29 13:44:40 +00:00
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', 'x-ping');
|
|
|
|
res.end('Hello World');
|
|
|
|
});
|
|
|
|
const url = server.CROSS_PROCESS_PREFIX + '/test.html';
|
2023-04-25 13:02:25 +00:00
|
|
|
const responsePromise = waitEvent<HTTPResponse>(
|
|
|
|
page,
|
|
|
|
'response',
|
|
|
|
response => {
|
2021-10-29 13:44:40 +00:00
|
|
|
// Get the preflight response.
|
2023-04-25 13:02:25 +00:00
|
|
|
return (
|
|
|
|
response.request().method() === 'OPTIONS' && response.url() === url
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2021-10-29 13:44:40 +00:00
|
|
|
|
|
|
|
// Trigger a request with a preflight.
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
await page.evaluate(async src => {
|
2021-10-29 13:44:40 +00:00
|
|
|
const response = await fetch(src, {
|
|
|
|
method: 'POST',
|
2022-06-22 13:25:44 +00:00
|
|
|
headers: {'x-ping': 'pong'},
|
2021-10-29 13:44:40 +00:00
|
|
|
});
|
|
|
|
return response;
|
|
|
|
}, url);
|
|
|
|
|
|
|
|
const response = await responsePromise;
|
|
|
|
await expect(response.buffer()).rejects.toThrowError(
|
|
|
|
'Could not load body for this request. This might happen if the request is a preflight request.'
|
|
|
|
);
|
|
|
|
});
|
2019-02-25 03:31:35 +00:00
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Response.statusText', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/cool', (_req, res) => {
|
2019-02-27 00:24:30 +00:00
|
|
|
res.writeHead(200, 'cool!');
|
|
|
|
res.end();
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/cool'))!;
|
2019-02-27 00:24:30 +00:00
|
|
|
expect(response.statusText()).toBe('cool!');
|
|
|
|
});
|
2021-11-26 08:17:34 +00:00
|
|
|
|
|
|
|
it('handles missing status text', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-11-26 08:17:34 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/nostatus', (_req, res) => {
|
2021-11-26 08:17:34 +00:00
|
|
|
res.writeHead(200, '');
|
|
|
|
res.end();
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.PREFIX + '/nostatus'))!;
|
2021-11-26 08:17:34 +00:00
|
|
|
expect(response.statusText()).toBe('');
|
|
|
|
});
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Response.timing', function () {
|
2022-02-15 10:16:49 +00:00
|
|
|
it('returns timing information', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2022-06-15 10:09:22 +00:00
|
|
|
const responses: HTTPResponse[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('response', response => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return responses.push(response);
|
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(responses).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(responses[0]!.timing()!.receiveHeadersEnd).toBeGreaterThan(0);
|
2022-02-15 10:16:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Network Events', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Page.Events.Request', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return requests.push(request);
|
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(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);
|
|
|
|
expect(requests[0]!.resourceType()).toBe('document');
|
|
|
|
expect(requests[0]!.method()).toBe('GET');
|
|
|
|
expect(requests[0]!.response()).toBeTruthy();
|
|
|
|
expect(requests[0]!.frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(requests[0]!.frame()!.url()).toBe(server.EMPTY_PAGE);
|
2019-02-25 03:31:35 +00:00
|
|
|
});
|
2021-03-17 14:42:35 +00:00
|
|
|
it('Page.Events.RequestServedFromCache', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-03-17 14:42:35 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const cached: string[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestservedfromcache', r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return cached.push(r.url().split('/').pop()!);
|
|
|
|
});
|
2021-03-17 14:42:35 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
|
|
|
expect(cached).toEqual([]);
|
|
|
|
|
|
|
|
await page.reload();
|
|
|
|
expect(cached).toEqual(['one-style.css']);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Page.Events.Response', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const responses: HTTPResponse[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('response', response => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return responses.push(response);
|
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(responses).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(responses[0]!.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(responses[0]!.status()).toBe(200);
|
|
|
|
expect(responses[0]!.ok()).toBe(true);
|
|
|
|
expect(responses[0]!.request()).toBeTruthy();
|
|
|
|
const remoteAddress = responses[0]!.remoteAddress();
|
2019-02-25 03:31:35 +00:00
|
|
|
// Either IPv6 or IPv4, depending on environment.
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
remoteAddress.ip!.includes('::1') || remoteAddress.ip === '127.0.0.1'
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(true);
|
2019-02-25 03:31:35 +00:00
|
|
|
expect(remoteAddress.port).toBe(server.PORT);
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Page.Events.RequestFailed', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, isChrome} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +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();
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const failedRequests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestfailed', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return failedRequests.push(request);
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(failedRequests).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(failedRequests[0]!.url()).toContain('one-style.css');
|
|
|
|
expect(failedRequests[0]!.response()).toBe(null);
|
|
|
|
expect(failedRequests[0]!.resourceType()).toBe('stylesheet');
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(failedRequests[0]!.failure()!.errorText).toBe('net::ERR_FAILED');
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(failedRequests[0]!.failure()!.errorText).toBe(
|
|
|
|
'NS_ERROR_FAILURE'
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(failedRequests[0]!.frame()).toBeTruthy();
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Page.Events.RequestFinished', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const requests: HTTPRequest[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestfinished', request => {
|
2023-05-24 09:56:24 +00:00
|
|
|
return !isFavicon(request) && requests.push(request);
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(requests).toHaveLength(1);
|
2023-05-22 12:52:31 +00:00
|
|
|
const request = requests[0]!;
|
|
|
|
expect(request.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(request.response()).toBeTruthy();
|
|
|
|
expect(request.frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(request.frame()!.url()).toBe(server.EMPTY_PAGE);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fire events in proper order', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const events: string[] = [];
|
|
|
|
page.on('request', () => {
|
|
|
|
return events.push('request');
|
|
|
|
});
|
|
|
|
page.on('response', () => {
|
|
|
|
return events.push('response');
|
|
|
|
});
|
|
|
|
page.on('requestfinished', () => {
|
|
|
|
return events.push('requestfinished');
|
|
|
|
});
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-05-24 09:56:24 +00:00
|
|
|
// Events can sneak in after the page has navigate
|
|
|
|
expect(events.slice(0, 3)).toEqual([
|
|
|
|
'request',
|
|
|
|
'response',
|
|
|
|
'requestfinished',
|
|
|
|
]);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should support redirects', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const events: string[] = [];
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return events.push(`${request.method()} ${request.url()}`);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('response', response => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return events.push(`${response.status()} ${response.url()}`);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestfinished', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return events.push(`DONE ${request.url()}`);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('requestfailed', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return events.push(`FAIL ${request.url()}`);
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
server.setRedirect('/foo.html', '/empty.html');
|
|
|
|
const FOO_URL = server.PREFIX + '/foo.html';
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(FOO_URL))!;
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(events).toEqual([
|
|
|
|
`GET ${FOO_URL}`,
|
|
|
|
`302 ${FOO_URL}`,
|
|
|
|
`DONE ${FOO_URL}`,
|
|
|
|
`GET ${server.EMPTY_PAGE}`,
|
|
|
|
`200 ${server.EMPTY_PAGE}`,
|
2020-05-07 10:54:55 +00:00
|
|
|
`DONE ${server.EMPTY_PAGE}`,
|
2018-03-20 03:00:12 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Check redirect chain
|
|
|
|
const redirectChain = response.request().redirectChain();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(redirectChain).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(redirectChain[0]!.url()).toContain('/foo.html');
|
|
|
|
expect(redirectChain[0]!.response()!.remoteAddress().port).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
server.PORT
|
|
|
|
);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
});
|
2018-04-11 22:25:42 +00:00
|
|
|
|
2019-02-13 01:38:48 +00:00
|
|
|
describe('Request.isNavigationRequest', () => {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-06-01 00:38:30 +00:00
|
|
|
const requests = new Map();
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return requests.set(request.url().split('/').pop(), request);
|
|
|
|
});
|
2018-06-01 00:38:30 +00:00
|
|
|
server.setRedirect('/rrredirect', '/frames/one-frame.html');
|
|
|
|
await page.goto(server.PREFIX + '/rrredirect');
|
|
|
|
expect(requests.get('rrredirect').isNavigationRequest()).toBe(true);
|
|
|
|
expect(requests.get('one-frame.html').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);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with request interception', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-06-01 00:38:30 +00:00
|
|
|
const requests = new Map();
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2018-06-01 00:38:30 +00:00
|
|
|
requests.set(request.url().split('/').pop(), request);
|
2023-08-28 11:01:52 +00:00
|
|
|
void request.continue();
|
2018-06-01 00:38:30 +00:00
|
|
|
});
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
server.setRedirect('/rrredirect', '/frames/one-frame.html');
|
|
|
|
await page.goto(server.PREFIX + '/rrredirect');
|
|
|
|
expect(requests.get('rrredirect').isNavigationRequest()).toBe(true);
|
|
|
|
expect(requests.get('one-frame.html').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);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work when navigating to image', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-05-22 12:52:31 +00:00
|
|
|
const [request] = await Promise.all([
|
|
|
|
waitEvent<HTTPRequest>(page, 'request'),
|
|
|
|
page.goto(server.PREFIX + '/pptr.png'),
|
|
|
|
]);
|
|
|
|
expect(request.isNavigationRequest()).toBe(true);
|
2018-06-01 00:38:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.setExtraHTTPHeaders', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 22:25:42 +00:00
|
|
|
await page.setExtraHTTPHeaders({
|
2020-05-07 10:54:55 +00:00
|
|
|
foo: 'bar',
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
page.goto(server.EMPTY_PAGE),
|
|
|
|
]);
|
|
|
|
expect(request.headers['foo']).toBe('bar');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw for non-string header values', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2018-04-11 22:25:42 +00:00
|
|
|
try {
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error purposeful bad input
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setExtraHTTPHeaders({foo: 1});
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
2022-06-15 10:09:22 +00:00
|
|
|
error = error_ as Error;
|
2018-04-11 22:25:42 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toBe(
|
|
|
|
'Expected value of header "foo" to be String, but "number" is found.'
|
|
|
|
);
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.authenticate', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 22:25:42 +00:00
|
|
|
server.setAuth('/empty.html', 'user', 'pass');
|
2022-05-02 09:37:21 +00:00
|
|
|
let response;
|
|
|
|
try {
|
2022-06-15 10:09:22 +00:00
|
|
|
response = (await page.goto(server.EMPTY_PAGE))!;
|
2022-05-02 09:37:21 +00:00
|
|
|
expect(response.status()).toBe(401);
|
|
|
|
} catch (error) {
|
|
|
|
// In headful, an error is thrown instead of 401.
|
2022-06-15 10:09:22 +00:00
|
|
|
if (
|
|
|
|
!(error as Error).message.startsWith(
|
|
|
|
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
|
|
|
)
|
|
|
|
) {
|
2022-05-02 09:37:21 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 22:25:42 +00:00
|
|
|
await page.authenticate({
|
|
|
|
username: 'user',
|
2020-05-07 10:54:55 +00:00
|
|
|
password: 'pass',
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
response = (await page.reload())!;
|
2018-04-11 22:25:42 +00:00
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail if wrong credentials', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 22:25:42 +00:00
|
|
|
// Use unique user/password since Chrome caches credentials per origin.
|
|
|
|
server.setAuth('/empty.html', 'user2', 'pass2');
|
|
|
|
await page.authenticate({
|
|
|
|
username: 'foo',
|
2020-05-07 10:54:55 +00:00
|
|
|
password: 'bar',
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2018-04-11 22:25:42 +00:00
|
|
|
expect(response.status()).toBe(401);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should allow disable authentication', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 22:25:42 +00:00
|
|
|
// Use unique user/password since Chrome caches credentials per origin.
|
|
|
|
server.setAuth('/empty.html', 'user3', 'pass3');
|
|
|
|
await page.authenticate({
|
|
|
|
username: 'user3',
|
2020-05-07 10:54:55 +00:00
|
|
|
password: 'pass3',
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
let response = (await page.goto(server.EMPTY_PAGE))!;
|
2018-04-11 22:25:42 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.authenticate({
|
|
|
|
username: '',
|
|
|
|
password: '',
|
|
|
|
});
|
2018-04-11 22:25:42 +00:00
|
|
|
// Navigate to a different origin to bust Chrome's credential caching.
|
2022-05-02 09:37:21 +00:00
|
|
|
try {
|
2022-06-15 10:09:22 +00:00
|
|
|
response = (await page.goto(
|
|
|
|
server.CROSS_PROCESS_PREFIX + '/empty.html'
|
|
|
|
))!;
|
2022-05-02 09:37:21 +00:00
|
|
|
expect(response.status()).toBe(401);
|
|
|
|
} catch (error) {
|
|
|
|
// In headful, an error is thrown instead of 401.
|
2022-06-15 10:09:22 +00:00
|
|
|
if (
|
|
|
|
!(error as Error).message.startsWith(
|
|
|
|
'net::ERR_INVALID_AUTH_CREDENTIALS'
|
|
|
|
)
|
|
|
|
) {
|
2022-05-02 09:37:21 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
2021-03-15 07:02:07 +00:00
|
|
|
it('should not disable caching', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-03-15 07:02:07 +00:00
|
|
|
|
|
|
|
// Use unique user/password since Chrome caches credentials per origin.
|
|
|
|
server.setAuth('/cached/one-style.css', 'user4', 'pass4');
|
|
|
|
server.setAuth('/cached/one-style.html', 'user4', 'pass4');
|
|
|
|
await page.authenticate({
|
|
|
|
username: 'user4',
|
|
|
|
password: 'pass4',
|
|
|
|
});
|
|
|
|
|
|
|
|
const responses = new Map();
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('response', r => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return responses.set(r.url().split('/').pop(), r);
|
|
|
|
});
|
2021-03-15 07:02:07 +00:00
|
|
|
|
|
|
|
// Load and re-load to make sure it's cached.
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
|
|
|
await page.reload();
|
|
|
|
|
|
|
|
expect(responses.get('one-style.css').status()).toBe(200);
|
|
|
|
expect(responses.get('one-style.css').fromCache()).toBe(true);
|
|
|
|
expect(responses.get('one-style.html').status()).toBe(304);
|
|
|
|
expect(responses.get('one-style.html').fromCache()).toBe(false);
|
|
|
|
});
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
2021-11-23 07:19:14 +00:00
|
|
|
|
2023-05-24 12:08:36 +00:00
|
|
|
describe('raw network headers', () => {
|
2021-11-23 07:19:14 +00:00
|
|
|
it('Same-origin set-cookie navigation', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-11-23 07:19:14 +00:00
|
|
|
|
|
|
|
const setCookieString = 'foo=bar';
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/empty.html', (_req, res) => {
|
2021-11-23 07:19:14 +00:00
|
|
|
res.setHeader('set-cookie', setCookieString);
|
|
|
|
res.end('hello world');
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2021-11-23 07:19:14 +00:00
|
|
|
expect(response.headers()['set-cookie']).toBe(setCookieString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Same-origin set-cookie subresource', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2023-04-04 07:37:15 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2021-11-23 07:19:14 +00:00
|
|
|
|
|
|
|
const setCookieString = 'foo=bar';
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/foo', (_req, res) => {
|
2021-11-23 07:19:14 +00:00
|
|
|
res.setHeader('set-cookie', setCookieString);
|
|
|
|
res.end('hello world');
|
|
|
|
});
|
|
|
|
|
2023-05-22 12:52:31 +00:00
|
|
|
const [response] = await Promise.all([
|
2023-06-07 08:18:01 +00:00
|
|
|
waitEvent<HTTPResponse>(page, 'response', res => {
|
|
|
|
return !isFavicon(res);
|
|
|
|
}),
|
2023-05-22 12:52:31 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', '/foo');
|
|
|
|
xhr.send();
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
expect(response.headers()['set-cookie']).toBe(setCookieString);
|
2021-11-23 07:19:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Cross-origin set-cookie', async () => {
|
2023-05-11 13:10:27 +00:00
|
|
|
const {page, httpsServer, close} = await launch({
|
2021-11-23 07:19:14 +00:00
|
|
|
ignoreHTTPSErrors: true,
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await page.goto(httpsServer.PREFIX + '/empty.html');
|
|
|
|
|
|
|
|
const setCookieString = 'hello=world';
|
2022-06-15 10:09:22 +00:00
|
|
|
httpsServer.setRoute('/setcookie.html', (_req, res) => {
|
2021-11-23 07:19:14 +00:00
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
res.setHeader('set-cookie', setCookieString);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await page.goto(httpsServer.PREFIX + '/setcookie.html');
|
2022-08-31 08:50:22 +00:00
|
|
|
const url = httpsServer.CROSS_PROCESS_PREFIX + '/setcookie.html';
|
2023-04-25 13:02:25 +00:00
|
|
|
const [response] = await Promise.all([
|
|
|
|
waitEvent<HTTPResponse>(page, 'response', response => {
|
|
|
|
return response.url() === url;
|
|
|
|
}),
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
page.evaluate(src => {
|
2021-11-23 07:19:14 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', src);
|
|
|
|
xhr.send();
|
2023-04-25 13:02:25 +00:00
|
|
|
}, url),
|
|
|
|
]);
|
2021-11-23 07:19:14 +00:00
|
|
|
expect(response.headers()['set-cookie']).toBe(setCookieString);
|
|
|
|
} finally {
|
2023-05-11 13:10:27 +00:00
|
|
|
await close();
|
2021-11-23 07:19:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-05-23 11:51:32 +00:00
|
|
|
|
2023-05-24 12:08:36 +00:00
|
|
|
describe('Page.setBypassServiceWorker', () => {
|
2023-05-23 11:51:32 +00:00
|
|
|
it('bypass for network', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2023-05-23 11:51:32 +00:00
|
|
|
|
|
|
|
const responses = new Map();
|
|
|
|
page.on('response', r => {
|
|
|
|
return !isFavicon(r) && responses.set(r.url().split('/').pop(), r);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load and re-load to make sure serviceworker is installed and running.
|
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/fetch/sw.html', {
|
|
|
|
waitUntil: 'networkidle2',
|
|
|
|
});
|
|
|
|
await page.evaluate(async () => {
|
2023-09-01 07:49:33 +00:00
|
|
|
return (globalThis as any).activationPromise;
|
2023-05-23 11:51:32 +00:00
|
|
|
});
|
|
|
|
await page.reload({
|
|
|
|
waitUntil: 'networkidle2',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(page.isServiceWorkerBypassed()).toBe(false);
|
|
|
|
expect(responses.size).toBe(2);
|
|
|
|
expect(responses.get('sw.html').status()).toBe(200);
|
|
|
|
expect(responses.get('sw.html').fromServiceWorker()).toBe(true);
|
|
|
|
expect(responses.get('style.css').status()).toBe(200);
|
|
|
|
expect(responses.get('style.css').fromServiceWorker()).toBe(true);
|
|
|
|
|
|
|
|
await page.setBypassServiceWorker(true);
|
|
|
|
await page.reload({
|
|
|
|
waitUntil: 'networkidle2',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(page.isServiceWorkerBypassed()).toBe(true);
|
|
|
|
expect(responses.get('sw.html').status()).toBe(200);
|
|
|
|
expect(responses.get('sw.html').fromServiceWorker()).toBe(false);
|
|
|
|
expect(responses.get('style.css').status()).toBe(200);
|
|
|
|
expect(responses.get('style.css').fromServiceWorker()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|