Commit 5ceba61b authored by Romain Courteaud's avatar Romain Courteaud

[node] Handle blob responseType

Thanks to Guillaume Royer <guillaume.royer@clearroad.io> for reporting
the issue.
See nexedi/jio!103
parent d6408551
...@@ -41,14 +41,42 @@ ...@@ -41,14 +41,42 @@
}; };
window.FormData = FormData; window.FormData = FormData;
function convertToBlob(promise, convert) {
if (!convert) {
return promise;
}
var result;
if (promise instanceof RSVP.Queue) {
result = promise;
} else {
result = new RSVP.Queue()
.push(function () {
return promise;
});
}
return result
.push(function (evt) {
evt.target.response = new Blob(
[evt.target.response || evt.target.responseText],
{type: evt.target.getResponseHeader('Content-Type')}
);
return evt;
});
}
originalAjax = jIO.util.ajax; originalAjax = jIO.util.ajax;
jIO.util.ajax = function ajax(param) { jIO.util.ajax = function ajax(param) {
var result,
need_convertion = (param.dataType === 'blob');
// Copy the param dict document (no need for deep copy) to
// allow tests to check them
param = Object.assign({}, param);
if (need_convertion) {
param.dataType = 'arraybuffer';
}
if (param.data instanceof Blob) { if (param.data instanceof Blob) {
// Copy the param dict document (no need for deep copy) to
// allow tests to check them
param = Object.assign({}, param);
// Blob is not supported by xhr2, so convert to ArrayBuffer instead // Blob is not supported by xhr2, so convert to ArrayBuffer instead
return new RSVP.Queue() result = new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.readBlobAsArrayBuffer(param.data); return jIO.util.readBlobAsArrayBuffer(param.data);
}) })
...@@ -56,12 +84,7 @@ ...@@ -56,12 +84,7 @@
param.data = evt.target.result; param.data = evt.target.result;
return originalAjax(param); return originalAjax(param);
}); });
} } else if (param.data instanceof FormData) {
if (param.data instanceof FormData) {
// Copy the param dict document (no need for deep copy) to
// allow tests to check them
param = Object.assign({}, param);
// Implement minimal FormData for erp5storage // Implement minimal FormData for erp5storage
if (!param.hasOwnProperty('headers')) { if (!param.hasOwnProperty('headers')) {
param.headers = {}; param.headers = {};
...@@ -74,10 +97,12 @@ ...@@ -74,10 +97,12 @@
param.data.boundary; param.data.boundary;
param.data.body += '--' + param.data.boundary + '--\r\n'; param.data.body += '--' + param.data.boundary + '--\r\n';
param.data = param.data.body; param.data = param.data.body;
return originalAjax(param); result = originalAjax(param);
} else {
result = originalAjax(param);
} }
return originalAjax(param); return convertToBlob(result, need_convertion);
}; };
}(window, window.jIO, window.Blob, window.RSVP)); }(window, window.jIO, window.Blob, window.RSVP));
......
...@@ -1003,8 +1003,12 @@ ...@@ -1003,8 +1003,12 @@
type: "dropbox", type: "dropbox",
access_token: token access_token: token
}); });
this.spy_ajax = sinon.spy(jIO.util, "ajax");
}, },
teardown: function () { teardown: function () {
this.spy_ajax.restore();
delete this.spy_ajax;
this.server.restore(); this.server.restore();
delete this.server; delete this.server;
} }
...@@ -1075,7 +1079,7 @@ ...@@ -1075,7 +1079,7 @@
test("getAttachment document", function () { test("getAttachment document", function () {
var url = "https://content.dropboxapi.com/2/files/download", var url = "https://content.dropboxapi.com/2/files/download",
server = this.server; context = this;
this.server.respondWith("POST", url, [200, { this.server.respondWith("POST", url, [200, {
"Content-Type": "text/xplain" "Content-Type": "text/xplain"
}, "foo\nbaré"]); }, "foo\nbaré"]);
...@@ -1088,20 +1092,20 @@ ...@@ -1088,20 +1092,20 @@
"attachment1" "attachment1"
) )
.then(function (result) { .then(function (result) {
equal(server.requests.length, 1); equal(context.spy_ajax.callCount, 1);
equal(server.requests[0].method, "POST"); equal(context.spy_ajax.firstCall.args[0].type, "POST");
equal(server.requests[0].url, url); equal(context.spy_ajax.firstCall.args[0].url, url);
equal(server.requests[0].status, 200); equal(context.spy_ajax.firstCall.args[0].data, undefined);
equal(server.requests[0].requestBody, undefined); equal(context.spy_ajax.firstCall.args[0].dataType, 'blob');
equal(server.requests[0].responseText, "foo\nbaré"); deepEqual(context.spy_ajax.firstCall.args[0].xhrFields, undefined);
deepEqual(server.requests[0].requestHeaders, { deepEqual(context.spy_ajax.firstCall.args[0].headers, {
"Authorization": "Bearer sample_token", "Authorization": "Bearer sample_token",
"Content-Type": "text/plain;charset=utf-8",
"Dropbox-API-Arg": '{"path":"/getAttachment1/attachment1"}' "Dropbox-API-Arg": '{"path":"/getAttachment1/attachment1"}'
}); });
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "text/xplain", "Check mimetype"); deepEqual(result.type, "text/xplain", "Check mimetype");
return jIO.util.readBlobAsText(result); return jIO.util.readBlobAsText(result);
}) })
.then(function (result) { .then(function (result) {
......
...@@ -725,7 +725,7 @@ ...@@ -725,7 +725,7 @@
}, ""]); }, ""]);
stop(); stop();
expect(17); expect(16);
this.jio.putAttachment( this.jio.putAttachment(
id, id,
...@@ -748,7 +748,6 @@ ...@@ -748,7 +748,6 @@
equal(server.requests[0].method, "POST"); equal(server.requests[0].method, "POST");
equal(server.requests[0].url, submit_url); equal(server.requests[0].url, submit_url);
equal(server.requests[0].status, 204); equal(server.requests[0].status, 204);
equal(server.requests[0].responseType, "blob");
ok(context.spy.calledTwice, "FormData.append count " + ok(context.spy.calledTwice, "FormData.append count " +
context.spy.callCount); context.spy.callCount);
......
...@@ -192,4 +192,36 @@ ...@@ -192,4 +192,36 @@
start(); start();
}); });
}); });
test("Blob responseType handling", function () {
stop();
expect(6);
var url = "https://www.example.org/com/bar",
server = this.server;
this.server.respondWith("POST", url, [200, {}, 'OK']);
return new RSVP.Queue()
.then(function () {
return jIO.util.ajax({
type: 'POST',
url: url,
dataType: 'blob'
});
})
.then(function (evt) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, url);
equal(server.requests[0].responseType, 'arraybuffer');
equal(server.requests[0].responseText, 'OK');
ok(evt.target.response instanceof Blob, evt.target.response);
})
.always(function () {
start();
});
});
}(jIO, QUnit, FormData, Blob, ArrayBuffer)); }(jIO, QUnit, FormData, Blob, ArrayBuffer));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment