Skip to content

Commit

Permalink
Merge pull request #886 from johann-sonntagbauer/fix#875
Browse files Browse the repository at this point in the history
Fix #875 Proper support UTF8 payloads
  • Loading branch information
fatso83 committed Oct 22, 2015
2 parents 875c1d8 + e71564e commit d931ca6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
15 changes: 3 additions & 12 deletions lib/sinon/util/fake_xml_http_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require("../extend");
require("./event");
require("../log_error");
var TextEncoder = require("text-encoding").TextEncoder;

var sinon = require("./core");

Expand Down Expand Up @@ -290,18 +291,8 @@ function verifyResponseBodyType(body) {
}
}

function convertToArrayBuffer(body) {
var buffer = new ArrayBuffer(body.length);
var view = new Uint8Array(buffer);
for (var i = 0; i < body.length; i++) {
var charCode = body.charCodeAt(i);
if (charCode >= 256) {
throw new TypeError("arraybuffer or blob responseTypes require binary string, " +
"invalid character " + body[i] + " found.");
}
view[i] = charCode;
}
return buffer;
function convertToArrayBuffer(body, encoding) {
return new TextEncoder(encoding || "utf-8").encode(body).buffer;
}

function isXmlContentType(contentType) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"formatio": "1.1.1",
"util": ">=0.10.3 <1",
"lolex": "1.3.2",
"samsam": "1.1.2"
"samsam": "1.1.2",
"text-encoding": "0.5.2"
},
"devDependencies": {
"browserify": "^11.1.0",
Expand Down
3 changes: 2 additions & 1 deletion test/buster-packaged.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ config.packaged = {
environment: "browser",
rootPath: "../",
libs: [
"node_modules/samsam/lib/samsam.js"
"node_modules/samsam/lib/samsam.js",
"node_modules/text-encoding/lib/encoding.js"
],
sources: [
"pkg/sinon.js"
Expand Down
24 changes: 17 additions & 7 deletions test/util/fake-xml-http-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var buster = root.buster || require("buster");
var sinon = root.sinon || require("../../lib/sinon");
var TextDecoder = root.TextDecoder || require("text-encoding").TextDecoder;
var assert = buster.assert;
var refute = buster.refute;

Expand Down Expand Up @@ -34,13 +35,9 @@
}
};

var assertArrayBufferMatches = function (actual, expected) {
var assertArrayBufferMatches = function (actual, expected, encoding) {
assert(actual instanceof ArrayBuffer, "${0} expected to be an ArrayBuffer");
var actualString = "";
var actualView = new Uint8Array(actual);
for (var i = 0; i < actualView.length; i++) {
actualString += String.fromCharCode(actualView[i]);
}
var actualString = new TextDecoder(encoding || "utf-8").decode(actual);
assert.same(actualString, expected, "ArrayBuffer [${0}] expected to match ArrayBuffer [${1}]");
};

Expand All @@ -49,7 +46,7 @@
actualReader.onloadend = done(function () {
assert.same(actualReader.result, expected);
});
actualReader.readAsBinaryString(actual);
actualReader.readAsText(actual);
};

var assertProgressEvent = function (event, progress) {
Expand Down Expand Up @@ -1257,6 +1254,19 @@
this.xhr.respond(200, { "Content-Type": "application/octet-stream" }, "\xFF");

assertBlobMatches(this.xhr.response, "\xFF", done);
},

"does parse utf-8 content outside ASCII range properly": function (done) {
this.xhr.responseType = "blob";
this.xhr.open("GET", "/");
this.xhr.send();

var responseText = JSON.stringify({foo: "♥"});

this.xhr.respond(200, { "Content-Type": "application/octet-stream" },
responseText);

assertBlobMatches(this.xhr.response, responseText, done);
}
}
},
Expand Down

0 comments on commit d931ca6

Please sign in to comment.