diff --git a/source/create.js b/source/create.js index 307e25685..5ff4b1252 100644 --- a/source/create.js +++ b/source/create.js @@ -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 = [ @@ -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; @@ -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}); diff --git a/test/merge-instances.js b/test/merge-instances.js index ba9b7b92e..fb00951fc 100644 --- a/test/merge-instances.js +++ b/test/merge-instances.js @@ -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'}});