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

Feature Request: reply can't replay with ArrayBuffer, Blob, etc. responses #66

Merged
merged 1 commit into from
Aug 1, 2017
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
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