From bb8eac6f17c7cb5ed965b5b40c6acf02d0dc7c27 Mon Sep 17 00:00:00 2001 From: roemhildtg Date: Fri, 29 Sep 2017 11:27:29 -0500 Subject: [PATCH] check for form data types before processing data #133 --- core.js | 31 ++++++++++++++++++++++++++++--- test/fixture_test.js | 18 ++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/core.js b/core.js index 5809579..b529392 100644 --- a/core.js +++ b/core.js @@ -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); @@ -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; }}); diff --git a/test/fixture_test.js b/test/fixture_test.js index 4c430fd..a37e205 100644 --- a/test/fixture_test.js +++ b/test/fixture_test.js @@ -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); + }); }