diff --git a/test/js-specs.js b/test/js-specs.js index 253841e..67343a8 100644 --- a/test/js-specs.js +++ b/test/js-specs.js @@ -419,7 +419,7 @@ describe('Common helpers', function () { response => response.data.should.be.equal('fakeData') ); - handler({ data: 'fakeData' }); + handler({ data: 'fakeData' }); }); it('does not change response data if response type is "text"', () => { @@ -449,7 +449,7 @@ describe('Common helpers', function () { const helpers = require('../www/helpers')(null, jsUtil, null, messages, null, errorCodes); const handler = helpers.injectRawResponseHandler( 'json', - response => should.equal(null, response.data) + response => should.equal(undefined, response.data) ); handler({ data: emptyData }); @@ -486,7 +486,7 @@ describe('Common helpers', function () { } ); - handler({ data: 'myString', headers: { 'content-type': 'fakeType'} }); + handler({ data: 'myString', headers: { 'content-type': 'fakeType' } }); }); it('handles empty "blob" response correctly', () => { @@ -498,7 +498,7 @@ describe('Common helpers', function () { } ); - handler({ data: '', headers: { 'content-type': 'fakeType'} }); + handler({ data: '', headers: { 'content-type': 'fakeType' } }); }); it('calls failure callback when post-processing fails', () => { @@ -579,7 +579,7 @@ describe('Common helpers', function () { it('processes data correctly when serializer "utf8" is configured', (cb) => { helpers.processData('myString', 'utf8', (data) => { - data.should.be.eql({text: 'myString'}); + data.should.be.eql({ text: 'myString' }); cb(); }) }); @@ -624,7 +624,7 @@ describe('Common helpers', function () { }); it('processes data correctly when serializer "raw" is configured', (cb) => { - const byteArray = new Uint8Array([1,2,3]); + const byteArray = new Uint8Array([1, 2, 3]); helpers.processData(byteArray, 'raw', (data) => { data.should.be.a('ArrayBuffer'); data.should.be.equal(byteArray.buffer); @@ -680,7 +680,7 @@ describe('Dependency Validator', function () { describe('checkFormDataInstance()', function () { it('throws an error if FormData.entries() is not supported on given instance', function () { const console = new ConsoleMock(); - const validator = require('../www/dependency-validator')({ FormData: {}}, console, messages); + const validator = require('../www/dependency-validator')({ FormData: {} }, console, messages); (() => validator.checkFormDataInstance({})).should.throw(messages.MISSING_FORMDATA_ENTRIES_API); }); @@ -717,12 +717,12 @@ describe('Ponyfills', function () { const iterator = new ponyfills.Iterator([]); iterator.next().should.be.eql({ done: true, value: undefined }); }); - + it('returns iteration object correctly when end posititon of list is not reached yet', () => { const iterator = new ponyfills.Iterator([['first', 'this is the first item']]); iterator.next().should.be.eql({ done: false, value: ['first', 'this is the first item'] }); }); - + it('returns iteration object correctly when end posititon of list is already reached', () => { const iterator = new ponyfills.Iterator([['first', 'this is the first item']]); iterator.next(); diff --git a/www/helpers.js b/www/helpers.js index 66e20aa..a578211 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -292,29 +292,31 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, return success(response); } - if (!response.data) { - // return null as data if response data is not set - response.data = null; - return success(response); - } - try { // json if (responseType === validResponseTypes[1]) { - response.data = JSON.parse(response.data); + response.data = response.data === '' + ? undefined + : JSON.parse(response.data); } // arraybuffer else if (responseType === validResponseTypes[2]) { - response.data = base64.toArrayBuffer(response.data); + response.data = response.data === '' + ? null + : base64.toArrayBuffer(response.data); } // blob else if (responseType === validResponseTypes[3]) { - var buffer = base64.toArrayBuffer(response.data); - var type = response.headers['content-type'] || ''; - var blob = new Blob([ buffer ], { type: type }); - response.data = blob; + if (response.data === '') { + response.data = null; + } else { + var buffer = base64.toArrayBuffer(response.data); + var type = response.headers['content-type'] || ''; + var blob = new Blob([buffer], { type: type }); + response.data = blob; + } } success(response); @@ -390,7 +392,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, if (allowedInstanceTypes) { var isCorrectInstanceType = false; - allowedInstanceTypes.forEach(function(type) { + allowedInstanceTypes.forEach(function (type) { if ((global[type] && data instanceof global[type]) || (ponyfills[type] && data instanceof ponyfills[type])) { isCorrectInstanceType = true; } @@ -446,7 +448,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, if (entry.value[1] instanceof global.Blob || entry.value[1] instanceof global.File) { var reader = new global.FileReader(); - reader.onload = function() { + reader.onload = function () { result.buffers.push(base64.fromArrayBuffer(reader.result)); result.names.push(entry.value[0]); result.fileNames.push(entry.value[1].name || 'blob');