Skip to content

Commit

Permalink
Throw when afterResponse hook returns an invalid value
Browse files Browse the repository at this point in the history
Fixes #1569
  • Loading branch information
szmarczak committed Apr 15, 2021
1 parent 2e95440 commit 4f21eb3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions source/as-promise/index.ts
Expand Up @@ -99,6 +99,10 @@ export default function asPromise<T>(firstRequest: Request): CancelableRequest<T

throw new RetryError(request);
});

if (!(is.object(response) && is.number(response.statusCode) && response.body)) {
throw new TypeError('The `afterResponse` hook returned an invalid value');
}
}
} catch (error) {
request._beforeError(error);
Expand Down
15 changes: 15 additions & 0 deletions test/hooks.ts
Expand Up @@ -145,6 +145,21 @@ test('catches beforeRetry thrown errors', withServer, async (t, server, got) =>
});
});

test('throws if afterResponse returns an invalid value', withServer, async (t, server, got) => {
server.get('/', echoHeaders);

await t.throwsAsync(got('', {
hooks: {
afterResponse: [
// @ts-expect-error
() => {}
]
}
}), {
message: 'The `afterResponse` hook returned an invalid value'
});
});

test('catches afterResponse thrown errors', withServer, async (t, server, got) => {
server.get('/', echoHeaders);

Expand Down
2 changes: 1 addition & 1 deletion test/pagination.ts
Expand Up @@ -716,7 +716,7 @@ test('calls init hooks on pagination', withServer, async (t, server) => {

test('throws when transform does not return an array', withServer, async (t, server) => {
server.get('/', (_request, response) => {
response.end(JSON.stringify(""));
response.end(JSON.stringify(''));
});

await t.throwsAsync(got.paginate.all<string>(server.url));
Expand Down

0 comments on commit 4f21eb3

Please sign in to comment.