Skip to content

Commit

Permalink
Added test for converting the data uri to a Blob;
Browse files Browse the repository at this point in the history
Fixed bug with parsing mime type for Blob;
  • Loading branch information
DigitalBrainJS committed May 20, 2022
1 parent 8554a2d commit 530d7c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/fromDataURI.js
Expand Up @@ -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);

Expand Down
21 changes: 20 additions & 1 deletion test/unit/adapters/http.js
Expand Up @@ -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 = ()=> {};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion test/unit/helpers/fromDataURI.js
Expand Up @@ -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);
});
});

0 comments on commit 530d7c6

Please sign in to comment.