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
class Page {
abstract authenticate(credentials: Credentials): Promise<void>;
abstract authenticate(credentials: Credentials | null): Promise<void>;
}
```
@ -35,7 +35,7 @@ credentials
</td><td>
[Credentials](./puppeteer.credentials.md)
[Credentials](./puppeteer.credentials.md) \| null
</td><td>

View File

@ -1420,7 +1420,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
* @remarks
* 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.

View File

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

View File

@ -743,7 +743,7 @@ export class CdpPage extends Page {
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);
}

View File

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