diff --git a/docs/docs/api/Dispatcher.md b/docs/docs/api/Dispatcher.md index f8639267825..70fc5c26826 100644 --- a/docs/docs/api/Dispatcher.md +++ b/docs/docs/api/Dispatcher.md @@ -476,6 +476,7 @@ The `RequestOptions.method` property should not be value `'CONNECT'`. #### Parameter: `ResponseData` * **statusCode** `number` +* **statusText** `string` - The status message from the response (e.g., "OK", "Not Found"). * **headers** `Record` - Note that all header keys are lower-cased, e.g. `content-type`. * **body** `stream.Readable` which also implements [the body mixin from the Fetch Standard](https://fetch.spec.whatwg.org/#body-mixin). * **trailers** `Record` - This object starts out @@ -517,7 +518,7 @@ await once(server, 'listening') const client = new Client(`http://localhost:${server.address().port}`) try { - const { body, headers, statusCode, trailers } = await client.request({ + const { body, headers, statusCode, statusText, trailers } = await client.request({ path: '/', method: 'GET' }) diff --git a/lib/api/api-request.js b/lib/api/api-request.js index c3461b23c84..f6d15f75b0e 100644 --- a/lib/api/api-request.js +++ b/lib/api/api-request.js @@ -121,6 +121,7 @@ class RequestHandler extends AsyncResource { try { this.runInAsyncScope(callback, null, null, { statusCode, + statusText: statusMessage, headers, trailers: this.trailers, opaque, diff --git a/test/request.js b/test/request.js index 6166a95e079..f0841654a7a 100644 --- a/test/request.js +++ b/test/request.js @@ -468,3 +468,29 @@ describe('Should include headers from iterable objects', scope => { }) }) }) + +test('request should include statusText in response', async t => { + t = tspl(t, { plan: 2 }) + + const server = createServer((req, res) => { + res.writeHead(200, 'Custom Status Text', { 'content-type': 'text/plain' }) + res.end('hello') + }) + + after(() => { + server.closeAllConnections?.() + server.close() + }) + + await new Promise((resolve) => server.listen(0, resolve)) + + const { statusText, body } = await request({ + method: 'GET', + origin: `http://localhost:${server.address().port}`, + path: '/' + }) + + t.strictEqual(statusText, 'Custom Status Text') + await body.dump() + t.ok('request completed') +}) diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 13b33ececc8..684ca439c9a 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -181,6 +181,7 @@ declare namespace Dispatcher { } export interface ResponseData { statusCode: number; + statusText: string; headers: IncomingHttpHeaders; body: BodyReadable & BodyMixin; trailers: Record;