Skip to content

Commit

Permalink
Remove req.body initialization to {}
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Dec 16, 2021
1 parent 6467f4c commit 6496e05
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
@@ -1,6 +1,8 @@
2.x
===

* `req.body` is no longer always initialized to `{}`
- it is left `undefined` unless a body is parsed
* `urlencoded` parser now defaults `extended` to `false`
* Use `on-finished` to determine when body read

Expand Down
4 changes: 3 additions & 1 deletion lib/types/json.js
Expand Up @@ -103,7 +103,9 @@ function json (options) {
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/types/raw.js
Expand Up @@ -60,7 +60,9 @@ function raw (options) {
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/types/text.js
Expand Up @@ -62,7 +62,9 @@ function text (options) {
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/types/urlencoded.js
Expand Up @@ -78,7 +78,9 @@ function urlencoded (options) {
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
6 changes: 3 additions & 3 deletions test/body-parser.js
Expand Up @@ -10,10 +10,10 @@ describe('bodyParser()', function () {
this.server = createServer()
})

it('should default to {}', function (done) {
it('should default req.body to undefined', function (done) {
request(this.server)
.post('/')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})

it('should parse JSON', function (done) {
Expand Down Expand Up @@ -149,7 +149,7 @@ function createServer (opts) {
return http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(req.body))
res.end(err ? err.message : (JSON.stringify(req.body) || typeof req.body))
})
})
}
8 changes: 4 additions & 4 deletions test/json.js
Expand Up @@ -36,7 +36,7 @@ describe('bodyParser.json()', function () {
.get('/')
.set('Content-Type', 'application/json')
.unset('Transfer-Encoding')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})

it('should 400 when invalid content-length', function (done) {
Expand Down Expand Up @@ -306,7 +306,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -338,7 +338,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/x-json')
.send('{"user":"tobi"}')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -653,7 +653,7 @@ function createServer (opts) {
res.end(err[req.headers['x-error-property'] || 'message'])
} else {
res.statusCode = 200
res.end(JSON.stringify(req.body))
res.end(JSON.stringify(req.body) || typeof req.body)
}
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/raw.js
Expand Up @@ -168,7 +168,7 @@ describe('bodyParser.raw()', function () {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/octet-stream')
test.write(Buffer.from('000102', 'hex'))
test.expect(200, '{}', done)
test.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -197,7 +197,7 @@ describe('bodyParser.raw()', function () {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/x-foo')
test.write(Buffer.from('000102', 'hex'))
test.expect(200, '{}', done)
test.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -375,7 +375,7 @@ function createServer (opts) {
return
}

res.end(JSON.stringify(req.body))
res.end(JSON.stringify(req.body) || typeof req.body)
})
})
}
6 changes: 3 additions & 3 deletions test/text.js
Expand Up @@ -189,7 +189,7 @@ describe('bodyParser.text()', function () {
.post('/')
.set('Content-Type', 'text/plain')
.send('user is tobi')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -219,7 +219,7 @@ describe('bodyParser.text()', function () {
.post('/')
.set('Content-Type', 'text/xml')
.send('<user>tobi</user>')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -433,7 +433,7 @@ function createServer (opts) {
return http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(req.body))
res.end(err ? err.message : (JSON.stringify(req.body) || typeof req.body))
})
})
}
6 changes: 3 additions & 3 deletions test/urlencoded.js
Expand Up @@ -435,7 +435,7 @@ describe('bodyParser.urlencoded()', function () {
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -467,7 +467,7 @@ describe('bodyParser.urlencoded()', function () {
.post('/')
.set('Content-Type', 'application/x-foo')
.send('user=tobi')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -728,7 +728,7 @@ function createServer (opts) {
res.end(err[req.headers['x-error-property'] || 'message'])
} else {
res.statusCode = 200
res.end(JSON.stringify(req.body))
res.end(JSON.stringify(req.body) || typeof req.body)
}
})
})
Expand Down

0 comments on commit 6496e05

Please sign in to comment.