From 6496e058059f4a74b9b537e4ab57b9f9cde42757 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 15 Sep 2014 18:00:43 -0700 Subject: [PATCH] Remove req.body initialization to {} --- HISTORY.md | 2 ++ lib/types/json.js | 4 +++- lib/types/raw.js | 4 +++- lib/types/text.js | 4 +++- lib/types/urlencoded.js | 4 +++- test/body-parser.js | 6 +++--- test/json.js | 8 ++++---- test/raw.js | 6 +++--- test/text.js | 6 +++--- test/urlencoded.js | 6 +++--- 10 files changed, 30 insertions(+), 20 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index a198c33f..35f5009a 100644 --- a/HISTORY.md +++ b/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 diff --git a/lib/types/json.js b/lib/types/json.js index 3436a511..72663efa 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -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)) { diff --git a/lib/types/raw.js b/lib/types/raw.js index 62063e89..bfe274cf 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -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)) { diff --git a/lib/types/text.js b/lib/types/text.js index fc478f7e..b153931b 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -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)) { diff --git a/lib/types/urlencoded.js b/lib/types/urlencoded.js index 468757ad..f4ba2cd0 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -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)) { diff --git a/test/body-parser.js b/test/body-parser.js index e32cf94d..8b3fdd83 100644 --- a/test/body-parser.js +++ b/test/body-parser.js @@ -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) { @@ -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)) }) }) } diff --git a/test/json.js b/test/json.js index ae745712..13046a82 100644 --- a/test/json.js +++ b/test/json.js @@ -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) { @@ -306,7 +306,7 @@ describe('bodyParser.json()', function () { .post('/') .set('Content-Type', 'application/json') .send('{"user":"tobi"}') - .expect(200, '{}', done) + .expect(200, 'undefined', done) }) }) @@ -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) }) }) @@ -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) } }) }) diff --git a/test/raw.js b/test/raw.js index 36dc1ef6..31bb51b7 100644 --- a/test/raw.js +++ b/test/raw.js @@ -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) }) }) @@ -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) }) }) @@ -375,7 +375,7 @@ function createServer (opts) { return } - res.end(JSON.stringify(req.body)) + res.end(JSON.stringify(req.body) || typeof req.body) }) }) } diff --git a/test/text.js b/test/text.js index 01781d7f..bc359cad 100644 --- a/test/text.js +++ b/test/text.js @@ -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) }) }) @@ -219,7 +219,7 @@ describe('bodyParser.text()', function () { .post('/') .set('Content-Type', 'text/xml') .send('tobi') - .expect(200, '{}', done) + .expect(200, 'undefined', done) }) }) @@ -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)) }) }) } diff --git a/test/urlencoded.js b/test/urlencoded.js index 53fed84a..69ad7a1f 100644 --- a/test/urlencoded.js +++ b/test/urlencoded.js @@ -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) }) }) @@ -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) }) }) @@ -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) } }) })