Skip to content

Commit

Permalink
Merge pull request #134 from roemhildtg/master
Browse files Browse the repository at this point in the history
check for form data types before processing data #133
  • Loading branch information
chasenlehara committed Jan 5, 2018
2 parents ddfef65 + bb8eac6 commit 005a892
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
31 changes: 28 additions & 3 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
18 changes: 18 additions & 0 deletions test/fixture_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1864,4 +1864,22 @@ if ("onabort" in XMLHttpRequest._XHR.prototype) {
xhr.open('GET', '/onload');
xhr.send();
});

asyncTest('should not process FormData type #133', function() {

fixture('/upload', function(req, res) {
ok(req.data instanceof FormData, 'data returned should be instance of formdata');
res(400);
});

var data = new FormData();
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
fixture('/upload', null);
ok(true, 'should not throw when sending FormData');
start();
});
xhr.open('POST', '/upload', true);
xhr.send(data);
});
}

0 comments on commit 005a892

Please sign in to comment.