2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Added data URL support for node.js; (#4725)

* Added data URL support for node.js;
Added missed data URL protocol for the browser environment;
Optimized JSON parsing in the default response transformer;
Refactored project structure;
Added `cause` prop for AxiosError instance that refers to the original error if it was wrapped with `AxiosError.from` method;
Added fromDataURI helper;
Added test for handling data:url as an `arraybuffer|text|stream`;

* Added throwing of 405 HTTP error if the method is not GET;
This commit is contained in:
Dmitriy Mozgovoy
2022-05-20 09:04:36 +03:00
committed by GitHub
parent 63e559fa60
commit c30252f685
17 changed files with 226 additions and 48 deletions
+43
View File
@@ -1442,4 +1442,47 @@ describe('supports http with nodejs', function () {
}).catch(done);
});
});
describe('Data URL', function () {
it('should support requesting data URL as a Buffer', function (done) {
const buffer = Buffer.from('123');
const dataURI = 'data:application/octet-stream;base64,' + buffer.toString('base64');
axios.get(dataURI).then(({data})=> {
assert.deepStrictEqual(data, buffer);
done();
}).catch(done);
});
it('should support requesting data URL as a String (text)', function (done) {
const buffer = Buffer.from('123', 'utf-8');
const dataURI = 'data:application/octet-stream;base64,' + buffer.toString('base64');
axios.get(dataURI, {responseType: "text"}).then(({data})=> {
assert.deepStrictEqual(data, '123');
done();
}).catch(done);
});
it('should support requesting data URL as a Stream', function (done) {
const buffer = Buffer.from('123', 'utf-8');
const dataURI = 'data:application/octet-stream;base64,' + buffer.toString('base64');
axios.get(dataURI, {responseType: "stream"}).then(({data})=> {
var str = '';
data.on('data', function(response){
str += response.toString();
});
data.on('end', function(){
assert.strictEqual(str, '123');
done();
});
}).catch(done);
});
});
});
+12
View File
@@ -0,0 +1,12 @@
var assert = require('assert');
var fromDataURI = require('../../../lib/helpers/fromDataURI');
describe('helpers::fromDataURI', function () {
it('should return buffer from data uri', function () {
const buffer= Buffer.from('123');
const dataURI = 'data:application/octet-stream;base64,' + buffer.toString('base64');
assert.deepStrictEqual(fromDataURI(dataURI), buffer);
});
});