From b8572e7624c9fe30f17e60e119a1240b80839548 Mon Sep 17 00:00:00 2001 From: Iswarya Sankaran Date: Tue, 17 May 2022 22:31:34 +0530 Subject: [PATCH 1/4] Add a response.ok to say whether it is a success or failure status code --- source/core/index.ts | 2 ++ source/core/response.ts | 5 +++++ test/http.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/source/core/index.ts b/source/core/index.ts index 1d913c265..2fe465dcd 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -147,6 +147,7 @@ export default class Request extends Duplex implements RequestEvents { requestUrl?: URL; redirectUrls: URL[]; retryCount: number; + ok?: boolean; declare private _requestOptions: NativeRequestOptions; @@ -632,6 +633,7 @@ export default class Request extends Duplex implements RequestEvents { typedResponse.isFromCache = (this._nativeResponse as any).fromCache ?? false; typedResponse.ip = this.ip; typedResponse.retryCount = this.retryCount; + typedResponse.ok = (statusCode >= 200 && statusCode <= 299); this._isFromCache = typedResponse.isFromCache; diff --git a/source/core/response.ts b/source/core/response.ts index 413dcad4a..60580d62f 100644 --- a/source/core/response.ts +++ b/source/core/response.ts @@ -91,6 +91,11 @@ export interface PlainResponse extends IncomingMessageWithTimings { The result of the request. */ body?: unknown; + + /** + Whether the response status code is 2xx. + */ + ok?: boolean; } // For Promise support diff --git a/test/http.ts b/test/http.ts index 8ff23637c..f18388d1a 100644 --- a/test/http.ts +++ b/test/http.ts @@ -375,3 +375,43 @@ test('ClientRequest can throw before promise resolves', async t => { message: /EINVAL|EHOSTUNREACH|ETIMEDOUT/, }); }); + +test('status code 200 has response ok is true', withServer, async (t, server, got) => { + server.get('/', (_request, response) => { + response.statusCode = 200; + response.end(); + }); + + const promise = got(''); + await t.notThrowsAsync(promise); + const {statusCode, body, ok} = await promise; + t.is(ok, true); + t.is(statusCode, 200); + t.is(body, ''); +}); + +test('status code 404 has response ok is false if error is not thrown', withServer, async (t, server, got) => { + server.get('/', (_request, response) => { + response.statusCode = 404; + response.end(); + }); + + const promise = got('', {throwHttpErrors: false}); + await t.notThrowsAsync(promise); + const {statusCode, body, ok} = await promise; + t.is(ok, false); + t.is(statusCode, 404); + t.is(body, ''); +}); + +test('status code 404 has error response ok is false if error is thrown', withServer, async (t, server, got) => { + server.get('/', (_request, response) => { + response.statusCode = 404; + response.end('not'); + }); + + const error = await t.throwsAsync(got(''), {instanceOf: HTTPError}); + t.is(error.response.statusCode, 404); + t.is(error.response.ok, false); + t.is(error.response.body, 'not'); +}); From e3ab2444ba936879d54e037d0b75f7e5387af94d Mon Sep 17 00:00:00 2001 From: Iswarya Sankaran Date: Wed, 18 May 2022 09:53:24 +0530 Subject: [PATCH 2/4] Addressed all comments --- documentation/3-streams.md | 9 +++++++++ source/core/index.ts | 3 +-- source/core/response.ts | 5 +++-- test/http.ts | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/documentation/3-streams.md b/documentation/3-streams.md index 854185189..27b56b406 100644 --- a/documentation/3-streams.md +++ b/documentation/3-streams.md @@ -354,6 +354,15 @@ The server's IP address. Whether the response comes from cache or not. +### `ok` + +**Type: `boolean`** + +**Note:** +> - This property need not be checked if throwHttpErrors is true. + +Whether the response was successful (status code in the range 200-299). + ### `statusCode` **Type: `number`** diff --git a/source/core/index.ts b/source/core/index.ts index 2fe465dcd..63e2c5e7b 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -147,7 +147,6 @@ export default class Request extends Duplex implements RequestEvents { requestUrl?: URL; redirectUrls: URL[]; retryCount: number; - ok?: boolean; declare private _requestOptions: NativeRequestOptions; @@ -633,7 +632,7 @@ export default class Request extends Duplex implements RequestEvents { typedResponse.isFromCache = (this._nativeResponse as any).fromCache ?? false; typedResponse.ip = this.ip; typedResponse.retryCount = this.retryCount; - typedResponse.ok = (statusCode >= 200 && statusCode <= 299); + typedResponse.ok = isResponseOk(typedResponse); this._isFromCache = typedResponse.isFromCache; diff --git a/source/core/response.ts b/source/core/response.ts index 60580d62f..eb89d0207 100644 --- a/source/core/response.ts +++ b/source/core/response.ts @@ -93,9 +93,10 @@ export interface PlainResponse extends IncomingMessageWithTimings { body?: unknown; /** - Whether the response status code is 2xx. + Whether the response was successful (status code in the range 200-299). + This property need not be checked if throwHttpErrors is true. */ - ok?: boolean; + ok: boolean; } // For Promise support diff --git a/test/http.ts b/test/http.ts index f18388d1a..662129461 100644 --- a/test/http.ts +++ b/test/http.ts @@ -385,7 +385,7 @@ test('status code 200 has response ok is true', withServer, async (t, server, go const promise = got(''); await t.notThrowsAsync(promise); const {statusCode, body, ok} = await promise; - t.is(ok, true); + t.true(ok); t.is(statusCode, 200); t.is(body, ''); }); From 0781198e09ff1ca611e1e1a355ef1f7f81486bb5 Mon Sep 17 00:00:00 2001 From: IswaryaS Date: Thu, 19 May 2022 17:46:19 +0530 Subject: [PATCH 3/4] Update documentation/3-streams.md Co-authored-by: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> --- documentation/3-streams.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/3-streams.md b/documentation/3-streams.md index 27b56b406..bd812f671 100644 --- a/documentation/3-streams.md +++ b/documentation/3-streams.md @@ -359,7 +359,7 @@ Whether the response comes from cache or not. **Type: `boolean`** **Note:** -> - This property need not be checked if throwHttpErrors is true. +> - Got throws automatically when `response.ok` is `false` and `throwHttpErrors` is `true`. Whether the response was successful (status code in the range 200-299). From 249dc9acf7d2c11e6922158d7dacda027ce0b673 Mon Sep 17 00:00:00 2001 From: Iswarya Sankaran Date: Thu, 19 May 2022 19:22:18 +0530 Subject: [PATCH 4/4] Update docs for response.ok --- documentation/3-streams.md | 7 +++++-- source/core/response.ts | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/documentation/3-streams.md b/documentation/3-streams.md index bd812f671..ec029dcc4 100644 --- a/documentation/3-streams.md +++ b/documentation/3-streams.md @@ -358,11 +358,14 @@ Whether the response comes from cache or not. **Type: `boolean`** +Whether the response was successful + **Note:** +> - A request is successful when the status code of the final request is `2xx` or `3xx`. +> - When [following redirects](2-options.md#followredirect), a request is successful **only** when the status code of the final request is `2xx`. +> - `304` responses are always considered successful. > - Got throws automatically when `response.ok` is `false` and `throwHttpErrors` is `true`. -Whether the response was successful (status code in the range 200-299). - ### `statusCode` **Type: `number`** diff --git a/source/core/response.ts b/source/core/response.ts index eb89d0207..6fee258b0 100644 --- a/source/core/response.ts +++ b/source/core/response.ts @@ -93,8 +93,9 @@ export interface PlainResponse extends IncomingMessageWithTimings { body?: unknown; /** - Whether the response was successful (status code in the range 200-299). - This property need not be checked if throwHttpErrors is true. + Whether the response was successful. + + __Note__: Got throws automatically when `response.ok` is `false` and `throwHttpErrors` is `true`. */ ok: boolean; }