Skip to content

Commit

Permalink
Merge 11e58b5 into bbadc8c
Browse files Browse the repository at this point in the history
  • Loading branch information
destromas1 committed Nov 30, 2016
2 parents bbadc8c + 11e58b5 commit a82e2e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
14 changes: 6 additions & 8 deletions lib/index.js
@@ -1,26 +1,24 @@
"use strict";

var _contentLength =
{
var _contentLength = {
DEFAULT_MAX_LENGTH: 999,
DEFAULT_ERROR_STATUS: 400,
DEFAULT_ERROR_MESSAGE: "Invalid payload; too big."
};

var _validateMax = function(opts)
{
var _validateMax = function (opts) {
var _opts = opts || {};

var _maxLength = _opts.max || _contentLength.DEFAULT_MAX_LENGTH;
var _status = _opts.status || _contentLength.DEFAULT_ERROR_STATUS;
var _message = _opts.message || _contentLength.DEFAULT_ERROR_MESSAGE;

var _middleware = function(request, response, next)
{
var _middleware = function(request, response, next) {
var _contentLength = request.headers['content-length'] ? parseInt(request.headers['content-length']) : null;

if (_contentLength > _maxLength)
{
if (_contentLength > _maxLength) {
response.status(_status).json({message: _message});

return;
Expand Down
57 changes: 22 additions & 35 deletions tests/index_test.js
Expand Up @@ -5,12 +5,9 @@ var expect = require('chai').expect;

var ERROR_MESSAGE_DEFAULT = "Invalid payload; too big.";

describe('validator', function()
{
describe('validateMax - default length 999', function()
{
it('should call next - no content-length', function()
{
describe('validator', function(){
describe('validateMax - default length 999', function () {
it('should call next - no content-length', function () {
var _called = false;
var _endCalled = false;
var _req = {headers: {}};
Expand All @@ -21,10 +18,9 @@ describe('validator', function()

expect(_called).to.be.true;
expect(_endCalled).to.be.false;
})
});

it('should not call next - length is bigger than what was expected', function()
{
it('should not call next - length is bigger than what was expected', function () {
var _called = false;
var _endCalled = false;
var _req = {headers: {'content-length': '1000'}};
Expand All @@ -35,10 +31,9 @@ describe('validator', function()

expect(_called).to.be.false;
expect(_endCalled).to.be.true;
})
});

it('should call next correctly', function()
{
it('should call next correctly', function () {
var _called = false;
var _endCalled = false;
var _req = {headers: {'content-length': '999'}};
Expand All @@ -49,13 +44,11 @@ describe('validator', function()

expect(_called).to.be.true;
expect(_endCalled).to.be.false;
})
})
});
});

describe('validateMax - specific content length', function()
{
it('should call next - no content-length', function()
{
describe('validateMax - specific content length', function(){
it('should call next - no content-length', function(){
var _called = false;
var _endCalled = false;
var _req = {headers: {}};
Expand All @@ -66,10 +59,9 @@ describe('validator', function()

expect(_called).to.be.true;
expect(_endCalled).to.be.false;
})
});

it('should not call next, content-length bigger than expected', function()
{
it('should not call next, content-length bigger than expected', function(){
var _called = false;
var _endCalled = false;
var _req = {headers: {'content-length': '101'}};
Expand All @@ -80,10 +72,9 @@ describe('validator', function()

expect(_called).to.be.false;
expect(_endCalled).to.be.true;
})
});

it('should call next correctly', function()
{
it('should call next correctly', function(){
var _called = false;
var _endCalled = false;
var _req = {headers: {'content-length': '99'}};
Expand All @@ -94,13 +85,11 @@ describe('validator', function()

expect(_called).to.be.true;
expect(_endCalled).to.be.false;
})
});
})

describe('status', function()
{
it('should call the status with the correct status', function()
{
describe('status', function(){
it('should call the status with the correct status', function(){
var _called = false;
var _endCalled = false;
var _req = {headers: {'content-length': '101'}};
Expand All @@ -111,13 +100,11 @@ describe('validator', function()

expect(_called).to.be.false;
expect(_endCalled).to.be.true;
})
});
})

describe('message', function()
{
it('should call the json with the correct message', function()
{
describe('message', function(){
it('should call the json with the correct message', function(){
var _called = false;
var _endCalled = false;
var _req = {headers: {'content-length': '101'}};
Expand All @@ -128,6 +115,6 @@ describe('validator', function()

expect(_called).to.be.false;
expect(_endCalled).to.be.true;
})
});
})
})

0 comments on commit a82e2e0

Please sign in to comment.