Skip to content

Commit

Permalink
Merge pull request #148 from TimvdEijnden/add_responseType_support
Browse files Browse the repository at this point in the history
added support for setting responseType to array buffer
  • Loading branch information
trek committed Mar 10, 2016
2 parents ce3bdde + a3812e3 commit 318acfc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pretender.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,18 @@ function interceptor(pretender) {

var xhr = fakeXHR._passthroughRequest = new pretender._nativeXMLHttpRequest();

if (fakeXHR.responseType === 'arraybuffer') {
lifecycleProps = ['readyState', 'response', 'status', 'statusText'];
xhr.responseType = fakeXHR.responseType;
}

// Use onload if the browser supports it
if ('onload' in xhr) {
evts.push('load');
}

// add progress event for async calls
if (fakeXHR.async) {
if (fakeXHR.async && fakeXHR.responseType !== 'arraybuffer') {
evts.push('progress');
}

Expand Down
28 changes: 28 additions & 0 deletions test/passthrough_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ describe('passthrough requests', function(config) {
xhr.send('some data');
});

asyncTest('asynchronous request with pass-through and ' +
'arraybuffer as responseType', function(assert) {
var pretender = this.pretender;
function testXHR() {
this.pretender = pretender;
this.open = function() {};
this.setRequestHeader = function() {};
this.upload = {};
this.responseType = '';
this.send = {
pretender: pretender,
apply: function(xhr, argument) {
assert.equal(xhr.responseType, 'arraybuffer');
this.pretender.resolve(xhr);
QUnit.start();
}
};
}
pretender._nativeXMLHttpRequest = testXHR;

pretender.get('/some/path', pretender.passthrough);

var xhr = new window.XMLHttpRequest();
xhr.open('GET', '/some/path');
xhr.responseType = 'arraybuffer';
xhr.send();
});

asyncTest('synchronous request does not have timeout, withCredentials and onprogress event', function(assert) {
var pretender = this.pretender;
function testXHR() {
Expand Down

0 comments on commit 318acfc

Please sign in to comment.