From a26b12b7c775c36271cd4c98e39bbd59f4356320 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Fri, 26 Nov 2021 09:17:34 +0100 Subject: [PATCH] fix: parse statusText from the extraInfo event (#7798) Issues: #7458 --- src/common/HTTPResponse.ts | 21 +++++++++++++++++++-- test/network.spec.ts | 11 +++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/common/HTTPResponse.ts b/src/common/HTTPResponse.ts index b88ff2dc5fd..f1e225815cd 100644 --- a/src/common/HTTPResponse.ts +++ b/src/common/HTTPResponse.ts @@ -78,8 +78,9 @@ export class HTTPResponse { ip: responsePayload.remoteIPAddress, port: responsePayload.remotePort, }; - // TODO extract statusText from extraInfo.headersText instead if present - this._statusText = responsePayload.statusText; + this._statusText = + this._parseStatusTextFromExtrInfo(extraInfo) || + responsePayload.statusText; this._url = request.url(); this._fromDiskCache = !!responsePayload.fromDiskCache; this._fromServiceWorker = !!responsePayload.fromServiceWorker; @@ -94,6 +95,22 @@ export class HTTPResponse { : null; } + /** + * @internal + */ + _parseStatusTextFromExtrInfo( + extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null + ): string | undefined { + if (!extraInfo || !extraInfo.headersText) return; + const firstLine = extraInfo.headersText.split('\r', 1)[0]; + if (!firstLine) return; + const match = firstLine.match(/[^ ]* [^ ]* (.*)/); + if (!match) return; + const statusText = match[1]; + if (!statusText) return; + return statusText; + } + /** * @internal */ diff --git a/test/network.spec.ts b/test/network.spec.ts index c73a043aa86..e09633983f6 100644 --- a/test/network.spec.ts +++ b/test/network.spec.ts @@ -417,6 +417,17 @@ describe('network', function () { const response = await page.goto(server.PREFIX + '/cool'); expect(response.statusText()).toBe('cool!'); }); + + it('handles missing status text', async () => { + const { page, server } = getTestState(); + + server.setRoute('/nostatus', (req, res) => { + res.writeHead(200, ''); + res.end(); + }); + const response = await page.goto(server.PREFIX + '/nostatus'); + expect(response.statusText()).toBe(''); + }); }); describeFailsFirefox('Network Events', function () {