fix: providing null to page.authenticate should disable authentication (#12203)

This commit is contained in:
Nikolay Vitkov 2024-05-22 20:18:37 +08:00 committed by GitHub
parent 97a4951d52
commit f375267e79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 7 additions and 10 deletions

View File

@ -10,7 +10,7 @@ Provide credentials for `HTTP authentication`.
```typescript ```typescript
class Page { class Page {
abstract authenticate(credentials: Credentials): Promise<void>; abstract authenticate(credentials: Credentials | null): Promise<void>;
} }
``` ```
@ -35,7 +35,7 @@ credentials
</td><td> </td><td>
[Credentials](./puppeteer.credentials.md) [Credentials](./puppeteer.credentials.md) \| null
</td><td> </td><td>

View File

@ -1420,7 +1420,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
* @remarks * @remarks
* To disable authentication, pass `null`. * To disable authentication, pass `null`.
*/ */
abstract authenticate(credentials: Credentials): Promise<void>; abstract authenticate(credentials: Credentials | null): Promise<void>;
/** /**
* The extra HTTP headers will be sent with every request the page initiates. * The extra HTTP headers will be sent with every request the page initiates.

View File

@ -64,7 +64,7 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
#frameManager: FrameProvider; #frameManager: FrameProvider;
#networkEventManager = new NetworkEventManager(); #networkEventManager = new NetworkEventManager();
#extraHTTPHeaders?: Record<string, string>; #extraHTTPHeaders?: Record<string, string>;
#credentials?: Credentials; #credentials: Credentials | null = null;
#attemptedAuthentications = new Set<string>(); #attemptedAuthentications = new Set<string>();
#userRequestInterceptionEnabled = false; #userRequestInterceptionEnabled = false;
#protocolRequestInterceptionEnabled = false; #protocolRequestInterceptionEnabled = false;
@ -121,7 +121,7 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
this.#clients.delete(client); this.#clients.delete(client);
} }
async authenticate(credentials?: Credentials): Promise<void> { async authenticate(credentials: Credentials | null): Promise<void> {
this.#credentials = credentials; this.#credentials = credentials;
const enabled = this.#userRequestInterceptionEnabled || !!this.#credentials; const enabled = this.#userRequestInterceptionEnabled || !!this.#credentials;
if (enabled === this.#protocolRequestInterceptionEnabled) { if (enabled === this.#protocolRequestInterceptionEnabled) {

View File

@ -743,7 +743,7 @@ export class CdpPage extends Page {
this.#bindings.delete(name); this.#bindings.delete(name);
} }
override async authenticate(credentials: Credentials): Promise<void> { override async authenticate(credentials: Credentials | null): Promise<void> {
return await this.#frameManager.networkManager.authenticate(credentials); return await this.#frameManager.networkManager.authenticate(credentials);
} }

View File

@ -751,10 +751,7 @@ describe('network', function () {
}); });
let response = (await page.goto(server.EMPTY_PAGE))!; let response = (await page.goto(server.EMPTY_PAGE))!;
expect(response.status()).toBe(200); expect(response.status()).toBe(200);
await page.authenticate({ await page.authenticate(null);
username: '',
password: '',
});
// Navigate to a different origin to bust Chrome's credential caching. // Navigate to a different origin to bust Chrome's credential caching.
try { try {
response = (await page.goto( response = (await page.goto(