2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-02 16:04:10 +03:00
Files
axios/test/specs/defaults.spec.js
T
Zac Delventhal a9a3b5e22b Fixing default transformRequest with buffer pools (#1511)
* Fixing default transformRequest of TypedArrays with buffer pools

A buffer pool is a large ArrayBuffer of a preset size used with a TypedArray
such as Uint8Array. This can speed up performance when constructing TypedArrays
of unknown sizes, and is a technique used by Node with their Buffers, and
by libraries like dcodeIO/protobuf.js.

Because the ArrayBuffer of such a TypedArray is much longer than the array
itself, using `.buffer` to transform the array before POSTing results in
sending a request with many extraneous empty bytes, which is wastefule and may
result in unexpected behavior.

Using `.slice()` before grabbing the ArrayBuffer fixes the problem by creating
a new TypedArray with a buffer of the expected length.

Signed-off-by: Zac Delventhal <delventhalz@gmail.com>

* Adding test for using default transformRequest with buffer pools

Adds a new test to the default transformRequest, running it on a
Uint8Array with a byte length of 16, but a much larger ArrayBuffer
with a byte length of 256. The transformed array should not include
any extra bytes, and so must have a byte length of just 16.

Signed-off-by: Zac Delventhal <delventhalz@gmail.com>

Co-authored-by: Zac Delventhal <zac@bitwise.io>
Co-authored-by: Jay <jasonsaayman@gmail.com>
2020-05-27 14:42:41 +02:00

169 lines
4.6 KiB
JavaScript

var defaults = require('../../lib/defaults');
var utils = require('../../lib/utils');
describe('defaults', function () {
var XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN';
beforeEach(function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
delete axios.defaults.baseURL;
delete axios.defaults.headers.get['X-CUSTOM-HEADER'];
delete axios.defaults.headers.post['X-CUSTOM-HEADER'];
document.cookie = XSRF_COOKIE_NAME + '=;expires=' + new Date(Date.now() - 86400000).toGMTString();
});
it('should transform request json', function () {
expect(defaults.transformRequest[0]({foo: 'bar'})).toEqual('{"foo":"bar"}');
});
it('should do nothing to request string', function () {
expect(defaults.transformRequest[0]('foo=bar')).toEqual('foo=bar');
});
it('should transform TypedArrays without including buffer pool', function () {
if (typeof Uint8Array === 'undefined') return this.skip();
const buffered = new Uint8Array(256).subarray(10, 26);
expect(defaults.transformRequest[0](buffered).byteLength).toEqual(16);
});
it('should transform response json', function () {
var data = defaults.transformResponse[0]('{"foo":"bar"}');
expect(typeof data).toEqual('object');
expect(data.foo).toEqual('bar');
});
it('should do nothing to response string', function () {
expect(defaults.transformResponse[0]('foo=bar')).toEqual('foo=bar');
});
it('should use global defaults config', function (done) {
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('/foo');
done();
});
});
it('should use modified defaults config', function (done) {
axios.defaults.baseURL = 'http://example.com/';
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('http://example.com/foo');
done();
});
});
it('should use request config', function (done) {
axios('/foo', {
baseURL: 'http://www.example.com'
});
getAjaxRequest().then(function (request) {
expect(request.url).toBe('http://www.example.com/foo');
done();
});
});
it('should use default config for custom instance', function (done) {
var instance = axios.create({
xsrfCookieName: XSRF_COOKIE_NAME,
xsrfHeaderName: 'X-CUSTOM-XSRF-TOKEN'
});
document.cookie = instance.defaults.xsrfCookieName + '=foobarbaz';
instance.get('/foo');
getAjaxRequest().then(function (request) {
expect(request.requestHeaders[instance.defaults.xsrfHeaderName]).toEqual('foobarbaz');
done();
});
});
it('should use GET headers', function (done) {
axios.defaults.headers.get['X-CUSTOM-HEADER'] = 'foo';
axios.get('/foo');
getAjaxRequest().then(function (request) {
expect(request.requestHeaders['X-CUSTOM-HEADER']).toBe('foo');
done();
});
});
it('should use POST headers', function (done) {
axios.defaults.headers.post['X-CUSTOM-HEADER'] = 'foo';
axios.post('/foo', {});
getAjaxRequest().then(function (request) {
expect(request.requestHeaders['X-CUSTOM-HEADER']).toBe('foo');
done();
});
});
it('should use header config', function (done) {
var instance = axios.create({
headers: {
common: {
'X-COMMON-HEADER': 'commonHeaderValue'
},
get: {
'X-GET-HEADER': 'getHeaderValue'
},
post: {
'X-POST-HEADER': 'postHeaderValue'
}
}
});
instance.get('/foo', {
headers: {
'X-FOO-HEADER': 'fooHeaderValue',
'X-BAR-HEADER': 'barHeaderValue'
}
});
getAjaxRequest().then(function (request) {
expect(request.requestHeaders).toEqual(
utils.merge(defaults.headers.common, defaults.headers.get, {
'X-COMMON-HEADER': 'commonHeaderValue',
'X-GET-HEADER': 'getHeaderValue',
'X-FOO-HEADER': 'fooHeaderValue',
'X-BAR-HEADER': 'barHeaderValue'
})
);
done();
});
});
it('should be used by custom instance if set before instance created', function (done) {
axios.defaults.baseURL = 'http://example.org/';
var instance = axios.create();
instance.get('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('http://example.org/foo');
done();
});
});
it('should not be used by custom instance if set after instance created', function (done) {
var instance = axios.create();
axios.defaults.baseURL = 'http://example.org/';
instance.get('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('/foo');
done();
});
});
});