Skip to content

Commit

Permalink
Fix to allow mocking arraybuffer responses (#66)
Browse files Browse the repository at this point in the history
It also allows to mock other non simple object (like Blob) responses.
  • Loading branch information
petraszd authored and ctimmerm committed Aug 1, 2017
1 parent 782402b commit 817ccd0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var utils = require('./utils');
function makeResponse(result, config) {
return {
status: result[0],
data: utils.isObject(result[1]) ? JSON.parse(JSON.stringify(result[1])) : result[1],
data: utils.isSimpleObject(result[1]) ? JSON.parse(JSON.stringify(result[1])) : result[1],
headers: result[2],
config: config
};
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ function createErrorResponse(message, config, response) {
return error;
}

function isObject(value) {
return value !== null && typeof value === 'object';
function isSimpleObject(value) {
return value !== null && value !== undefined && value.toString() === '[object Object]';
}

module.exports = {
find: find,
findHandler: findHandler,
isObject: isObject,
isSimpleObject: isSimpleObject,
purgeIfReplyOnce: purgeIfReplyOnce,
settle: settle
};
17 changes: 17 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,23 @@ describe('MockAdapter basics', function() {
});
});

it('allows sending an any object as response', function() {
var buffer = new ArrayBuffer(1);
var view = new Uint8Array(buffer);
view[0] = 0xFF;

mock.onGet('/').reply(200, buffer);

return instance({
url: '/',
method: 'GET',
responseType: 'arraybuffer'
}).then(function(response) {
var view = new Uint8Array(response.data);
expect(view[0]).to.equal(0xFF);
});
});

it('returns a deep copy of the mock data in the response when the data is an object', function() {
var data = {
foo: {
Expand Down

0 comments on commit 817ccd0

Please sign in to comment.