axios-axios/test/unit/helpers/estimateDataURLDecodedBytes.spec.js
Felix Bernhard a1b1d3f073
fix: backport maxContentLength vulnerability fix to v0.x (#7034)
* backport `maxContentLength` check for `data:` protocol urls

* backport test for `estimateDataURLDecodedBytes`

* use CommonJS export

* use `var` instead of `const`

* replace `let` with `var`

* use standard function instead of arrow function
2025-09-16 16:21:06 +02:00

31 lines
1.1 KiB
JavaScript

var assert = require('assert');
var estimateDataURLDecodedBytes = require('../../../lib/helpers/estimateDataURLDecodedBytes');
describe('estimateDataURLDecodedBytes', () => {
it('should return 0 for non-data URLs', () => {
assert.strictEqual(estimateDataURLDecodedBytes('http://example.com'), 0);
});
it('should calculate length for simple non-base64 data URL', () => {
const url = 'data:,Hello';
assert.strictEqual(estimateDataURLDecodedBytes(url), Buffer.byteLength('Hello', 'utf8'));
});
it('should calculate decoded length for base64 data URL', () => {
const str = 'Hello';
const b64 = Buffer.from(str, 'utf8').toString('base64');
const url = `data:text/plain;base64,${b64}`;
assert.strictEqual(estimateDataURLDecodedBytes(url), str.length);
});
it('should handle base64 with = padding', () => {
const url = 'data:text/plain;base64,TQ=='; // "M"
assert.strictEqual(estimateDataURLDecodedBytes(url), 1);
});
it('should handle base64 with %3D padding', () => {
const url = 'data:text/plain;base64,TQ%3D%3D'; // "M"
assert.strictEqual(estimateDataURLDecodedBytes(url), 1);
});
});