Skip to content

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

fix: fulfill with unassigned status codes (#30856)

Yury Semikhatskyb375f1077871
diff --git a/packages/playwright-core/src/server/chromium/crNetworkManager.ts b/packages/playwright-core/src/server/chromium/crNetworkManager.ts
index b78dd4084e..6411457a32 100644
--- a/packages/playwright-core/src/server/chromium/crNetworkManager.ts
+++ b/packages/playwright-core/src/server/chromium/crNetworkManager.ts
@@ -317,7 +317,7 @@ export class CRNetworkManager {
requestPausedSessionInfo!.session._sendMayFail('Fetch.fulfillRequest', {
requestId: requestPausedEvent.requestId,
responseCode: 204,
- responsePhrase: network.STATUS_TEXTS['204'],
+ responsePhrase: network.statusText(204),
responseHeaders,
body: '',
});
@@ -622,7 +622,7 @@ class RouteImpl implements network.RouteDelegate {
await this._session.send('Fetch.fulfillRequest', {
requestId: this._interceptionId!,
responseCode: response.status,
- responsePhrase: network.STATUS_TEXTS[String(response.status)],
+ responsePhrase: network.statusText(response.status),
responseHeaders,
body,
});
diff --git a/packages/playwright-core/src/server/firefox/ffNetworkManager.ts b/packages/playwright-core/src/server/firefox/ffNetworkManager.ts
index 081a1c9480..266f5bcb83 100644
--- a/packages/playwright-core/src/server/firefox/ffNetworkManager.ts
+++ b/packages/playwright-core/src/server/firefox/ffNetworkManager.ts
@@ -242,7 +242,7 @@ class FFRouteImpl implements network.RouteDelegate {
await this._session.sendMayFail('Network.fulfillInterceptedRequest', {
requestId: this._request._id,
status: response.status,
- statusText: network.STATUS_TEXTS[String(response.status)] || '',
+ statusText: network.statusText(response.status),
headers: response.headers,
base64body,
});
diff --git a/packages/playwright-core/src/server/network.ts b/packages/playwright-core/src/server/network.ts
index 83b0093a38..fd62e1751b 100644
--- a/packages/playwright-core/src/server/network.ts
+++ b/packages/playwright-core/src/server/network.ts
@@ -616,7 +616,7 @@ export interface RouteDelegate {
}
// List taken from https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml with extra 306 and 418 codes.
-export const STATUS_TEXTS: { [status: string]: string } = {
+const STATUS_TEXTS: { [status: string]: string } = {
'100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing',
@@ -682,6 +682,10 @@ export const STATUS_TEXTS: { [status: string]: string } = {
'511': 'Network Authentication Required',
};
+export function statusText(status: number): string {
+ return STATUS_TEXTS[String(status)] || 'Unknown';
+}
+
export function singleHeader(name: string, value: string): HeadersArray {
return [{ name, value }];
}
diff --git a/packages/playwright-core/src/server/webkit/wkInterceptableRequest.ts b/packages/playwright-core/src/server/webkit/wkInterceptableRequest.ts
index 5218420480..f3c669dfc7 100644
--- a/packages/playwright-core/src/server/webkit/wkInterceptableRequest.ts
+++ b/packages/playwright-core/src/server/webkit/wkInterceptableRequest.ts
@@ -128,7 +128,7 @@ export class WKRouteImpl implements network.RouteDelegate {
await this._session.sendMayFail('Network.interceptRequestWithResponse', {
requestId: this._requestId,
status: response.status,
- statusText: network.STATUS_TEXTS[String(response.status)],
+ statusText: network.statusText(response.status),
mimeType,
headers,
base64Encoded: response.isBase64,
diff --git a/tests/page/page-request-fulfill.spec.ts b/tests/page/page-request-fulfill.spec.ts
index 4a67b4353c..16f559b467 100644
--- a/tests/page/page-request-fulfill.spec.ts
+++ b/tests/page/page-request-fulfill.spec.ts
@@ -78,8 +78,9 @@ it('should work with status code 422', async ({ page, server }) => {
expect(await page.evaluate(() => document.body.textContent)).toBe('Yo, page!');
});
-it('should throw exception if status code is not supported', async ({ page, server, browserName }) => {
+it('should fulfill with unuassigned status codes', async ({ page, server, browserName }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/28490' });
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/30773' });
let fulfillPromiseCallback;
const fulfillPromise = new Promise<Error|undefined>(f => fulfillPromiseCallback = f);
await page.route('**/data.json', route => {
@@ -89,14 +90,14 @@ it('should throw exception if status code is not supported', async ({ page, serv
}).catch(e => e));
});
await page.goto(server.EMPTY_PAGE);
- page.evaluate(url => fetch(url), server.PREFIX + '/data.json').catch(() => {});
+ const response = await page.evaluate(async url => {
+ const { status, statusText } = await fetch(url);
+ return { status, statusText };
+ }, server.PREFIX + '/data.json');
const error = await fulfillPromise;
- if (browserName === 'chromium') {
- expect(error).toBeTruthy();
- expect(error.message).toContain(' Invalid http status code or phrase');
- } else {
- expect(error).toBe(undefined);
- }
+ expect(error).toBe(undefined);
+ expect(response.status).toBe(430);
+ expect(response.statusText).toBe('Unknown');
});
it('should not throw if request was cancelled by the page', async ({ page, server }) => {