Skip to content

Commit

Permalink
fix: fulfill with unassigned status codes (#30856)
Browse files Browse the repository at this point in the history
Fixes #30773
  • Loading branch information
yury-s committed May 17, 2024
1 parent 4ad94c1 commit b375f10
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
});
Expand Down Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
6 changes: 5 additions & 1 deletion packages/playwright-core/src/server/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 }];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions tests/page/page-request-fulfill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 }) => {
Expand Down

0 comments on commit b375f10

Please sign in to comment.