Skip to content

Commit

Permalink
Merge pull request #690 from cliffano/master
Browse files Browse the repository at this point in the history
Fix JSON leading whitespaces handling in strict mode.
  • Loading branch information
tj committed Nov 9, 2012
2 parents 0943ca9 + 0737981 commit 9fe77f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/middleware/json.js
Expand Up @@ -65,7 +65,8 @@ exports = module.exports = function(options){
req.setEncoding('utf8');
req.on('data', function(chunk){ buf += chunk });
req.on('end', function(){
if (strict && '{' != buf[0] && '[' != buf[0]) return next(utils.error(400, 'invalid json'));
var first = buf.trim()[0];
if (strict && '{' != first && '[' != first) return next(utils.error(400, 'invalid json'));
try {
req.body = JSON.parse(buf, options.reviver);
next();
Expand Down
19 changes: 19 additions & 0 deletions test/json.js
Expand Up @@ -122,6 +122,25 @@ describe('connect.json()', function(){
done();
});
})

it('should allow leading whitespaces in JSON', function(done){
var app = connect();
app.use(connect.json({ strict: true }));

app.use(function(req, res){
res.end(JSON.stringify(req.body));
});

app.request()
.post('/')
.set('Content-Type', 'application/json')
.write(' { "user": "tobi" }')
.end(function(res){
res.should.have.status(200);
res.body.should.include('{"user":"tobi"}');
done();
});
})
})

describe('by default', function(){
Expand Down

0 comments on commit 9fe77f4

Please sign in to comment.