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 () {