Skip to content

Commit

Permalink
Refactor invalid header handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaman committed Oct 22, 2018
1 parent 5d96d03 commit c78cc55
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ exports.METHOD_NOT_FOUND = METHOD_NOT_FOUND;
exports.INVALID_PARAMS = INVALID_PARAMS;
exports.SERVER_ERROR = SERVER_ERROR;
exports.SERVER_ERROR_MAX = SERVER_ERROR_MAX;
exports.INVALID_ACCEPT_HEADER_MSG = 'Accept header must include application/json';
exports.INVALID_CONTENT_LENGTH_MSG = 'Invalid Content-Length header';
13 changes: 8 additions & 5 deletions lib/reqhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ function checkRequest(req, path) {
const trimmedValue = value.trim();
return trimmedValue === 'application/json' || trimmedValue.startsWith('application/json;');
}))) {
err = { statusCode: 400, message: 'Accept header must include application/json' };
} else if (!('content-length' in req.headers)) {
err = { statusCode: 400, message: 'Missing Content-Length header' };
err = { statusCode: 400, message: consts.INVALID_ACCEPT_HEADER_MSG };
} else {
const reqContentLength = parseInt(req.headers['content-length'], 10);
if (Number.isNaN(reqContentLength) || reqContentLength < 0) {
err = { statusCode: 400, message: 'Invalid Content-Length header' };
err = { statusCode: 400, message: consts.INVALID_CONTENT_LENGTH_MSG };
}
}
return err;
Expand All @@ -59,12 +57,17 @@ function reqHandler(req, res) {
return;
}

res.setHeader('Content-Type', 'application/json');
const body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
const bodyStr = Buffer.concat(body).toString();
const reqContentLength = parseInt(req.headers['content-length'], 10);

if (bodyStr.length !== reqContentLength) {
sendError(res, 400, consts.INVALID_CONTENT_LENGTH_MSG);
return;
}

res.setHeader('Content-Type', 'application/json');

Expand Down
6 changes: 5 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const assert = require('assert');
const request = require('supertest');
const RpcServer = require('../lib/http-jsonrpc-server');
const consts = require('../lib/consts');

function sum(arr) {
let total = 0;
Expand Down Expand Up @@ -140,7 +141,10 @@ describe('handling requests', () => {
it('should 400 requests that do not accept application/json', () => request(rpcServer.server)
.post('/')
.set('Content-Type', 'application/json')
.expect(400));
.expect(400)
.then((response) => {
assert.strictEqual(response.body.error, consts.INVALID_ACCEPT_HEADER_MSG);
}));

it('should error invalid json', () => testRequest({ server: rpcServer.server, body: 'asdlkfjasld' })
.then((response) => {
Expand Down

0 comments on commit c78cc55

Please sign in to comment.