diff --git a/lib/index.js b/lib/index.js index 1d21968..c752be1 100755 --- a/lib/index.js +++ b/lib/index.js @@ -38,18 +38,13 @@ internals.options = Validate.object().keys({ }); -exports.inject = function (dispatchFunc, options) { +exports.inject = async function (dispatchFunc, options) { // eslint-disable-line require-await options = (typeof options === 'string' ? { url: options } : options); - if (options.validate !== false) { // Defaults to true - try { - Hoek.assert(typeof dispatchFunc === 'function', 'Invalid dispatch function'); - Validate.assert(options, internals.options); - } - catch (err) { - return Promise.reject(err); - } + if (options?.validate !== false) { // Defaults to true + Hoek.assert(typeof dispatchFunc === 'function', 'Invalid or missing dispatch function'); + Validate.assert(options ?? null, internals.options, 'Invalid options:'); } return new Promise((resolve) => { diff --git a/test/index.js b/test/index.js index 74f42bc..e4083f0 100755 --- a/test/index.js +++ b/test/index.js @@ -537,6 +537,44 @@ describe('inject()', () => { }); expect(res.payload.toString()).to.equal(body.toString()); }); + + it('errors for invalid input options', async () => { + + await expect(Shot.inject()).to.reject('Invalid or missing dispatch function'); + await expect(Shot.inject({}, {})).to.reject('Invalid or missing dispatch function'); + await expect(Shot.inject(Hoek.ignore)).to.reject(/^Invalid options:/); + await expect(Shot.inject(Hoek.ignore, true)).to.reject(/^Invalid options:/); + }); + + it('errors for missing url', async () => { + + const err = await expect(Shot.inject((req, res) => { }, {})).to.reject(/^Invalid options:/); + expect(err.isJoi).to.be.true(); + }); + + it('errors for an incorrect simulation object', async () => { + + const err = await expect(Shot.inject((req, res) => { }, { url: '/', simulate: 'sample string' })).to.reject(); + expect(err.isJoi).to.be.true(); + }); + + it('ignores incorrect simulation object', async () => { + + const dispatch = function (req, res) { + + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end(req.headers.super); + }; + + const res = await Shot.inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', headers: { Super: 'duper' }, simulate: 'sample string', validate: false }); + expect(res.payload).to.equal('duper'); + }); + + it('errors for an incorrect simulation object values', async () => { + + const err = await expect(Shot.inject((req, res) => { }, { url: '/', simulate: { end: 'wrong input' } })).to.reject(); + expect(err.isJoi).to.be.true(); + }); }); describe('writeHead()', () => { @@ -791,41 +829,6 @@ describe('_read()', () => { expect(res.payload).to.equal('close'); expect(events).to.equal(['finish (res)', 'close (req)', 'close (res)']); }); - - it('errors for invalid input options', async () => { - - await expect(Shot.inject({}, {})).to.reject('Invalid dispatch function'); - }); - - it('errors for missing url', async () => { - - const err = await expect(Shot.inject((req, res) => { }, {})).to.reject(); - expect(err.isJoi).to.be.true(); - }); - - it('errors for an incorrect simulation object', async () => { - - const err = await expect(Shot.inject((req, res) => { }, { url: '/', simulate: 'sample string' })).to.reject(); - expect(err.isJoi).to.be.true(); - }); - - it('ignores incorrect simulation object', async () => { - - const dispatch = function (req, res) { - - res.writeHead(200, { 'Content-Type': 'text/plain' }); - res.end(req.headers.super); - }; - - const res = await Shot.inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', headers: { Super: 'duper' }, simulate: 'sample string', validate: false }); - expect(res.payload).to.equal('duper'); - }); - - it('errors for an incorrect simulation object values', async () => { - - const err = await expect(Shot.inject((req, res) => { }, { url: '/', simulate: { end: 'wrong input' } })).to.reject(); - expect(err.isJoi).to.be.true(); - }); });