Skip to content

Commit

Permalink
Fix parsing response when using afterResponse hook (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Apr 24, 2019
1 parent 7ce0dbd commit e2054cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion source/as-promise.ts
Expand Up @@ -56,7 +56,9 @@ export default function asPromise(options: Options) {
updatedOptions = reNormalizeArguments(mergeOptions(options, {
...updatedOptions,
retry: 0,
throwHttpErrors: false
throwHttpErrors: false,
responseType: 'text',
resolveBodyOnly: false
}));

// Remove any further hooks for that request, because we we'll call them anyway.
Expand Down
1 change: 1 addition & 0 deletions source/request-as-event-emitter.ts
Expand Up @@ -151,6 +151,7 @@ export default (options, input?: TransformStream) => {
const redirectOptions = {
...options,
port: null,
auth: null,
...urlToOptions(redirectURL)
};

Expand Down
21 changes: 21 additions & 0 deletions test/hooks.ts
Expand Up @@ -477,3 +477,24 @@ test('beforeError allows modifications', async t => {
}
}), errorString2);
});

test('does not break on `afterResponse` hook with JSON mode', withServer, async (t, server, got) => {
server.get('/foobar', echoHeaders);

await t.notThrowsAsync(got('/', {
hooks: {
afterResponse: [
(response, retryWithMergedOptions) => {
if (response.statusCode === 404) {
return retryWithMergedOptions({
path: '/foobar'
});
}

return response;
}
]
},
responseType: 'json'
}));
});

0 comments on commit e2054cd

Please sign in to comment.