Skip to content

Commit

Permalink
'Improvements'
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Jan 11, 2019
1 parent 1b89d8f commit 98f2872
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
4 changes: 2 additions & 2 deletions migration-guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const gotInstance = got.extend({
init: [
options => {
// Save the original option, so we can look at it in the `afterResponse` hook
options._json = options.json;
options.originalJson = options.json;

if (options.json && options.jsonReplacer) {
options.body = JSON.stringify(options.body, options.jsonReplacer);
Expand All @@ -117,7 +117,7 @@ const gotInstance = got.extend({
afterResponse: [
response => {
const options = response.request.gotOptions;
if (options._json && options.jsonReviver) {
if (options.originalJson && options.jsonReviver) {
response.body = JSON.parse(response.body, options.jsonReviver);
options.json = false; // We've handled that on our own
}
Expand Down
6 changes: 5 additions & 1 deletion source/normalize-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ const normalize = (url, options, defaults) => {
options = merge({path: ''}, url, {protocol: url.protocol || 'https:'}, options);

for (const hook of options.hooks.init) {
hook(options);
const called = hook(options);

if (called instanceof Promise) {
throw new TypeError('The `init` hook must be a synchronous function');
}
}

const {baseUrl} = options;
Expand Down
79 changes: 59 additions & 20 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava';
import delay from 'delay';
import getStream from 'get-stream';
import {createServer} from './helpers/server';
import knownHookEvents from '../source/known-hook-events'
import got from '..';

const errorString = 'oops';
Expand Down Expand Up @@ -79,61 +80,97 @@ test('async hooks', async t => {
t.is(body.foo, 'bar');
});

test('catches thrown errors', async t => {
test('catches init thrown errors', async t => {
await t.throwsAsync(() => got(s.url, {
hooks: {
beforeRequest: [
() => {
throw error;
}
]
init: [() => {
throw error;
}]
}
}), errorString);
});

test('catches promise rejections', async t => {
test('catches beforeRequest thrown errors', async t => {
await t.throwsAsync(() => got(s.url, {
hooks: {
beforeRequest: [
() => Promise.reject(error)
]
beforeRequest: [() => {
throw error;
}]
}
}), errorString);
});

test('catches beforeRequest errors', async t => {
test('catches beforeRedirect thrown errors', async t => {
await t.throwsAsync(() => got(`${s.url}/redirect`, {
hooks: {
beforeRedirect: [() => {
throw error;
}]
}
}), errorString);
});

test('catches beforeRetry thrown errors', async t => {
await t.throwsAsync(() => got(`${s.url}/retry`, {
hooks: {
beforeRetry: [() => {
throw error;
}]
}
}), errorString);
});

test('catches afterResponse thrown errors', async t => {
await t.throwsAsync(() => got(s.url, {
hooks: {
afterResponse: [() => {
throw error;
}]
}
}), errorString);
});

test('throws a helpful error when passing async function as init hook', async t => {
await t.throwsAsync(() => got(s.url, {
hooks: {
init: [() => Promise.resolve()]
}
}), 'The `init` hook must be a synchronous function');
});

test('catches beforeRequest promise rejections', async t => {
await t.throwsAsync(() => got(s.url, {
hooks: {
beforeRequest: [() => Promise.reject(error)]
}
}), errorString);
});

test('catches beforeRedirect errors', async t => {
test('catches beforeRedirect promise rejections', async t => {
await t.throwsAsync(() => got(`${s.url}/redirect`, {
hooks: {
beforeRedirect: [() => Promise.reject(error)]
}
}), errorString);
});

test('catches beforeRetry errors', async t => {
test('catches beforeRetry promise rejections', async t => {
await t.throwsAsync(() => got(`${s.url}/retry`, {
hooks: {
beforeRetry: [() => Promise.reject(error)]
}
}), errorString);
});

test('catches afterResponse errors', async t => {
test('catches afterResponse promise rejections', async t => {
await t.throwsAsync(() => got(s.url, {
hooks: {
afterResponse: [() => Promise.reject(error)]
}
}), errorString);
});

test('init', async t => {
test('init is called with options', async t => {
await got(s.url, {
json: true,
hooks: {
Expand All @@ -160,7 +197,7 @@ test('init allows modifications', async t => {
t.is(body, 'foobar');
});

test('beforeRequest', async t => {
test('beforeRequest is called with options', async t => {
await got(s.url, {
json: true,
hooks: {
Expand Down Expand Up @@ -188,7 +225,7 @@ test('beforeRequest allows modifications', async t => {
t.is(body.foo, 'bar');
});

test('beforeRedirect', async t => {
test('beforeRedirect is called with options', async t => {
await got(`${s.url}/redirect`, {
json: true,
hooks: {
Expand Down Expand Up @@ -216,15 +253,17 @@ test('beforeRedirect allows modifications', async t => {
t.is(body.foo, 'bar');
});

test('beforeRetry', async t => {
test('beforeRetry is called with options', async t => {
await got(`${s.url}/retry`, {
json: true,
retry: 1,
throwHttpErrors: false,
hooks: {
beforeRetry: [
options => {
(options, error, retryCount) => {
t.is(options.hostname, 'localhost');
t.truthy(error);
t.true(retryCount >= 1);
}
]
}
Expand All @@ -245,7 +284,7 @@ test('beforeRetry allows modifications', async t => {
t.is(body.foo, 'bar');
});

test('afterResponse', async t => {
test('afterResponse is called with response', async t => {
await got(`${s.url}`, {
json: true,
hooks: {
Expand Down

0 comments on commit 98f2872

Please sign in to comment.