Skip to content

Commit

Permalink
fix verify function with non-native charsets
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 19, 2014
1 parent 5cdcf63 commit bda6029
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

var getBody = require('raw-body')
var iconv = require('iconv-lite')
var typer = require('media-typer')
var zlib = require('zlib')

Expand Down Expand Up @@ -90,7 +91,7 @@ function read(req, res, next, parse, options) {
// parse
try {
str = typeof body !== 'string'
? body.toString(encoding)
? iconv.decode(body, encoding)
: body
req.body = parse(str)
} catch (err){
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"bytes": "1.0.0",
"depd": "0.3.0",
"iconv-lite": "0.4.3",
"media-typer": "0.2.0",
"qs": "0.6.6",
"raw-body": "1.2.1",
Expand Down
12 changes: 12 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ describe('bodyParser.json()', function(){
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should work with different charsets', function(done){
var server = createServer({verify: function(req, res, buf){
if (buf[0] === 0x5b) throw new Error('no arrays')
}})

var test = request(server).post('/')
test.set('Content-Type', 'application/json; charset=utf-16')
test.write(new Buffer('feff007b0022006e0061006d00650022003a00228bba0022007d', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

})

describe('charset', function(){
Expand Down

0 comments on commit bda6029

Please sign in to comment.