Skip to content

Commit 0412c76

Browse files
committed
Fixing issue with multibyte characters in node
closes axios#38
1 parent 59093a9 commit 0412c76

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

lib/adapters/http.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ module.exports = function httpAdapter(resolve, reject, config) {
5252
// Create the request
5353
var transport = parsed.protocol === 'https:' ? https : http;
5454
var req = transport.request(options, function (res) {
55-
var responseText = '';
55+
var responseBuffer = [];
5656
res.on('data', function (chunk) {
57-
responseText += chunk;
57+
responseBuffer.push(chunk);
5858
});
5959

6060
res.on('end', function () {
6161
var response = {
6262
data: transformData(
63-
responseText,
63+
Buffer.concat(responseBuffer).toString('utf8'),
6464
res.headers,
6565
config.transformResponse
6666
),
@@ -83,4 +83,4 @@ module.exports = function httpAdapter(resolve, reject, config) {
8383

8484
// Send the request
8585
req.end(data);
86-
};
86+
};

test/unit/axios/http.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var axios = require('../../../index');
2+
var http = require('http');
3+
var server;
4+
5+
module.exports = {
6+
tearDown: function (callback) {
7+
server.close();
8+
server = null;
9+
callback();
10+
},
11+
12+
testJSON: function (test) {
13+
console.log('testJSON');
14+
var data = {
15+
firstName: 'Fred',
16+
lastName: 'Flintstone',
17+
emailAddr: 'fred@example.com'
18+
};
19+
20+
server = http.createServer(function (req, res) {
21+
res.setHeader('Content-Type', 'application/json;charset=utf-8');
22+
res.end(JSON.stringify(data));
23+
}).listen(4444, function () {
24+
axios.get('http://localhost:4444/').then(function (res) {
25+
test.deepEqual(res.data, data);
26+
test.done();
27+
});
28+
});
29+
},
30+
31+
testUTF8: function (test) {
32+
var str = Array(100000).join('ж');
33+
34+
server = http.createServer(function (req, res) {
35+
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
36+
res.end(str);
37+
}).listen(4444, function () {
38+
axios.get('http://localhost:4444/').then(function (res) {
39+
test.equal(res.data, str);
40+
test.done();
41+
});
42+
});
43+
}
44+
};

0 commit comments

Comments
 (0)