Skip to content

Commit

Permalink
Merge pull request #29 from dschenkelman/allowNoContentHeader
Browse files Browse the repository at this point in the history
Allow no content header if payload it empty
  • Loading branch information
mac- committed Dec 4, 2014
2 parents 8ee9d97 + 475b946 commit a3e8e00
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/RouteSchemaManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,17 @@ 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'] };

var headers = request.raw.req.headers;

if (!headers['content-type'] && request.payload &&
!(typeof request.payload === 'object' &&
Object.keys(request.payload).length === 0)){
return { valid: false, errors: ['unable to validate payload: missing content-type header and had content'] };
}

// 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) {
if (headers['content-type'] && headers['content-type'].indexOf('application/x-www-form-urlencoded') === 0) {
convertPropertyTypesToMatchSchema(request.payload, schemas[validationTypes.PAYLOAD]);
}

Expand Down
36 changes: 35 additions & 1 deletion test/RouteSchemaManager.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ var assert = require('assert'),
}
}
},
mockRoute6 = {
method: 'POST',
path: '/good/fnord/allow/empty/body',
server: {
info: {
uri: 'http://fnord.com'
}
},
settings: {
plugins: {
ratify: {
payload: {
type: [ 'object', 'null' ]
}
}
}
}
},
mockRoutes = [mockRoute1, mockRoute2, mockRoute4];

describe('RouteSchemaManager Unit Tests', function() {
Expand Down Expand Up @@ -509,7 +527,7 @@ describe('RouteSchemaManager Unit Tests', function() {
assert(report.errors, 'errors obj should be valid');
});

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

var routeSchemaManager = new RouteSchemaManager(rsmConfig),
mockRequest = {
Expand All @@ -528,6 +546,22 @@ describe('RouteSchemaManager Unit Tests', function() {
assert(!report.valid, 'payload obj should not be valid');
assert(report.errors, 'errors obj should be valid');
});

it('should pass validation of payload when no content-type header present if there is no content', function() {

var routeSchemaManager = new RouteSchemaManager(rsmConfig),
mockRequest = {
_route: mockRoute6,
raw: {
req: {
headers: {}
}
}
};
routeSchemaManager.initializeRoutes(mockRoute6.server.info.uri, mockRoutes);
var report = routeSchemaManager.validatePayload(mockRequest);
assert(report.valid, 'payload obj should be valid');
});
});

/*
Expand Down

0 comments on commit a3e8e00

Please sign in to comment.