This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace body-parser dependency with body and qs
- Loading branch information
1 parent
edc47ec
commit 1890f63
Showing
5 changed files
with
173 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
var qs = require('qs'); | ||
var bytes = require('bytes'); | ||
var anyBody = require('body/any'); | ||
var formBody = require('body/form'); | ||
|
||
var defaultSizeLimit = '200kb'; | ||
|
||
function queryStringParser(text, callback) { | ||
callback(null, qs.parse(text)); | ||
} | ||
|
||
function handleBodyFn(options, bodyFn) { | ||
options = options || {}; | ||
|
||
options.querystring = { parse: queryStringParser }; | ||
options.limit = bytes.parse(options.limit || defaultSizeLimit); | ||
|
||
return function (req, res, next) { | ||
bodyFn(req, res, options, function (err, parsedBody) { | ||
req.body = parsedBody || req.body || {}; | ||
next(); | ||
}); | ||
}; | ||
} | ||
|
||
module.exports = { | ||
/** | ||
* Middleware that forces a default | ||
* req.body object (null object). | ||
*/ | ||
forceDefaultBody: function () { | ||
return function forceDefaultBodyMiddleware(req, res, next) { | ||
req.body = req.body || {}; | ||
next(); | ||
}; | ||
}, | ||
|
||
/** | ||
* Middleware for parsing a form (query string) | ||
* encoded request body. | ||
*/ | ||
form: function formParserMiddleware(options) { | ||
return handleBodyFn(options, formBody); | ||
}, | ||
|
||
/** | ||
* Middleware for parsing either a form | ||
* (query string) or JSON encoded request body. | ||
*/ | ||
formOrJson: function formOrJsonParserMiddleware(options) { | ||
return handleBodyFn(options, anyBody); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
|
||
var async = require('async'); | ||
var assert = require('assert'); | ||
var express = require('express'); | ||
var request = require('supertest'); | ||
|
||
var bodyParser = require('../../lib/helpers').bodyParser; | ||
|
||
describe('bodyParser', function () { | ||
var app, server, host; | ||
|
||
before(function (done) { | ||
app = express(); | ||
|
||
server = app.listen(function () { | ||
var address = server.address().address === '::' ? 'http://localhost' : server.address().address; | ||
|
||
address = address === '0.0.0.0' ? 'http://localhost' : address; | ||
host = address + ':' + server.address().port; | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
describe('forceDefaultBody middleware', function () { | ||
it('can force default body on request', function (done) { | ||
var resultRequestBody; | ||
|
||
app.get('/test/default-body', bodyParser.forceDefaultBody(), function (req, res, next) { | ||
resultRequestBody = req.body; | ||
res.status(200).end(); | ||
next(); | ||
}); | ||
|
||
request(host) | ||
.get('/test/default-body') | ||
.expect(200) | ||
.end(function () { | ||
assert.equal(typeof resultRequestBody, 'object'); | ||
assert.deepEqual(resultRequestBody, {}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('form middlware', function () { | ||
it('can handle form request', function (done) { | ||
var resultRequestBody; | ||
|
||
app.get('/test/form', bodyParser.form(), function (req, res, next) { | ||
resultRequestBody = req.body; | ||
res.status(200).end(); | ||
next(); | ||
}); | ||
|
||
request(host) | ||
.get('/test/form') | ||
.send('test=abc') | ||
.expect(200) | ||
.end(function () { | ||
assert.equal(typeof resultRequestBody, 'object'); | ||
assert.deepEqual(resultRequestBody, { test: 'abc' }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('formOrJson middleware', function () { | ||
it('can handle form and json requests', function (done) { | ||
var resultRequestBody; | ||
|
||
app.post('/test/form-and-json', bodyParser.formOrJson(), function (req, res, next) { | ||
resultRequestBody = req.body; | ||
res.status(200).end(); | ||
next(); | ||
}); | ||
|
||
async.series([ | ||
function (callback) { | ||
request(host) | ||
.post('/test/form-and-json') | ||
.send('test1=abc1') | ||
.expect(200) | ||
.end(function () { | ||
assert.equal(typeof resultRequestBody, 'object'); | ||
assert.deepEqual(resultRequestBody, { test1: 'abc1' }); | ||
callback(); | ||
}); | ||
}, | ||
function (callback) { | ||
request(host) | ||
.post('/test/form-and-json') | ||
.type('json') | ||
.send({ test2: 'abc2' }) | ||
.expect(200) | ||
.end(function () { | ||
assert.equal(typeof resultRequestBody, 'object'); | ||
assert.deepEqual(resultRequestBody, { test2: 'abc2' }); | ||
callback(); | ||
}); | ||
} | ||
], done); | ||
}); | ||
}); | ||
}); |