Skip to content

Commit 4f21eb3

Browse files
committedApr 15, 2021
Throw when afterResponse hook returns an invalid value
Fixes #1569
1 parent 2e95440 commit 4f21eb3

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
 

‎source/as-promise/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export default function asPromise<T>(firstRequest: Request): CancelableRequest<T
9999

100100
throw new RetryError(request);
101101
});
102+
103+
if (!(is.object(response) && is.number(response.statusCode) && response.body)) {
104+
throw new TypeError('The `afterResponse` hook returned an invalid value');
105+
}
102106
}
103107
} catch (error) {
104108
request._beforeError(error);

‎test/hooks.ts

+15
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ test('catches beforeRetry thrown errors', withServer, async (t, server, got) =>
145145
});
146146
});
147147

148+
test('throws if afterResponse returns an invalid value', withServer, async (t, server, got) => {
149+
server.get('/', echoHeaders);
150+
151+
await t.throwsAsync(got('', {
152+
hooks: {
153+
afterResponse: [
154+
// @ts-expect-error
155+
() => {}
156+
]
157+
}
158+
}), {
159+
message: 'The `afterResponse` hook returned an invalid value'
160+
});
161+
});
162+
148163
test('catches afterResponse thrown errors', withServer, async (t, server, got) => {
149164
server.get('/', echoHeaders);
150165

‎test/pagination.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ test('calls init hooks on pagination', withServer, async (t, server) => {
716716

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.