2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-11 18:02:32 +03:00

Fixed & Imporoved AxiosHeaders class (#5224)

* Refactored AxiosHeaders class;

* Added support for instances of AxiosHeaders as a value for the headers option;

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Dmitriy Mozgovoy
2022-11-07 21:11:42 +02:00
committed by GitHub
parent c0a723ab6c
commit ab77a40e1c
16 changed files with 321 additions and 159 deletions
+22
View File
@@ -1,5 +1,6 @@
import defaults from '../../../lib/defaults';
import mergeConfig from '../../../lib/core/mergeConfig';
import {AxiosHeaders} from "../../../index.js";
describe('core::mergeConfig', function() {
it('should accept undefined for second argument', function() {
@@ -100,6 +101,27 @@ describe('core::mergeConfig', function() {
expect(merged.nestedConfig.propertyOnRequestConfig).toEqual(true);
});
describe('headers', ()=> {
it('should allow merging with AxiosHeaders instances', () => {
const merged = mergeConfig({
headers: new AxiosHeaders({
x: 1,
y: 2
})
}, {
headers: new AxiosHeaders({
X: 1,
Y: 2
})
});
expect(merged.headers).toEqual({
x: '1',
y: '2'
});
});
});
describe('valueFromConfig2Keys', function() {
const config1 = {url: '/foo', method: 'post', data: {a: 3}};
+28 -3
View File
@@ -1,3 +1,5 @@
const {AxiosHeaders} = axios;
function testHeaderValue(headers, key, val) {
let found = false;
@@ -106,12 +108,35 @@ describe('headers', function () {
});
});
it('should preserve content-type if data is false', function (done) {
it('should preserve content-type if data is false', async function () {
axios.post('/foo', false);
getAjaxRequest().then(function (request) {
await getAjaxRequest().then(function (request) {
testHeaderValue(request.requestHeaders, 'Content-Type', 'application/x-www-form-urlencoded');
done();
});
});
it('should allow an AxiosHeaders instance to be used as the value of the headers option', async ()=> {
const instance = axios.create({
headers: new AxiosHeaders({
xFoo: 'foo',
xBar: 'bar'
})
});
instance.get('/foo', {
headers: {
XFOO: 'foo2',
xBaz: 'baz'
}
});
await getAjaxRequest().then(function (request) {
expect(request.requestHeaders.xFoo).toEqual('foo2');
expect(request.requestHeaders.xBar).toEqual('bar');
expect(request.requestHeaders.xBaz).toEqual('baz');
expect(request.requestHeaders.XFOO).toEqual(undefined);
});
});
});
+2
View File
@@ -1,3 +1,5 @@
import AxiosHeaders from "../../lib/core/AxiosHeaders.js";
describe('options', function () {
beforeEach(function () {
jasmine.Ajax.install();
+10
View File
@@ -83,4 +83,14 @@ describe('utils::merge', function () {
expect(d).toEqual({a: [1, 2, 3]});
expect(d.a).not.toBe(a);
});
it('should support caseless option', ()=> {
const a = {x: 1};
const b = {X: 2};
const merged = merge.call({caseless: true}, a, b);
expect(merged).toEqual({
x: 2
});
});
});