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';
|
|
|
|
import path from 'path';
|
2020-07-13 09:22:26 +00:00
|
|
|
import utils from './utils.js';
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2020-06-23 05:18:46 +00:00
|
|
|
itFailsFirefox,
|
|
|
|
describeFailsFirefox,
|
2020-07-13 09:22:26 +00:00
|
|
|
} from './mocha-utils'; // eslint-disable-line import/extensions
|
2021-10-29 13:44:40 +00:00
|
|
|
import { HTTPResponse } from '../lib/cjs/puppeteer/api-docs-entry.js';
|
2018-03-20 03:00:12 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('network', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2018-03-20 03:00:12 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.Events.Request', function () {
|
|
|
|
it('should fire for navigation requests', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:10:14 +00:00
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'request',
|
|
|
|
(request) => !utils.isFavicon(request) && requests.push(request)
|
|
|
|
);
|
2019-02-13 03:10:14 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should fire for iframes', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:10:14 +00:00
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'request',
|
|
|
|
(request) => !utils.isFavicon(request) && requests.push(request)
|
|
|
|
);
|
2019-02-13 03:10:14 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(2);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fire for fetches', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:10:14 +00:00
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'request',
|
|
|
|
(request) => !utils.isFavicon(request) && requests.push(request)
|
|
|
|
);
|
2019-02-13 03:10:14 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2019-03-08 16:59:07 +00:00
|
|
|
await page.evaluate(() => fetch('/empty.html'));
|
2019-02-13 03:10:14 +00:00
|
|
|
expect(requests.length).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Request.frame', function () {
|
|
|
|
it('should work for main frame navigation request', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:10:14 +00:00
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'request',
|
|
|
|
(request) => !utils.isFavicon(request) && requests.push(request)
|
|
|
|
);
|
2019-02-13 03:10:14 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0].frame()).toBe(page.mainFrame());
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should work for subframe navigation request', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:10:14 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'request',
|
|
|
|
(request) => !utils.isFavicon(request) && requests.push(request)
|
|
|
|
);
|
2019-02-13 03:10:14 +00:00
|
|
|
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0].frame()).toBe(page.frames()[1]);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for fetch requests', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:10:14 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'request',
|
|
|
|
(request) => !utils.isFavicon(request) && requests.push(request)
|
|
|
|
);
|
2019-02-13 03:10:14 +00:00
|
|
|
await page.evaluate(() => fetch('/digits/1.png'));
|
2020-05-07 10:54:55 +00:00
|
|
|
requests = requests.filter(
|
|
|
|
(request) => !request.url().includes('favicon')
|
|
|
|
);
|
2019-02-13 03:10:14 +00:00
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0].frame()).toBe(page.mainFrame());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Request.headers', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server, isChrome } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 08:55:56 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2020-04-09 05:56:25 +00:00
|
|
|
if (isChrome)
|
2019-02-13 08:55:56 +00:00
|
|
|
expect(response.request().headers()['user-agent']).toContain('Chrome');
|
|
|
|
else
|
|
|
|
expect(response.request().headers()['user-agent']).toContain('Firefox');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Response.headers', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 08:55:56 +00:00
|
|
|
server.setRoute('/empty.html', (req, res) => {
|
|
|
|
res.setHeader('foo', 'bar');
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.headers()['foo']).toBe('bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-29 15:14:21 +00:00
|
|
|
describeFailsFirefox('Request.initiator', () => {
|
|
|
|
it('shoud return the initiator', async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
const initiators = new Map();
|
|
|
|
page.on('request', (request) =>
|
|
|
|
initiators.set(request.url().split('/').pop(), request.initiator())
|
|
|
|
);
|
|
|
|
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');
|
|
|
|
expect(initiators.get('injectedfile.js').stack.callFrames[0].url).toBe(
|
|
|
|
server.PREFIX + '/initiator.js'
|
|
|
|
);
|
|
|
|
expect(initiators.get('injectedstyle.css').type).toBe('script');
|
|
|
|
expect(initiators.get('injectedstyle.css').stack.callFrames[0].url).toBe(
|
|
|
|
server.PREFIX + '/initiator.js'
|
|
|
|
);
|
|
|
|
expect(initiators.get('initiator.js').url).toBe(
|
|
|
|
server.PREFIX + '/initiator.html'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Response.fromCache', function () {
|
|
|
|
it('should return |false| for non-cached content', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:59:54 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.fromCache()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:59:54 +00:00
|
|
|
const responses = new Map();
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'response',
|
|
|
|
(r) =>
|
|
|
|
!utils.isFavicon(r.request()) &&
|
|
|
|
responses.set(r.url().split('/').pop(), r)
|
|
|
|
);
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Response.fromServiceWorker', function () {
|
|
|
|
it('should return |false| for non-service-worker content', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:59:54 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.fromServiceWorker()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Response.fromServiceWorker', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 03:59:54 +00:00
|
|
|
const responses = new Map();
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('response', (r) => responses.set(r.url().split('/').pop(), r));
|
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',
|
|
|
|
});
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.evaluate(async () => await globalThis.activationPromise);
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Request.postData', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 21:11:50 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
server.setRoute('/post', (req, res) => res.end());
|
|
|
|
let request = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('request', (r) => (request = r));
|
|
|
|
await page.evaluate(() =>
|
|
|
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-13 21:11:50 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.request().postData()).toBe(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Response.text', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
server.enableGzip('/simple.json');
|
|
|
|
const response = await page.goto(server.PREFIX + '/simple.json');
|
|
|
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 03:22:18 +00:00
|
|
|
server.setRedirect('/foo.html', '/empty.html');
|
|
|
|
const response = await page.goto(server.PREFIX + '/foo.html');
|
|
|
|
const redirectChain = response.request().redirectChain();
|
|
|
|
expect(redirectChain.length).toBe(1);
|
|
|
|
const redirected = redirectChain[0].response();
|
|
|
|
expect(redirected.status()).toBe(302);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await redirected.text().catch((error_) => (error = error_));
|
|
|
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
// Setup server to trap request.
|
|
|
|
let serverResponse = null;
|
|
|
|
server.setRoute('/get', (req, res) => {
|
|
|
|
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;
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on(
|
|
|
|
'requestfinished',
|
|
|
|
(r) => (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([
|
2020-05-07 10:54:55 +00:00
|
|
|
page.waitForResponse((r) => !utils.isFavicon(r.request())),
|
|
|
|
page.evaluate(() => fetch('./get', { method: 'GET' })),
|
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.
|
2020-05-07 10:54:55 +00:00
|
|
|
await new Promise((x) => serverResponse.write('wor', x));
|
2018-03-20 03:00:12 +00:00
|
|
|
// Finish response.
|
2020-05-07 10:54:55 +00:00
|
|
|
await new Promise((x) => serverResponse.end('ld!', x));
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(await responseText).toBe('hello world!');
|
|
|
|
});
|
2019-02-25 03:31:35 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Response.json', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/simple.json');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await response.json()).toEqual({ foo: 'bar' });
|
2019-02-25 03:31:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Response.buffer', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/pptr.png');
|
2020-05-07 10:54:55 +00:00
|
|
|
const imageBuffer = fs.readFileSync(
|
|
|
|
path.join(__dirname, 'assets', 'pptr.png')
|
|
|
|
);
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
server.enableGzip('/pptr.png');
|
|
|
|
const response = await page.goto(server.PREFIX + '/pptr.png');
|
2020-05-07 10:54:55 +00:00
|
|
|
const imageBuffer = fs.readFileSync(
|
|
|
|
path.join(__dirname, 'assets', 'pptr.png')
|
|
|
|
);
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/empty.html');
|
|
|
|
|
|
|
|
server.setRoute('/test.html', (req, res) => {
|
|
|
|
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';
|
|
|
|
const responsePromise = new Promise<HTTPResponse>((resolve) => {
|
|
|
|
page.on('response', (response) => {
|
|
|
|
// Get the preflight response.
|
|
|
|
if (
|
|
|
|
response.request().method() === 'OPTIONS' &&
|
|
|
|
response.url() === url
|
|
|
|
) {
|
|
|
|
resolve(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Trigger a request with a preflight.
|
|
|
|
await page.evaluate<(src: string) => void>(async (src) => {
|
|
|
|
const response = await fetch(src, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { 'x-ping': 'pong' },
|
|
|
|
});
|
|
|
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-27 00:24:30 +00:00
|
|
|
server.setRoute('/cool', (req, res) => {
|
|
|
|
res.writeHead(200, 'cool!');
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/cool');
|
|
|
|
expect(response.statusText()).toBe('cool!');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Network Events', function () {
|
|
|
|
it('Page.Events.Request', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('request', (request) => requests.push(request));
|
2019-02-25 03:31:35 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
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);
|
|
|
|
});
|
2021-03-17 14:42:35 +00:00
|
|
|
it('Page.Events.RequestServedFromCache', async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
const cached = [];
|
|
|
|
page.on('requestservedfromcache', (r) =>
|
|
|
|
cached.push(r.url().split('/').pop())
|
|
|
|
);
|
|
|
|
|
|
|
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
const responses = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('response', (response) => responses.push(response));
|
2019-02-25 03:31:35 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(responses.length).toBe(1);
|
|
|
|
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();
|
|
|
|
// Either IPv6 or IPv4, depending on environment.
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
remoteAddress.ip.includes('::1') || remoteAddress.ip === '127.0.0.1'
|
|
|
|
).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 () => {
|
|
|
|
const { page, server, isChrome } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.setRequestInterception(true);
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('request', (request) => {
|
|
|
|
if (request.url().endsWith('css')) request.abort();
|
|
|
|
else request.continue();
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
const failedRequests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('requestfailed', (request) => failedRequests.push(request));
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
expect(failedRequests.length).toBe(1);
|
|
|
|
expect(failedRequests[0].url()).toContain('one-style.css');
|
|
|
|
expect(failedRequests[0].response()).toBe(null);
|
|
|
|
expect(failedRequests[0].resourceType()).toBe('stylesheet');
|
2020-04-09 05:56:25 +00:00
|
|
|
if (isChrome)
|
2019-02-19 22:51:56 +00:00
|
|
|
expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED');
|
|
|
|
else
|
|
|
|
expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE');
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(failedRequests[0].frame()).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Page.Events.RequestFinished', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('requestfinished', (request) => requests.push(request));
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(requests[0].response()).toBeTruthy();
|
|
|
|
expect(requests[0].frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fire events in proper order', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const events = [];
|
2020-06-23 05:18:46 +00:00
|
|
|
page.on('request', () => events.push('request'));
|
|
|
|
page.on('response', () => events.push('response'));
|
|
|
|
page.on('requestfinished', () => events.push('requestfinished'));
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(events).toEqual(['request', 'response', 'requestfinished']);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should support redirects', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const events = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('request', (request) =>
|
|
|
|
events.push(`${request.method()} ${request.url()}`)
|
|
|
|
);
|
|
|
|
page.on('response', (response) =>
|
|
|
|
events.push(`${response.status()} ${response.url()}`)
|
|
|
|
);
|
|
|
|
page.on('requestfinished', (request) =>
|
|
|
|
events.push(`DONE ${request.url()}`)
|
|
|
|
);
|
|
|
|
page.on('requestfailed', (request) =>
|
|
|
|
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';
|
|
|
|
const response = await page.goto(FOO_URL);
|
|
|
|
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();
|
|
|
|
expect(redirectChain.length).toBe(1);
|
|
|
|
expect(redirectChain[0].url()).toContain('/foo.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(redirectChain[0].response().remoteAddress().port).toBe(
|
|
|
|
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', () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-06-01 00:38:30 +00:00
|
|
|
const requests = new Map();
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('request', (request) =>
|
|
|
|
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);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should work with request interception', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-06-01 00:38:30 +00:00
|
|
|
const requests = new Map();
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('request', (request) => {
|
2018-06-01 00:38:30 +00:00
|
|
|
requests.set(request.url().split('/').pop(), request);
|
|
|
|
request.continue();
|
|
|
|
});
|
|
|
|
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);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work when navigating to image', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-06-01 00:38:30 +00:00
|
|
|
const requests = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('request', (request) => requests.push(request));
|
2018-06-01 00:38:30 +00:00
|
|
|
await page.goto(server.PREFIX + '/pptr.png');
|
|
|
|
expect(requests[0].isNavigationRequest()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.setExtraHTTPHeaders', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = 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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 22:25:42 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error purposeful bad input
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setExtraHTTPHeaders({ foo: 1 });
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
|
|
|
error = 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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.authenticate', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-11 22:25:42 +00:00
|
|
|
server.setAuth('/empty.html', 'user', 'pass');
|
|
|
|
let response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.status()).toBe(401);
|
|
|
|
await page.authenticate({
|
|
|
|
username: 'user',
|
2020-05-07 10:54:55 +00:00
|
|
|
password: 'pass',
|
2018-04-11 22:25:42 +00:00
|
|
|
});
|
|
|
|
response = await page.reload();
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail if wrong credentials', async () => {
|
|
|
|
const { page, server } = 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
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.status()).toBe(401);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should allow disable authentication', async () => {
|
|
|
|
const { page, server } = 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
|
|
|
});
|
|
|
|
let response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
await page.authenticate(null);
|
|
|
|
// Navigate to a different origin to bust Chrome's credential caching.
|
|
|
|
response = await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
|
|
|
expect(response.status()).toBe(401);
|
|
|
|
});
|
2021-03-15 07:02:07 +00:00
|
|
|
it('should not disable caching', async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
page.on('response', (r) => responses.set(r.url().split('/').pop(), r));
|
|
|
|
|
|
|
|
// 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
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|