Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass responseType for defaked XHR #45

Merged
merged 1 commit into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/fake-xhr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ FakeXMLHttpRequest.defake = function defake(fakeXhr, xhrArgs) {
[
"open",
"setRequestHeader",
"send",
"abort",
"getResponseHeader",
"getAllResponseHeaders",
Expand All @@ -178,6 +177,11 @@ FakeXMLHttpRequest.defake = function defake(fakeXhr, xhrArgs) {
};
});

fakeXhr.send = function () {
xhr.responseType = fakeXhr.responseType;
return apply(xhr, "send", arguments);
};

var copyAttrs = function (args) {
args.forEach(function (attr) {
fakeXhr[attr] = xhr[attr];
Expand All @@ -190,9 +194,15 @@ FakeXMLHttpRequest.defake = function defake(fakeXhr, xhrArgs) {
copyAttrs(["status", "statusText"]);
}
if (xhr.readyState >= FakeXMLHttpRequest.LOADING) {
copyAttrs(["responseText", "response"]);
copyAttrs(["response"]);
if (xhr.responseType === "" || xhr.responseType === "text") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

responseText can be accessed only for text responses

copyAttrs(["responseText"]);
}
}
if (xhr.readyState === FakeXMLHttpRequest.DONE) {
if (
xhr.readyState === FakeXMLHttpRequest.DONE &&
(xhr.responseType === "" || xhr.responseType === "document")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

responseXML can be accessed only for XML responses

) {
copyAttrs(["responseXML"]);
}
if (fakeXhr.onreadystatechange) {
Expand Down
18 changes: 18 additions & 0 deletions lib/fake-xhr/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,24 @@ describe("FakeXMLHttpRequest", function () {
assert(abortSpy.calledOnce);
assert(onabortSpy.calledOnce);
});

it("passes responseType to working XHR object",
function () {
var workingXHRInstance;
var workingXHROverride = function () {
workingXHRInstance = this;
this.responseType = "";
this.open = function () {};
this.send = function () {};
};

runWithWorkingXHROveride(workingXHROverride, function () {
FakeXMLHttpRequest.defake(fakeXhr, []);
fakeXhr.responseType = "arraybuffer";
fakeXhr.send();
assert.equals(workingXHRInstance.responseType, "arraybuffer");
});
});
});

describe("defaked XHR filters", function () {
Expand Down