From f5e5e9de08c0cd8517b4e42c1428f982f1a8bc0e Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 6 Sep 2014 15:45:49 +0100 Subject: [PATCH 1/2] Fix exception when setting XHR responseType parameter According to the XHR spec (http://xhr.spec.whatwg.org/#the-responsetype-attribute) and https://bugzilla.mozilla.org/show_bug.cgi?id=707484 it is legal to set the responseType attribute before opening the request. However, this doesn't work with current versions of Firefox and results in an exception. Work around this issue by setting the xhr.responseType attribute only after opening the request. Also if the caller explicitly specifies a desired response type, report an error instead of silently returning an unexpected data type if the xhr.responseType assignment fails. Fixes #65 --- lib/request.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/request.js b/lib/request.js index d1e2b42..69ec49c 100644 --- a/lib/request.js +++ b/lib/request.js @@ -22,15 +22,20 @@ var Request = module.exports = function (xhr, params) { try { xhr.withCredentials = params.withCredentials } catch (e) {} - if (params.responseType) try { xhr.responseType = params.responseType } - catch (e) {} - xhr.open( params.method || 'GET', self.uri, true ); + if (params.responseType) { + try { + xhr.responseType = params.responseType + } catch (e) { + self.emit('error', e); + } + } + xhr.onerror = function(event) { self.emit('error', new Error('Network error')); }; From 6f8bdaa58e9f03c385f4bcc9e720e9d7fe7ce733 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 6 Sep 2014 15:58:46 +0100 Subject: [PATCH 2/2] Add test for setting responseType parameter This verifies that the responseType is set, but not that it is a legal value or one which the browser will understand. --- test/request_url.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/request_url.js b/test/request_url.js index 1ff8e53..205aabb 100644 --- a/test/request_url.js +++ b/test/request_url.js @@ -86,3 +86,11 @@ test('Test withCredentials param', function(t) { t.end(); }); + +test('Test responseType param', function(t) { + var url = '/api/binary-endpoint'; + var request = http.request({url: url, responseType: 'arraybuffer'}, noop); + t.equal(request.xhr.responseType, 'arraybuffer', 'xhr.responseType should be set'); + t.end(); +}); +