Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
73 changes: 38 additions & 35 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down Expand Up @@ -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();
});
});


Expand Down
Loading