Skip to content

Commit

Permalink
make empty-body-handling consistent between chunked requests
Browse files Browse the repository at this point in the history
fixes #44
  • Loading branch information
dougwilson committed Sep 6, 2014
1 parent b1f1315 commit 628bc7b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
unreleased
==========

* make empty-body-handling consistent between chunked requests
- empty `json` produces `{}`
- empty `raw` produces `new Buffer(0)`
- empty `text` produces `''`
- empty `urlencoded` produces `{}`
* deps: [email protected]
- Fix issue where first empty value in array is discarded
* deps: type-is@~1.5.0
- fix `hasbody` to be true for `content-length: 0`

1.7.0 / 2014-09-01
==================
Expand Down
7 changes: 4 additions & 3 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ function json(options) {
}

function parse(body) {
if (0 === body.length) {
throw new Error('invalid json, empty body')
if (body.length === 0) {
// special-case empty json body, as it's a common client-side mistake
// TODO: maybe make this configurable or part of "strict" option
return {}
}

if (strict) {
Expand Down Expand Up @@ -101,7 +103,6 @@ function json(options) {


function firstchar(str) {
if (!str) return ''
var match = firstcharRegExp.exec(str)
return match ? match[1] : ''
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"on-finished": "2.1.0",
"qs": "2.2.3",
"raw-body": "1.3.0",
"type-is": "~1.3.2"
"type-is": "~1.5.0"
},
"devDependencies": {
"istanbul": "0.3.2",
Expand Down
18 changes: 9 additions & 9 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,34 @@ describe('bodyParser.json()', function(){
.expect(200, '{}', done)
})

it('should handle no message-body', function(done){
it('should handle empty message-body', function(done){
var server = createServer()

request(server)
.get('/')
.set('Content-Type', 'application/json')
.unset('Transfer-Encoding')
.set('Transfer-Encoding', 'chunked')
.expect(200, '{}', done)
})

it('should 400 on malformed JSON', function(done){
it('should handle no message-body', function(done){
var server = createServer()

request(server)
.post('/')
.get('/')
.set('Content-Type', 'application/json')
.send('{"foo')
.expect(400, done);
.unset('Transfer-Encoding')
.expect(200, '{}', done)
})

it('should 400 when no body is given', function(done){
it('should 400 on malformed JSON', function(done){
var server = createServer()

request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('Transfer-Encoding', 'chunked')
.expect(400, 'invalid json, empty body', done)
.send('{"foo')
.expect(400, done);
})

it('should 400 when invalid content-length', function(done){
Expand Down
8 changes: 8 additions & 0 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('bodyParser.raw()', function(){
test.expect(400, /content length/, done)
})

it('should handle Content-Length: 0', function(done){
request(server)
.post('/')
.set('Content-Type', 'application/octet-stream')
.set('Content-Length', '0')
.expect(200, 'buf:', done)
})

it('should handle empty message-body', function(done){
request(server)
.post('/')
Expand Down
10 changes: 10 additions & 0 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ describe('bodyParser.text()', function(){
test.expect(400, /content length/, done)
})

it('should handle Content-Length: 0', function(done){
var server = createServer({ limit: '1kb' })

request(server)
.post('/')
.set('Content-Type', 'text/plain')
.set('Content-Length', '0')
.expect(200, '""', done)
})

it('should handle empty message-body', function(done){
var server = createServer({ limit: '1kb' })

Expand Down
9 changes: 9 additions & 0 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ describe('bodyParser.urlencoded()', function(){
test.expect(400, /content length/, done)
})

it('should handle Content-Length: 0', function(done){
request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Content-Length', '0')
.send('')
.expect(200, '{}', done)
})

it('should handle empty message-body', function(done){
var server = createServer({ limit: '1kb' })

Expand Down

0 comments on commit 628bc7b

Please sign in to comment.