Skip to content

Commit

Permalink
Cleanup validation and make it optional. Closes #75
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Aug 26, 2016
1 parent 56eb804 commit df67229
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
42 changes: 40 additions & 2 deletions lib/index.js
Expand Up @@ -2,14 +2,52 @@

// Load modules

const Hoek = require('hoek');
const Joi = require('joi');
const Request = require('./request');
const Response = require('./response');
const Schema = require('./schema');


// Declare internals

const internals = {};


internals.options = Joi.object().keys({
url: Joi.alternatives([
Joi.string(),
Joi.object().keys({
protocol: Joi.string(),
hostname: Joi.string(),
port: Joi.any(),
pathname: Joi.string().required(),
query: Joi.any()
})
])
.required(),
headers: Joi.object(),
payload: Joi.any(),
simulate: {
end: Joi.boolean(),
split: Joi.boolean(),
error: Joi.boolean(),
close: Joi.boolean()
},
authority: Joi.string(),
remoteAddress: Joi.string(),
method: Joi.string(),
validate: Joi.boolean()
});


exports.inject = function (dispatchFunc, options, callback) {

Joi.assert({ dispatchFunc, options, callback }, Schema);
options = (typeof options === 'string' ? { url: options } : options);

if (options.validate !== false) { // Defaults to true

This comment has been minimized.

Copy link
@hueniverse

hueniverse Aug 31, 2016

Author Contributor

Turning validation off is a performance optimization. If you are passing garbage, it's your bug, not the modules' problem.

This comment has been minimized.

Copy link
@Marsup

Marsup Aug 31, 2016

Contributor

That depends how you see it. If you're passing invalid data to inject that is going to be ignored without you knowing, is that ok with you ? I see that as a bug, at most an enhancement, certainly not a breaking change.

This comment has been minimized.

Copy link
@Marsup

Marsup Aug 31, 2016

Contributor

In bassmaster, it's probably broken since hapi 12 (hapijs/hapi#3002). I think you're missing the point, apps are already broken by passing invalid options.

Hoek.assert(typeof dispatchFunc === 'function', 'Invalid dispatch function');
Joi.assert(options, internals.options);
}

const req = new Request(options);
const res = new Response(req, callback);
Expand Down
3 changes: 0 additions & 3 deletions lib/request.js
Expand Up @@ -18,10 +18,7 @@ exports = module.exports = internals.Request = function (options) {

// options: method, url, payload, headers, remoteAddress

options = (typeof options === 'string' ? { url: options } : options);

let url = options.url;

if (typeof url === 'object') {
url = Url.format(url);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "shot",
"description": "Injects a fake HTTP request/response into a node HTTP server",
"version": "3.2.1",
"version": "3.3.0",
"repository": "git://github.com/hapijs/shot",
"main": "lib/index.js",
"keywords": [
Expand Down
21 changes: 14 additions & 7 deletions test/index.js
Expand Up @@ -760,15 +760,12 @@ describe('_read()', () => {

it('errors for invalid input options', (done) => {

try {
expect(() => {

Shot.inject({}, {}, (res) => {});
}
catch (err) {
}).to.throw('Invalid dispatch function');

expect(err).to.exist();
expect(err.isJoi).to.be.true();
done();
}
done();
});

it('errors for missing url', (done) => {
Expand All @@ -795,6 +792,16 @@ describe('_read()', () => {
}
});

it('ignores incorrect simulation object', (done) => {

expect(() => {

Shot.inject((req, res) => { }, { url: '/', simulate: 'sample string', validate: false }, (res) => { });
}).to.not.throw();

done();
});

it('errors for an incorrect simulation object values', (done) => {

try {
Expand Down

0 comments on commit df67229

Please sign in to comment.