Skip to content

Commit

Permalink
#882 Corrects misspelling and provides unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhyder committed Dec 2, 2015
1 parent 69cb975 commit 56cf825
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
16 changes: 13 additions & 3 deletions include/http/request_handler.js
Expand Up @@ -1438,15 +1438,25 @@ module.exports = function RequestHandlerModule(pb) {
* @param {Function} prototype A prototype that can have an instance created and parse the specified mime type
* @return {Boolean} TRUE if the body parser was registered, FALSE if not
*/
RequestHandler.registerBodyParser = function(mime, protoype) {
if (!pb.validation.isNonEmptyStr(mime) || !util.isFunction(prototype)) {
RequestHandler.registerBodyParser = function(mime, prototype) {
if (!pb.validation.isNonEmptyStr(mime, true) || !util.isFunction(prototype)) {
return false;
}

//set the prototype handler
BODY_PARSER_MAP[mime] = protoype;
BODY_PARSER_MAP[mime] = prototype;
return true;
};

/**
* Retrieves the body parser mapping
* @static
* @method getBodyParsers
* @return {Object} MIME string as the key and parser as the value
*/
RequestHandler.getBodyParsers = function() {
return util.merge(BODY_PARSER_MAP, {});
};

return RequestHandler;
};
65 changes: 65 additions & 0 deletions test/include/http/request_handler_tests.js
@@ -0,0 +1,65 @@

//dependencies
var should = require('should');
var Configuration = require('../../../include/config.js');
var Lib = require('../../../lib');

describe('RequestHandler', function(){

var pb = null;
var RequestHandler = null;
before('Initialize the Environment with the default configuration', function() {

//travis gets slow so we bump the timeout just a little here to get around the BS
this.timeout(10000);

pb = new Lib(Configuration.getBaseConfig());
RequestHandler = pb.RequestHandler;
});

describe('RequestHandler.getBodyParsers', function() {

it('should return the default list of body parsers', function() {
var result = RequestHandler.getBodyParsers();
result.should.be.type('object');
result['application/json'].should.be.type('function');
result['application/x-www-form-urlencoded'].should.be.type('function');
result['multipart/form-data'].should.be.type('function');
Object.keys(result).length.should.be.exactly(3);
});
});

describe('RequestHandler.registerBodyParser', function(){

[null, undefined, ''].forEach(function(mime){

it('should return false when an invalid mime '+mime+' is provided', function(){
var result = RequestHandler.registerBodyParser(mime, function(){});
result.should.be.false;
});
});

[null, undefined, '', false, true, 1, 10.0, {}].forEach(function(parser){

it('should return false when an invalid parser '+parser+' is provided', function(){
var result = RequestHandler.registerBodyParser('application/xml', parser);
result.should.be.false;
});
});

it('should replace the default parser when supplied with a mime that is already registered', function(){

var parsers = RequestHandler.getBodyParsers();
var originalJsonParser = parsers['application/json'];
originalJsonParser.should.be.type('function');

var newParser = function(){};
var result = RequestHandler.registerBodyParser('application/json', newParser);
result.should.be.true;

parsers = RequestHandler.getBodyParsers();
parsers['application/json'].should.not.eql(originalJsonParser);
parsers['application/json'].should.eql(newParser);
});
});
});

0 comments on commit 56cf825

Please sign in to comment.