diff --git a/lib/node/index.js b/lib/node/index.js index 34078a71e..d1955936e 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -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. @@ -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 * diff --git a/package.json b/package.json index 5b4510959..42aa87e7e 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/node/parsers.js b/test/node/parsers.js index c3855a00a..55fd61496 100644 --- a/test/node/parsers.js +++ b/test/node/parsers.js @@ -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(){ @@ -21,5 +40,32 @@ describe('req.parse(fn)', function(){ assert('manny' == res.body.name); done(); }); - }) -}) \ No newline at end of file + }); + + 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('你好'); + }); + }); +}); \ No newline at end of file