fix: broken handling for empty strings

This commit is contained in:
Sefa Ilkimen
2020-05-29 04:03:20 +02:00
parent 65e4a4d4dc
commit 4f4e7ffa33
2 changed files with 25 additions and 23 deletions
+7 -7
View File
@@ -419,7 +419,7 @@ describe('Common helpers', function () {
response => response.data.should.be.equal('fakeData') response => response.data.should.be.equal('fakeData')
); );
handler({ data: 'fakeData' }); handler({ data: 'fakeData' });
}); });
it('does not change response data if response type is "text"', () => { 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 helpers = require('../www/helpers')(null, jsUtil, null, messages, null, errorCodes);
const handler = helpers.injectRawResponseHandler( const handler = helpers.injectRawResponseHandler(
'json', 'json',
response => should.equal(null, response.data) response => should.equal(undefined, response.data)
); );
handler({ data: emptyData }); 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', () => { 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', () => { 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) => { it('processes data correctly when serializer "utf8" is configured', (cb) => {
helpers.processData('myString', 'utf8', (data) => { helpers.processData('myString', 'utf8', (data) => {
data.should.be.eql({text: 'myString'}); data.should.be.eql({ text: 'myString' });
cb(); cb();
}) })
}); });
@@ -624,7 +624,7 @@ describe('Common helpers', function () {
}); });
it('processes data correctly when serializer "raw" is configured', (cb) => { 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) => { helpers.processData(byteArray, 'raw', (data) => {
data.should.be.a('ArrayBuffer'); data.should.be.a('ArrayBuffer');
data.should.be.equal(byteArray.buffer); data.should.be.equal(byteArray.buffer);
@@ -680,7 +680,7 @@ describe('Dependency Validator', function () {
describe('checkFormDataInstance()', function () { describe('checkFormDataInstance()', function () {
it('throws an error if FormData.entries() is not supported on given instance', function () { it('throws an error if FormData.entries() is not supported on given instance', function () {
const console = new ConsoleMock(); 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); (() => validator.checkFormDataInstance({})).should.throw(messages.MISSING_FORMDATA_ENTRIES_API);
}); });
+16 -14
View File
@@ -292,29 +292,31 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
return success(response); return success(response);
} }
if (!response.data) {
// return null as data if response data is not set
response.data = null;
return success(response);
}
try { try {
// json // json
if (responseType === validResponseTypes[1]) { if (responseType === validResponseTypes[1]) {
response.data = JSON.parse(response.data); response.data = response.data === ''
? undefined
: JSON.parse(response.data);
} }
// arraybuffer // arraybuffer
else if (responseType === validResponseTypes[2]) { else if (responseType === validResponseTypes[2]) {
response.data = base64.toArrayBuffer(response.data); response.data = response.data === ''
? null
: base64.toArrayBuffer(response.data);
} }
// blob // blob
else if (responseType === validResponseTypes[3]) { else if (responseType === validResponseTypes[3]) {
var buffer = base64.toArrayBuffer(response.data); if (response.data === '') {
var type = response.headers['content-type'] || ''; response.data = null;
var blob = new Blob([ buffer ], { type: type }); } else {
response.data = blob; var buffer = base64.toArrayBuffer(response.data);
var type = response.headers['content-type'] || '';
var blob = new Blob([buffer], { type: type });
response.data = blob;
}
} }
success(response); success(response);
@@ -390,7 +392,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
if (allowedInstanceTypes) { if (allowedInstanceTypes) {
var isCorrectInstanceType = false; var isCorrectInstanceType = false;
allowedInstanceTypes.forEach(function(type) { allowedInstanceTypes.forEach(function (type) {
if ((global[type] && data instanceof global[type]) || (ponyfills[type] && data instanceof ponyfills[type])) { if ((global[type] && data instanceof global[type]) || (ponyfills[type] && data instanceof ponyfills[type])) {
isCorrectInstanceType = true; 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) { if (entry.value[1] instanceof global.Blob || entry.value[1] instanceof global.File) {
var reader = new global.FileReader(); var reader = new global.FileReader();
reader.onload = function() { reader.onload = function () {
result.buffers.push(base64.fromArrayBuffer(reader.result)); result.buffers.push(base64.fromArrayBuffer(reader.result));
result.names.push(entry.value[0]); result.names.push(entry.value[0]);
result.fileNames.push(entry.value[1].name || 'blob'); result.fileNames.push(entry.value[1].name || 'blob');