From 652195ce0652f8003909c15fdc1e7d27baad5756 Mon Sep 17 00:00:00 2001 From: DigitalBrainJS Date: Fri, 20 May 2022 16:20:14 +0300 Subject: [PATCH] Added test for converting the data uri to a Blob; Fixed bug with parsing mime type for Blob; --- lib/helpers/fromDataURI.js | 2 +- test/unit/adapters/http.js | 19 +++++++++++++++++++ test/unit/helpers/fromDataURI.js | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/helpers/fromDataURI.js b/lib/helpers/fromDataURI.js index 4f57c7cf46..3ee2ab925a 100644 --- a/lib/helpers/fromDataURI.js +++ b/lib/helpers/fromDataURI.js @@ -23,7 +23,7 @@ module.exports = function fromDataURI(uri, asBlob, options) { } if (protocol === 'data') { - uri = uri.slice(protocol.length); + uri = protocol.length ? uri.slice(protocol.length + 1) : uri; var match = DATA_URL_PATTERN.exec(uri); diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 4bdc3eaae7..bc5a9436d2 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -15,6 +15,7 @@ var formidable = require('formidable'); var express = require('express'); var multer = require('multer'); var bodyParser = require('body-parser'); +const isBlobSupported = typeof Blob !== 'undefined'; var noop = ()=> {}; @@ -1483,6 +1484,24 @@ describe('supports http with nodejs', function () { }).catch(done); }); + it('should support requesting data URL as a Blob (if supported by the environment)', function (done) { + + if (isBlobSupported) { + this.skip(); + return; + } + + const buffer = Buffer.from('123'); + + const dataURI = 'data:application/octet-stream;base64,' + buffer.toString('base64'); + + axios.get(dataURI, {responseType: 'blob'}).then(async ({data})=> { + assert.strictEqual(data.type, 'application/octet-stream'); + assert.deepStrictEqual(await data.text(), '123'); + done(); + }).catch(done); + }); + it('should support requesting data URL as a String (text)', function (done) { const buffer = Buffer.from('123', 'utf-8'); diff --git a/test/unit/helpers/fromDataURI.js b/test/unit/helpers/fromDataURI.js index e4f6754535..97ff84bf06 100644 --- a/test/unit/helpers/fromDataURI.js +++ b/test/unit/helpers/fromDataURI.js @@ -7,6 +7,6 @@ describe('helpers::fromDataURI', function () { const dataURI = 'data:application/octet-stream;base64,' + buffer.toString('base64'); - assert.deepStrictEqual(fromDataURI(dataURI), buffer); + assert.deepStrictEqual(fromDataURI(dataURI, false), buffer); }); });