Skip to content

Commit

Permalink
adding check for missing content-type header, fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mac- committed Sep 3, 2014
1 parent 3586da7 commit 4781be2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/RouteSchemaManager.js
Expand Up @@ -237,6 +237,9 @@ var RouteSchemaManager = function(options) {
if (!schemas || !schemas[validationTypes.PAYLOAD]) {
return { valid: true };
}
if (!request.raw.req.headers['content-type']) {
return { valid: false, errors: ['unable to validate payload: missing content-type header'] };
}

// convert payload types before validating only if payload is type application/x-www-form-urlencoded
if (request.raw.req.headers['content-type'].indexOf('application/x-www-form-urlencoded') === 0) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"contributors": [
"Mac Angell <mac.ang311@gmail.com>"
],
"version": "0.5.1",
"version": "0.5.2",
"dependencies": {
"async": "0.x.x",
"z-schema": "2.x.x",
Expand Down
46 changes: 46 additions & 0 deletions test/RouteSchemaManager.tests.js
Expand Up @@ -482,6 +482,52 @@ describe('RouteSchemaManager Unit Tests', function() {
done();
});
});

it('should fail validation of payload when no payload is present', function(done) {

var routeSchemaManager = new RouteSchemaManager(rsmConfig),
mockRequest = {
_route: mockRoute1,
payload: null,
raw: {
req: {
headers: {
'content-type': 'application/json'
}
}
}
};
routeSchemaManager.initializeRoutes(mockRoute1.server.info.uri, mockRoutes, function(error) {
assert(!error, 'initialize should not return error');
var report = routeSchemaManager.validatePayload(mockRequest);
assert(!report.valid, 'payload obj should not be valid');
assert(report.errors, 'errors obj should be valid');
done();
});
});

it('should fail validation of payload when no content-type header present', function(done) {

var routeSchemaManager = new RouteSchemaManager(rsmConfig),
mockRequest = {
_route: mockRoute1,
payload: {
string: 'fnord'
},
raw: {
req: {
headers: {}
}
}
};
routeSchemaManager.initializeRoutes(mockRoute1.server.info.uri, mockRoutes, function(error) {
assert(!error, 'initialize should not return error');
var report = routeSchemaManager.validatePayload(mockRequest);
assert(!report.valid, 'payload obj should not be valid');
assert(report.errors, 'errors obj should be valid');
done();
});
});
});

/*
Expand Down
2 changes: 1 addition & 1 deletion test/integration/ratify.tests.js
Expand Up @@ -75,7 +75,7 @@ before(function(done) {



describe('hapi-statsd plugin tests', function() {
describe('ratify plugin tests', function() {

it('should provide a swagger docs end point', function(done) {

Expand Down

0 comments on commit 4781be2

Please sign in to comment.