refactor: configure https error handling globally (#12339)

This commit is contained in:
Alex Rudenko 2024-04-26 10:16:32 +02:00 committed by GitHub
parent ad0076093d
commit b665c60de8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 20 additions and 47 deletions

View File

@ -59,7 +59,6 @@ export class CdpBrowser extends BrowserBase {
product, product,
connection, connection,
contextIds, contextIds,
ignoreHTTPSErrors,
defaultViewport, defaultViewport,
process, process,
closeCallback, closeCallback,
@ -67,10 +66,14 @@ export class CdpBrowser extends BrowserBase {
isPageTargetCallback, isPageTargetCallback,
waitForInitiallyDiscoveredTargets waitForInitiallyDiscoveredTargets
); );
if (ignoreHTTPSErrors) {
await connection.send('Security.setIgnoreCertificateErrors', {
ignore: true,
});
}
await browser._attach(); await browser._attach();
return browser; return browser;
} }
#ignoreHTTPSErrors: boolean;
#defaultViewport?: Viewport | null; #defaultViewport?: Viewport | null;
#process?: ChildProcess; #process?: ChildProcess;
#connection: Connection; #connection: Connection;
@ -85,7 +88,6 @@ export class CdpBrowser extends BrowserBase {
product: 'chrome' | 'firefox' | undefined, product: 'chrome' | 'firefox' | undefined,
connection: Connection, connection: Connection,
contextIds: string[], contextIds: string[],
ignoreHTTPSErrors: boolean,
defaultViewport?: Viewport | null, defaultViewport?: Viewport | null,
process?: ChildProcess, process?: ChildProcess,
closeCallback?: BrowserCloseCallback, closeCallback?: BrowserCloseCallback,
@ -95,7 +97,6 @@ export class CdpBrowser extends BrowserBase {
) { ) {
super(); super();
product = product || 'chrome'; product = product || 'chrome';
this.#ignoreHTTPSErrors = ignoreHTTPSErrors;
this.#defaultViewport = defaultViewport; this.#defaultViewport = defaultViewport;
this.#process = process; this.#process = process;
this.#connection = connection; this.#connection = connection;
@ -268,7 +269,6 @@ export class CdpBrowser extends BrowserBase {
context, context,
this.#targetManager, this.#targetManager,
createSession, createSession,
this.#ignoreHTTPSErrors,
this.#defaultViewport ?? null this.#defaultViewport ?? null
); );
} }
@ -279,7 +279,6 @@ export class CdpBrowser extends BrowserBase {
context, context,
this.#targetManager, this.#targetManager,
createSession, createSession,
this.#ignoreHTTPSErrors,
this.#defaultViewport ?? null this.#defaultViewport ?? null
); );
} }

View File

@ -76,13 +76,12 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
constructor( constructor(
client: CDPSession, client: CDPSession,
page: CdpPage, page: CdpPage,
ignoreHTTPSErrors: boolean,
timeoutSettings: TimeoutSettings timeoutSettings: TimeoutSettings
) { ) {
super(); super();
this.#client = client; this.#client = client;
this.#page = page; this.#page = page;
this.#networkManager = new NetworkManager(ignoreHTTPSErrors, this); this.#networkManager = new NetworkManager(this);
this.#timeoutSettings = timeoutSettings; this.#timeoutSettings = timeoutSettings;
this.setupEventListeners(this.#client); this.setupEventListeners(this.#client);
client.once(CDPSessionEvent.Disconnected, () => { client.once(CDPSessionEvent.Disconnected, () => {

View File

@ -37,7 +37,7 @@ class MockCDPSession extends EventEmitter<CDPSessionEvents> {
describe('NetworkManager', () => { describe('NetworkManager', () => {
it('should process extra info on multiple redirects', async () => { it('should process extra info on multiple redirects', async () => {
const mockCDPSession = new MockCDPSession(); const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(true, { const manager = new NetworkManager({
frame(): CdpFrame | null { frame(): CdpFrame | null {
return null; return null;
}, },
@ -477,7 +477,7 @@ describe('NetworkManager', () => {
}); });
it(`should handle "double pause" (crbug.com/1196004) Fetch.requestPaused events for the same Network.requestWillBeSent event`, async () => { it(`should handle "double pause" (crbug.com/1196004) Fetch.requestPaused events for the same Network.requestWillBeSent event`, async () => {
const mockCDPSession = new MockCDPSession(); const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(true, { const manager = new NetworkManager({
frame(): CdpFrame | null { frame(): CdpFrame | null {
return null; return null;
}, },
@ -561,7 +561,7 @@ describe('NetworkManager', () => {
}); });
it(`should handle Network.responseReceivedExtraInfo event after Network.responseReceived event (github.com/puppeteer/puppeteer/issues/8234)`, async () => { it(`should handle Network.responseReceivedExtraInfo event after Network.responseReceived event (github.com/puppeteer/puppeteer/issues/8234)`, async () => {
const mockCDPSession = new MockCDPSession(); const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(true, { const manager = new NetworkManager({
frame(): CdpFrame | null { frame(): CdpFrame | null {
return null; return null;
}, },
@ -678,7 +678,7 @@ describe('NetworkManager', () => {
it(`should resolve the response once the late responseReceivedExtraInfo event arrives`, async () => { it(`should resolve the response once the late responseReceivedExtraInfo event arrives`, async () => {
const mockCDPSession = new MockCDPSession(); const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(true, { const manager = new NetworkManager({
frame(): CdpFrame | null { frame(): CdpFrame | null {
return null; return null;
}, },
@ -829,7 +829,7 @@ describe('NetworkManager', () => {
it(`should send responses for iframe that don't receive loadingFinished event`, async () => { it(`should send responses for iframe that don't receive loadingFinished event`, async () => {
const mockCDPSession = new MockCDPSession(); const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(true, { const manager = new NetworkManager({
frame(): CdpFrame | null { frame(): CdpFrame | null {
return null; return null;
}, },
@ -992,7 +992,7 @@ describe('NetworkManager', () => {
it(`should send responses for iframe that don't receive loadingFinished event`, async () => { it(`should send responses for iframe that don't receive loadingFinished event`, async () => {
const mockCDPSession = new MockCDPSession(); const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(true, { const manager = new NetworkManager({
frame(): CdpFrame | null { frame(): CdpFrame | null {
return null; return null;
}, },
@ -1137,7 +1137,7 @@ describe('NetworkManager', () => {
it(`should handle cached redirects`, async () => { it(`should handle cached redirects`, async () => {
const mockCDPSession = new MockCDPSession(); const mockCDPSession = new MockCDPSession();
const manager = new NetworkManager(true, { const manager = new NetworkManager({
frame(): CdpFrame | null { frame(): CdpFrame | null {
return null; return null;
}, },

View File

@ -61,7 +61,6 @@ export interface FrameProvider {
* @internal * @internal
*/ */
export class NetworkManager extends EventEmitter<NetworkManagerEvents> { export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
#ignoreHTTPSErrors: boolean;
#frameManager: FrameProvider; #frameManager: FrameProvider;
#networkEventManager = new NetworkEventManager(); #networkEventManager = new NetworkEventManager();
#extraHTTPHeaders?: Record<string, string>; #extraHTTPHeaders?: Record<string, string>;
@ -88,9 +87,8 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
#clients = new Map<CDPSession, DisposableStack>(); #clients = new Map<CDPSession, DisposableStack>();
constructor(ignoreHTTPSErrors: boolean, frameManager: FrameProvider) { constructor(frameManager: FrameProvider) {
super(); super();
this.#ignoreHTTPSErrors = ignoreHTTPSErrors;
this.#frameManager = frameManager; this.#frameManager = frameManager;
} }
@ -109,11 +107,6 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
} }
await Promise.all([ await Promise.all([
this.#ignoreHTTPSErrors
? client.send('Security.setIgnoreCertificateErrors', {
ignore: true,
})
: null,
client.send('Network.enable'), client.send('Network.enable'),
this.#applyExtraHTTPHeaders(client), this.#applyExtraHTTPHeaders(client),
this.#applyNetworkConditions(client), this.#applyNetworkConditions(client),

View File

@ -100,10 +100,9 @@ export class CdpPage extends Page {
static async _create( static async _create(
client: CDPSession, client: CDPSession,
target: CdpTarget, target: CdpTarget,
ignoreHTTPSErrors: boolean,
defaultViewport: Viewport | null defaultViewport: Viewport | null
): Promise<CdpPage> { ): Promise<CdpPage> {
const page = new CdpPage(client, target, ignoreHTTPSErrors); const page = new CdpPage(client, target);
await page.#initialize(); await page.#initialize();
if (defaultViewport) { if (defaultViewport) {
try { try {
@ -228,11 +227,7 @@ export class CdpPage extends Page {
['Page.fileChooserOpened', this.#onFileChooser.bind(this)], ['Page.fileChooserOpened', this.#onFileChooser.bind(this)],
] as const; ] as const;
constructor( constructor(client: CDPSession, target: CdpTarget) {
client: CDPSession,
target: CdpTarget,
ignoreHTTPSErrors: boolean
) {
super(); super();
this.#primaryTargetClient = client; this.#primaryTargetClient = client;
this.#tabTargetClient = client.parentSession()!; this.#tabTargetClient = client.parentSession()!;
@ -245,12 +240,7 @@ export class CdpPage extends Page {
this.#mouse = new CdpMouse(client, this.#keyboard); this.#mouse = new CdpMouse(client, this.#keyboard);
this.#touchscreen = new CdpTouchscreen(client, this.#keyboard); this.#touchscreen = new CdpTouchscreen(client, this.#keyboard);
this.#accessibility = new Accessibility(client); this.#accessibility = new Accessibility(client);
this.#frameManager = new FrameManager( this.#frameManager = new FrameManager(client, this, this._timeoutSettings);
client,
this,
ignoreHTTPSErrors,
this._timeoutSettings
);
this.#emulationManager = new EmulationManager(client); this.#emulationManager = new EmulationManager(client);
this.#tracing = new Tracing(client); this.#tracing = new Tracing(client);
this.#coverage = new Coverage(client); this.#coverage = new Coverage(client);

View File

@ -74,10 +74,10 @@ export class CdpTarget extends Target {
const session = this._session(); const session = this._session();
if (!session) { if (!session) {
return await this.createCDPSession().then(client => { return await this.createCDPSession().then(client => {
return CdpPage._create(client, this, false, null); return CdpPage._create(client, this, null);
}); });
} }
return await CdpPage._create(session, this, false, null); return await CdpPage._create(session, this, null);
} }
_subtype(): string | undefined { _subtype(): string | undefined {
@ -196,7 +196,6 @@ export class CdpTarget extends Target {
export class PageTarget extends CdpTarget { export class PageTarget extends CdpTarget {
#defaultViewport?: Viewport; #defaultViewport?: Viewport;
protected pagePromise?: Promise<Page>; protected pagePromise?: Promise<Page>;
#ignoreHTTPSErrors: boolean;
constructor( constructor(
targetInfo: Protocol.Target.TargetInfo, targetInfo: Protocol.Target.TargetInfo,
@ -204,11 +203,9 @@ export class PageTarget extends CdpTarget {
browserContext: BrowserContext, browserContext: BrowserContext,
targetManager: TargetManager, targetManager: TargetManager,
sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>, sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>,
ignoreHTTPSErrors: boolean,
defaultViewport: Viewport | null defaultViewport: Viewport | null
) { ) {
super(targetInfo, session, browserContext, targetManager, sessionFactory); super(targetInfo, session, browserContext, targetManager, sessionFactory);
this.#ignoreHTTPSErrors = ignoreHTTPSErrors;
this.#defaultViewport = defaultViewport ?? undefined; this.#defaultViewport = defaultViewport ?? undefined;
} }
@ -246,12 +243,7 @@ export class PageTarget extends CdpTarget {
? Promise.resolve(session) ? Promise.resolve(session)
: this._sessionFactory()(/* isAutoAttachEmulated=*/ false) : this._sessionFactory()(/* isAutoAttachEmulated=*/ false)
).then(client => { ).then(client => {
return CdpPage._create( return CdpPage._create(client, this, this.#defaultViewport ?? null);
client,
this,
this.#ignoreHTTPSErrors,
this.#defaultViewport ?? null
);
}); });
} }
return (await this.pagePromise) ?? null; return (await this.pagePromise) ?? null;