Skip to content

Commit

Permalink
Add response.ok (#2043)
Browse files Browse the repository at this point in the history
Co-authored-by: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>
Co-authored-by: Iswarya Sankaran <iswarya.sankaran@adp.com>
  • Loading branch information
3 people authored May 22, 2022
1 parent c25af09 commit 22d58fb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
12 changes: 12 additions & 0 deletions documentation/3-streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ The server's IP address.
Whether the response comes from cache or not.
### `ok`
**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`.
### `statusCode`
**Type: `number`**
Expand Down
1 change: 1 addition & 0 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
typedResponse.isFromCache = (this._nativeResponse as any).fromCache ?? false;
typedResponse.ip = this.ip;
typedResponse.retryCount = this.retryCount;
typedResponse.ok = isResponseOk(typedResponse);

this._isFromCache = typedResponse.isFromCache;

Expand Down
7 changes: 7 additions & 0 deletions source/core/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ export interface PlainResponse extends IncomingMessageWithTimings {
The result of the request.
*/
body?: unknown;

/**
Whether the response was successful.
__Note__: Got throws automatically when `response.ok` is `false` and `throwHttpErrors` is `true`.
*/
ok: boolean;
}

// For Promise support
Expand Down
40 changes: 40 additions & 0 deletions test/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.true(ok);
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<HTTPError>(got(''), {instanceOf: HTTPError});
t.is(error.response.statusCode, 404);
t.is(error.response.ok, false);
t.is(error.response.body, 'not');
});

0 comments on commit 22d58fb

Please sign in to comment.