Skip to content

Commit

Permalink
Remove deprecated bodyParser() combination middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 22, 2023
1 parent 42de1bf commit 4d7b821
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 181 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -3,6 +3,7 @@

This incorporates all changes after 1.19.1 up to 1.20.2.

* Remove deprecated `bodyParser()` combination middleware
* deps: debug@3.1.0
- Add `DEBUG_HIDE_DATE` environment variable
- Change timer to per-namespace instead of global
Expand Down
32 changes: 3 additions & 29 deletions index.js
Expand Up @@ -6,13 +6,6 @@

'use strict'

/**
* Module dependencies.
* @private
*/

var deprecate = require('depd')('body-parser')

/**
* Cache of loaded parsers.
* @private
Expand All @@ -34,8 +27,7 @@ var parsers = Object.create(null)
* @type {Parsers}
*/

exports = module.exports = deprecate.function(bodyParser,
'bodyParser: use individual json/urlencoded middlewares')
exports = module.exports = bodyParser

/**
* JSON parser.
Expand Down Expand Up @@ -90,26 +82,8 @@ Object.defineProperty(exports, 'urlencoded', {
* @public
*/

function bodyParser (options) {
// use default type for parsers
var opts = Object.create(options || null, {
type: {
configurable: true,
enumerable: true,
value: undefined,
writable: true
}
})

var _urlencoded = exports.urlencoded(opts)
var _json = exports.json(opts)

return function bodyParser (req, res, next) {
_json(req, res, function (err) {
if (err) return next(err)
_urlencoded(req, res, next)
})
}
function bodyParser () {
throw new Error('The bodyParser() generic has been split into individual middleware to use instead.')
}

/**
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -12,7 +12,6 @@
"bytes": "3.1.2",
"content-type": "~1.0.5",
"debug": "3.1.0",
"depd": "2.0.0",
"destroy": "1.2.0",
"http-errors": "2.0.0",
"iconv-lite": "0.5.2",
Expand Down
154 changes: 3 additions & 151 deletions test/body-parser.js
@@ -1,158 +1,10 @@

var http = require('http')
var methods = require('methods')
var request = require('supertest')
var assert = require('assert')

var bodyParser = require('..')

describe('bodyParser()', function () {
before(function () {
this.server = createServer()
})

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

it('should parse JSON', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should parse x-www-form-urlencoded', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
.expect(200, '{"user":"tobi"}', done)
})

it('should handle duplicated middleware', function (done) {
var _bodyParser = bodyParser()
var server = http.createServer(function (req, res) {
_bodyParser(req, res, function (err0) {
_bodyParser(req, res, function (err1) {
var err = err0 || err1
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(req.body))
})
})
})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

describe('http methods', function () {
before(function () {
var _bodyParser = bodyParser()

this.server = http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
if (err) {
res.statusCode = 500
res.end(err.message)
return
}

res.statusCode = req.headers['x-expect-method'] === req.method
? req.body.user === 'tobi'
? 201
: 400
: 405
res.end()
})
})
})

methods.slice().sort().forEach(function (method) {
if (method === 'connect') {
// except CONNECT
return
}

it('should support ' + method.toUpperCase() + ' requests', function (done) {
request(this.server)[method]('/')
.set('Content-Type', 'application/json')
.set('Content-Length', '15')
.set('X-Expect-Method', method.toUpperCase())
.send('{"user":"tobi"}')
.expect(201, done)
})
})
})

describe('with type option', function () {
before(function () {
this.server = createServer({ limit: '1mb', type: 'application/octet-stream' })
})

it('should parse JSON', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should parse x-www-form-urlencoded', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
.expect(200, '{"user":"tobi"}', done)
})
})

describe('with verify option', function () {
it('should apply to json', function (done) {
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x20) throw new Error('no leading space')
}
})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(' {"user":"tobi"}')
.expect(403, '[entity.verify.failed] no leading space', done)
})

it('should apply to urlencoded', function (done) {
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x20) throw new Error('no leading space')
}
})

request(server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send(' user=tobi')
.expect(403, '[entity.verify.failed] no leading space', done)
})
it('should throw an error', function () {
assert.throws(bodyParser, /bodyParser\(\) generic has been split/)
})
})

function createServer (opts) {
var _bodyParser = bodyParser(opts)

return http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err
? ('[' + err.type + '] ' + err.message)
: (JSON.stringify(req.body) || typeof req.body)
)
})
})
}

0 comments on commit 4d7b821

Please sign in to comment.