Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(fetch): basic authentication without realm (#10979)

Yury Semikhatsky707befd6f735
diff --git a/packages/playwright-core/src/server/fetch.ts b/packages/playwright-core/src/server/fetch.ts
index 9a062229f5..52eb9d0660 100644
--- a/packages/playwright-core/src/server/fetch.ts
+++ b/packages/playwright-core/src/server/fetch.ts
@@ -313,7 +313,7 @@ export abstract class APIRequestContext extends SdkObject {
if (response.statusCode === 401 && !options.headers!['authorization']) {
const auth = response.headers['www-authenticate'];
const credentials = this._defaultOptions().httpCredentials;
- if (auth?.trim().startsWith('Basic ') && credentials) {
+ if (auth?.trim().startsWith('Basic') && credentials) {
const { username, password } = credentials;
const encoded = Buffer.from(`${username || ''}:${password || ''}`).toString('base64');
options.headers!['authorization'] = `Basic ${encoded}`;
diff --git a/tests/global-fetch.spec.ts b/tests/global-fetch.spec.ts
index 1aa36e4383..8aae416919 100644
--- a/tests/global-fetch.spec.ts
+++ b/tests/global-fetch.spec.ts
@@ -115,6 +115,24 @@ it('should return error with wrong credentials', async ({ playwright, server })
expect(response.status()).toBe(401);
});
+it('should support WWW-Authenticate: Basic', async ({ playwright, server }) => {
+ let credentials;
+ server.setRoute('/empty.html', (req, res) => {
+ if (!req.headers.authorization) {
+ res.writeHead(401, { 'WWW-Authenticate': 'Basic' });
+ res.end('HTTP Error 401 Unauthorized: Access is denied');
+ return;
+ }
+ credentials = Buffer.from((req.headers.authorization).split(' ')[1] || '', 'base64').toString();
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ res.end();
+ });
+ const request = await playwright.request.newContext({ httpCredentials: { username: 'user', password: 'pass' } });
+ const response = await request.get(server.EMPTY_PAGE);
+ expect(response.status()).toBe(200);
+ expect(credentials).toBe('user:pass');
+});
+
it('should use socks proxy', async ({ playwright, server, socksPort }) => {
const request = await playwright.request.newContext({ proxy: {
server: `socks5://localhost:${socksPort}`,