From 530d7c6ac4e09d23a079e5a75fe1ad5288c2f6aa 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 | 21 ++++++++++++++++++++- test/unit/helpers/fromDataURI.js | 2 +- 3 files changed, 22 insertions(+), 3 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..8804bd2d0b 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 = ()=> {}; @@ -577,7 +578,7 @@ describe('supports http with nodejs', function () { // consume the req stream req.on('data', noop); // and wait for the end before responding, otherwise an ECONNRESET error will be thrown - req.on('close', ()=> { + req.on('end', ()=> { res.end('OK'); }); }).listen(4444, function (err) { @@ -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); }); });