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

feat: Allow responding with Blob or ArrayBuffer when responseType is blob #164

Merged
merged 2 commits into from
May 26, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/fake-xhr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ function verifyResponseBodyType(body, responseType) {
);
error.name = "InvalidBodyException";
}
} else if (responseType === "blob") {
if (
!isString &&
!(body instanceof ArrayBuffer) &&
supportsBlob &&
!(body instanceof Blob)
) {
error = new Error(
offirgolan marked this conversation as resolved.
Show resolved Hide resolved
"Attempted to respond to fake XMLHttpRequest with " +
body +
", which is not a string, ArrayBuffer, or Blob."
);
error.name = "InvalidBodyException";
}
} else if (!isString) {
error = new Error(
"Attempted to respond to fake XMLHttpRequest with " +
Expand Down Expand Up @@ -357,6 +371,10 @@ function fakeXMLHttpRequestFor(globalScope) {
return null;
}
} else if (supportsBlob && responseType === "blob") {
if (body instanceof Blob) {
return body;
}

var blobOptions = {};
if (contentType) {
blobOptions.type = contentType;
Expand Down
63 changes: 63 additions & 0 deletions lib/fake-xhr/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,30 @@ describe("FakeXMLHttpRequest", function() {
}, "InvalidBodyException");
});

it("throws if body is not a string or arraybuffer and responseType='arraybuffer'", function() {
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/");
xhr.responseType = "arraybuffer";
xhr.send();
xhr.setResponseHeaders({});

assert.exception(function() {
xhr.setResponseBody({});
}, "InvalidBodyException");
});

it("throws if body is not a string, arraybuffer, or blob and responseType='blob'", function() {
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/");
xhr.responseType = "blob";
xhr.send();
xhr.setResponseHeaders({});

assert.exception(function() {
xhr.setResponseBody({});
}, "InvalidBodyException");
});

if (supportsArrayBuffer) {
describe("with ArrayBuffer support", function() {
it("invokes onreadystatechange for each chunk when responseType='arraybuffer'", function() {
Expand Down Expand Up @@ -1744,6 +1768,45 @@ describe("FakeXMLHttpRequest", function() {
assertBlobMatches(this.xhr.response, "\xFF", done);
});

it("returns blob with correct binary data when using an arraybuffer", function(done) {
this.xhr.responseType = "blob";
this.xhr.open("GET", "/");
this.xhr.send();

var buffer = new Uint8Array([160, 64, 0, 0, 32, 193])
.buffer;

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

assertBlobMatches(
this.xhr.response,
new Blob([buffer]),
done
);
});

it("returns blob with correct binary data when using a blob", function(done) {
this.xhr.responseType = "blob";
this.xhr.open("GET", "/");
this.xhr.send();

var blob = new Blob([
new Uint8Array([160, 64, 0, 0, 32, 193]).buffer
]);

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

assertBlobMatches(this.xhr.response, blob, done);
});

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