From 3db8b4b412a20fff5140e46233d5563e38db9fa9 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 29 Feb 2024 21:17:22 +0800 Subject: [PATCH] fix: keep statusMessage alias to statusText on response.res object closes https://github.com/eggjs/egg/issues/5300 --- src/HttpClient.ts | 3 ++- src/Response.ts | 5 +++++ test/index.test.ts | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 3e8cb382..7a550a48 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -330,6 +330,7 @@ export class HttpClient extends EventEmitter { status: -1, statusCode: -1, statusText: '', + statusMessage: '', headers: resHeaders, size: 0, aborted: false, @@ -589,7 +590,7 @@ export class HttpClient extends EventEmitter { res.headers = response.headers; res.status = res.statusCode = response.statusCode; - res.statusText = STATUS_CODES[res.status] || ''; + res.statusMessage = res.statusText = STATUS_CODES[res.status] || ''; if (res.headers['content-length']) { res.size = parseInt(res.headers['content-length']); } diff --git a/src/Response.ts b/src/Response.ts index c588af1b..66618a8d 100644 --- a/src/Response.ts +++ b/src/Response.ts @@ -45,6 +45,11 @@ export type RawResponseWithMeta = Readable & { status: number; statusCode: number; statusText: string; + /** + * @alias statusText + * @deprecated use `statusText` instead + **/ + statusMessage: string; headers: IncomingHttpHeaders; timing: Timing; // SocketInfo diff --git a/test/index.test.ts b/test/index.test.ts index b6a6c8ef..4a128179 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -24,6 +24,9 @@ describe('index.test.ts', () => { it('should work', async () => { const response = await getDefaultHttpClient().request(`${_url}html`); assert.equal(response.status, 200); + assert.equal(response.statusText, 'OK'); + assert.equal(response.res.statusMessage, 'OK'); + assert.equal(response.res.statusText, 'OK'); assert.equal(response.headers['content-type'], 'text/html'); assert(response.headers.date); assert.equal(response.url, `${_url}html`); @@ -338,6 +341,8 @@ describe('index.test.ts', () => { dataType: 'json', }); assert.equal(response.status, 400); + assert.equal(response.res.statusMessage, 'Bad Request'); + assert.equal(response.res.statusText, 'Bad Request'); assert.deepEqual(response.data, { message: 'mock 400 bad request' }); response = await httpClient.request(`${_url}bar?q=1`, {