2018-06-14 23:48:14 +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.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {TLSSocket} from 'tls';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
|
|
|
import expect from 'expect';
|
2022-10-19 07:06:31 +00:00
|
|
|
import {Browser} from 'puppeteer-core/internal/api/Browser.js';
|
|
|
|
import {BrowserContext} from 'puppeteer-core/internal/api/BrowserContext.js';
|
2022-10-05 12:17:03 +00:00
|
|
|
import {Page} from 'puppeteer-core/internal/api/Page.js';
|
|
|
|
import {HTTPResponse} from 'puppeteer-core/internal/common/HTTPResponse.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
import {getTestState} from './mocha-utils.js';
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('ignoreHTTPSErrors', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
/* Note that this test creates its own browser rather than use
|
|
|
|
* the one provided by the test set-up as we need one
|
|
|
|
* with ignoreHTTPSErrors set to true
|
|
|
|
*/
|
2022-06-15 10:09:22 +00:00
|
|
|
let browser!: Browser;
|
|
|
|
let context: BrowserContext;
|
|
|
|
let page!: Page;
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
before(async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {defaultBrowserOptions, puppeteer} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
const options = Object.assign(
|
2022-06-22 13:25:44 +00:00
|
|
|
{ignoreHTTPSErrors: true},
|
2020-05-07 10:54:55 +00:00
|
|
|
defaultBrowserOptions
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
browser = await puppeteer.launch(options);
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
after(async () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
beforeEach(async () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
context = await browser.createIncognitoBrowserContext();
|
|
|
|
page = await context.newPage();
|
|
|
|
});
|
2019-02-13 19:35:49 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
afterEach(async () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
await context.close();
|
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Response.securityDetails', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {httpsServer} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
const [serverRequest, response] = await Promise.all([
|
|
|
|
httpsServer.waitForRequest('/empty.html'),
|
2020-05-07 10:54:55 +00:00
|
|
|
page.goto(httpsServer.EMPTY_PAGE),
|
2020-04-09 05:56:25 +00:00
|
|
|
]);
|
2022-06-15 10:09:22 +00:00
|
|
|
const securityDetails = response!.securityDetails()!;
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(securityDetails.issuer()).toBe('puppeteer-tests');
|
2022-06-15 10:05:25 +00:00
|
|
|
const protocol = (serverRequest.socket as TLSSocket)
|
2022-06-15 10:09:22 +00:00
|
|
|
.getProtocol()!
|
2022-06-15 10:05:25 +00:00
|
|
|
.replace('v', ' ');
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(securityDetails.protocol()).toBe(protocol);
|
|
|
|
expect(securityDetails.subjectName()).toBe('puppeteer-tests');
|
2020-05-18 10:58:46 +00:00
|
|
|
expect(securityDetails.validFrom()).toBe(1589357069);
|
|
|
|
expect(securityDetails.validTo()).toBe(1904717069);
|
|
|
|
expect(securityDetails.subjectAlternativeNames()).toEqual([
|
|
|
|
'www.puppeteer-tests.test',
|
|
|
|
'www.puppeteer-tests-1.test',
|
|
|
|
]);
|
2019-02-13 19:35:49 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be |null| for non-secure requests', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {server} = getTestState();
|
2019-02-13 19:35:49 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(response.securityDetails()).toBe(null);
|
2018-06-14 23:48:14 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Network redirects should report SecurityDetails', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {httpsServer} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
httpsServer.setRedirect('/plzredirect', '/empty.html');
|
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);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const [serverRequest] = await Promise.all([
|
2020-04-09 05:56:25 +00:00
|
|
|
httpsServer.waitForRequest('/plzredirect'),
|
2020-05-07 10:54:55 +00:00
|
|
|
page.goto(httpsServer.PREFIX + '/plzredirect'),
|
2020-04-09 05:56:25 +00:00
|
|
|
]);
|
|
|
|
expect(responses.length).toBe(2);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(responses[0]!.status()).toBe(302);
|
|
|
|
const securityDetails = responses[0]!.securityDetails()!;
|
2022-06-15 10:05:25 +00:00
|
|
|
const protocol = (serverRequest.socket as TLSSocket)
|
2022-06-15 10:09:22 +00:00
|
|
|
.getProtocol()!
|
2022-06-15 10:05:25 +00:00
|
|
|
.replace('v', ' ');
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(securityDetails.protocol()).toBe(protocol);
|
2018-09-04 09:04:27 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {httpsServer} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
const response = await page.goto(httpsServer.EMPTY_PAGE).catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error).toBeUndefined();
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with request interception', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {httpsServer} = getTestState();
|
2020-04-09 05:56:25 +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();
|
|
|
|
});
|
|
|
|
const response = (await page.goto(httpsServer.EMPTY_PAGE))!;
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work with mixed content', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {server, httpsServer} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
httpsServer.setRoute('/mixedcontent.html', (_req, res) => {
|
2020-04-09 05:56:25 +00:00
|
|
|
res.end(`<iframe src=${server.EMPTY_PAGE}></iframe>`);
|
2018-06-14 23:48:14 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(httpsServer.PREFIX + '/mixedcontent.html', {
|
|
|
|
waitUntil: 'load',
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(page.frames().length).toBe(2);
|
|
|
|
// Make sure blocked iframe has functional execution context
|
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/2709
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(await page.frames()[0]!.evaluate('1 + 2')).toBe(3);
|
|
|
|
expect(await page.frames()[1]!.evaluate('2 + 3')).toBe(5);
|
2018-06-14 23:48:14 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|