Skip to content

Commit

Permalink
Fix merging default & custom handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Sep 3, 2018
1 parent 9c50d26 commit 5f191b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
12 changes: 6 additions & 6 deletions source/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const merge = require('./merge');
const deepFreeze = require('./deep-freeze');
const mergeInstances = require('./merge-instances');

const next = options => options.stream ? asStream(options) : asPromise(options);
const getPromiseOrStream = options => options.stream ? asStream(options) : asPromise(options);
const mergeOptions = (defaults, options = {}) => merge({}, defaults, options);

const aliases = [
Expand All @@ -23,12 +23,14 @@ const create = defaults => {
defaults = merge({}, defaults);
defaults.options = normalizeArguments.preNormalize(defaults.options);
if (!defaults.handler) {
defaults.handler = next;
// This can't be getPromiseOrStream, because when merging
// the chain would stop at this point and no further handlers would be called.
defaults.handler = (options, next) => next(options);
}

function got(url, options) {
try {
return defaults.handler(normalizeArguments(url, options, defaults), next);
return defaults.handler(normalizeArguments(url, options, defaults), getPromiseOrStream);
} catch (error) {
if (options && options.stream) {
throw error;
Expand All @@ -46,9 +48,7 @@ const create = defaults => {

got.mergeInstances = (...args) => create(mergeInstances(args));

got.stream = (url, options) => {
return defaults.handler(normalizeArguments(url, {...options, stream: true}, defaults), next);
};
got.stream = (url, options) => got(url, {...options, stream: true});

for (const method of aliases) {
got[method] = (url, options) => got(url, {...options, method});
Expand Down
33 changes: 32 additions & 1 deletion test/merge-instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,38 @@ test('merging instances', async t => {
t.not(headers['user-agent'], undefined);
});

test('merging already merged instances & another instance', async t => {
test('works even if no default handler in the end', async t => {
const instanceA = got.create({
options: {},
handler: (options, next) => next(options)
});

const instanceB = got.create({
options: {},
handler: (options, next) => next(options)
});

const merged = got.mergeInstances(instanceA, instanceB);
await t.notThrows(() => merged(s.url));
});

test('merges default handlers & custom handlers', async t => {
const instanceA = got.extend({headers: {unicorn: 'rainbow'}});
const instanceB = got.create({
options: {},
handler: (options, next) => {
options.headers.cat = 'meow';
return next(options);
}
});
const merged = got.mergeInstances(instanceA, instanceB);

const {body: headers} = await merged(s.url, {json: true});
t.is(headers.unicorn, 'rainbow');
t.is(headers.cat, 'meow');
});

test('merging one group & one instance', async t => {
const instanceA = got.extend({headers: {dog: 'woof'}});
const instanceB = got.extend({headers: {cat: 'meow'}});
const instanceC = got.extend({headers: {bird: 'tweet'}});
Expand Down

0 comments on commit 5f191b9

Please sign in to comment.