Skip to content

Commit

Permalink
feat: impl text parser support encoding: false (#64)
Browse files Browse the repository at this point in the history
text parser support with no decoder,
to get buffer instead of string.
  • Loading branch information
killagu authored and dead-horse committed May 2, 2018
1 parent 3f40376 commit f65a2d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function(req, opts){
var len = req.headers['content-length'];
var encoding = req.headers['content-encoding'] || 'identity';
if (len && encoding === 'identity') opts.length = ~~len;
opts.encoding = opts.encoding || 'utf8';
opts.encoding = opts.encoding === undefined ? 'utf8': opts.encoding;
opts.limit = opts.limit || '1mb';

// raw-body returns a Promise when no callback is specified
Expand Down
18 changes: 18 additions & 0 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var request = require('supertest');
var parse = require('..');
var koa = require('koa');
var Buffer = require('buffer').Buffer;

describe('parse.text(req, opts)', function(){
describe('with valid str', function(){
Expand Down Expand Up @@ -52,4 +53,21 @@ describe('parse.text(req, opts)', function(){
.expect(200, done);
});
})

describe('use no encoding', function(){
it('should return raw body when opts.returnRawBody = true', function(done){
var app = koa();

app.use(function *(){
const requestBody = yield parse.text(this, {encoding: false});
this.body = { isBuffer: Buffer.isBuffer(requestBody) };
});

request(app.listen())
.post('/')
.send('Hello World!')
.expect({ isBuffer: true })
.expect(200, done);
});
})
});

0 comments on commit f65a2d8

Please sign in to comment.