Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response.ok #2043

Merged
merged 4 commits into from May 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions documentation/3-streams.md
Expand Up @@ -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.
IswaryaS marked this conversation as resolved.
Show resolved Hide resolved

Whether the response was successful (status code in the range 200-299).
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

### `statusCode`

**Type: `number`**
Expand Down
1 change: 1 addition & 0 deletions source/core/index.ts
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
6 changes: 6 additions & 0 deletions source/core/response.ts
Expand Up @@ -91,6 +91,12 @@ export interface PlainResponse extends IncomingMessageWithTimings {
The result of the request.
*/
body?: unknown;

/**
Whether the response was successful (status code in the range 200-299).
This property need not be checked if throwHttpErrors is true.
*/
ok: boolean;
}

// For Promise support
Expand Down
40 changes: 40 additions & 0 deletions test/http.ts
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');
});