Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var fs = require('fs');
var qs = require('qs');
var zlib = require('zlib');
var util = require('util');
var superagentparse = require('superagentparse');

/**
* Expose the request function.
Expand Down Expand Up @@ -511,6 +512,19 @@ Request.prototype.parse = function(fn){
return this;
};


/**
* Support multi encodings.
*
* @param {String} encoding. 'big5', 'gbk' etc.
* @return {Request} for chaining
* @api public
*/
Request.prototype.decode = function (encoding) {
this.parse(superagentparse(encoding));
return this;
};

/**
* Redirect to `url
*
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@
"url": "git://github.com/visionmedia/superagent.git"
},
"dependencies": {
"qs": "0.6.6",
"formidable": "1.0.14",
"mime": "1.2.5",
"component-emitter": "1.1.2",
"methods": "0.0.1",
"cookiejar": "1.3.0",
"debug": "~0.7.2",
"extend": "~1.2.1",
"formidable": "1.0.14",
"methods": "0.0.1",
"mime": "1.2.5",
"qs": "0.6.6",
"reduce-component": "1.0.1",
"extend": "~1.2.1"
"superagentparse": "^0.1.1"
},
"devDependencies": {
"zuul": "~1.3.0",
"express": "3.5.0",
"better-assert": "~0.1.0",
"should": "3.1.3",
"iconv-lite": "^0.2.11",
"mocha": "*"
},
"browser": {
Expand Down
50 changes: 48 additions & 2 deletions test/node/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@
var request = require('../..')
, express = require('express')
, assert = require('better-assert')
, iconv = require('iconv-lite')
, app = express();

app.get('/manny', function(req, res){
res.send('{"name":"manny"}');
});

app.get('/gbk', function (req, res) {
res.type('html');
var buf = iconv.encode('你好', 'gbk');
res.send(buf);
});

app.get('/big5', function (req, res) {
res.type('html');
var buf = iconv.encode('你好', 'big5');
res.send(buf);
});

app.get('/utf-8', function (req, res) {
res.type('html');
var buf = new Buffer('你好');
res.send(buf);
});

app.listen(3033);

describe('req.parse(fn)', function(){
Expand All @@ -21,5 +40,32 @@ describe('req.parse(fn)', function(){
assert('manny' == res.body.name);
done();
});
})
})
});

it('should parse gbk encoding page', function () {
request
.get('http://localhost:3033/gbk')
.decode('gbk')
.end(function (res) {
res.text.should.equal('你好');
});
});

it('should parse big5 encoding page', function () {
request
.get('http://localhost:3033/big5')
.decode('big5')
.end(function (res) {
res.text.should.equal('你好');
});
});

it('should parse utf-8 encoding page', function () {
request
.get('http://localhost:3033/utf-8')
.decode('utf-8')
.end(function (res) {
res.text.should.equal('你好');
});
});
});