From 0412c7666e72f17e5359a1106f20b79e59f1b3c1 Mon Sep 17 00:00:00 2001 From: mzabriskie Date: Mon, 2 Feb 2015 22:04:18 -0700 Subject: [PATCH] Fixing issue with multibyte characters in node closes #38 --- lib/adapters/http.js | 8 ++++---- test/unit/axios/http.js | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 test/unit/axios/http.js diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 8a6fbf6..81da7ac 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -52,15 +52,15 @@ module.exports = function httpAdapter(resolve, reject, config) { // Create the request var transport = parsed.protocol === 'https:' ? https : http; var req = transport.request(options, function (res) { - var responseText = ''; + var responseBuffer = []; res.on('data', function (chunk) { - responseText += chunk; + responseBuffer.push(chunk); }); res.on('end', function () { var response = { data: transformData( - responseText, + Buffer.concat(responseBuffer).toString('utf8'), res.headers, config.transformResponse ), @@ -83,4 +83,4 @@ module.exports = function httpAdapter(resolve, reject, config) { // Send the request req.end(data); -}; \ No newline at end of file +}; diff --git a/test/unit/axios/http.js b/test/unit/axios/http.js new file mode 100644 index 0000000..07f3870 --- /dev/null +++ b/test/unit/axios/http.js @@ -0,0 +1,44 @@ +var axios = require('../../../index'); +var http = require('http'); +var server; + +module.exports = { + tearDown: function (callback) { + server.close(); + server = null; + callback(); + }, + + testJSON: function (test) { + console.log('testJSON'); + var data = { + firstName: 'Fred', + lastName: 'Flintstone', + emailAddr: 'fred@example.com' + }; + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'application/json;charset=utf-8'); + res.end(JSON.stringify(data)); + }).listen(4444, function () { + axios.get('http://localhost:4444/').then(function (res) { + test.deepEqual(res.data, data); + test.done(); + }); + }); + }, + + testUTF8: function (test) { + var str = Array(100000).join('ж'); + + server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end(str); + }).listen(4444, function () { + axios.get('http://localhost:4444/').then(function (res) { + test.equal(res.data, str); + test.done(); + }); + }); + } +};