Skip to content

Commit

Permalink
check for form data types before processing data canjs#133
Browse files Browse the repository at this point in the history
  • Loading branch information
green3g committed Sep 29, 2017
1 parent 8ca0064 commit 59421ae
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ var methodMapping = {
}
};


// Get a global reference.
var GLOBAL = typeof global !== "undefined"? global : window;

// valid form types in addition to plain objects as URL search params
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send
var formTypes = ['ArrayBuffer', 'ArrayBufferView', 'Blob', 'FormData'];

function getMethodAndPath (route) {
// Match URL if it has GET, POST, PUT, DELETE or PATCH.
var matches = route.match(/(GET|POST|PUT|DELETE|PATCH) (.+)/i);
Expand Down Expand Up @@ -249,15 +257,32 @@ exports.get = function(xhrSettings) {
};
}
} else {
var xhrData = assign({}, xhrSettings.data || {});
fixtureSettings.data = assign(xhrData, data);
if(exports.isTypedBody(xhrSettings.data)){
fixtureSettings.data = xhrSettings.data;
} else {
var xhrData = assign({}, xhrSettings.data || {});
fixtureSettings.data = assign(xhrData, data);
}
}
}



return fixtureSettings;
};

exports.isTypedBody = function(data){
for(var i = 0; i < formTypes.length; i ++){
var Type = GLOBAL[formTypes[i]];
if(!Type){
continue;
}
if(data instanceof Type){
return true;
}
}

return false;
}

exports.matches = function(settings, fixture, exact) {
if (exact) {
return canSet.equal(settings, fixture, {fixture: function(){ return true; }});
Expand Down

0 comments on commit 59421ae

Please sign in to comment.