mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +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:
+1
-1
@@ -15,6 +15,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const exec = util.promisify(cp.exec);
|
||||
|
||||
const {Axios} = axiosFactory;
|
||||
|
||||
const ignoreList = ['default'];
|
||||
|
||||
const instance = axiosFactory.create({});
|
||||
@@ -133,5 +134,4 @@ describe('module', function () {
|
||||
await exec(`npm test --prefix ${pkgPath}`, {});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -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}};
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import AxiosHeaders from "../../lib/core/AxiosHeaders.js";
|
||||
|
||||
describe('options', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
|
||||
@@ -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
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1393,6 +1393,7 @@ describe('supports http with nodejs', function () {
|
||||
|
||||
it('should omit a user-agent if one is explicitly disclaimed', function (done) {
|
||||
server = http.createServer(function (req, res) {
|
||||
console.log(req.headers);
|
||||
assert.equal("user-agent" in req.headers, false);
|
||||
assert.equal("User-Agent" in req.headers, false);
|
||||
res.end();
|
||||
|
||||
@@ -33,7 +33,16 @@ describe('AxiosHeaders', function () {
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'value1');
|
||||
assert.strictEqual(headers.get('bar'), 'value2');
|
||||
})
|
||||
});
|
||||
|
||||
it('should support adding multiple headers from raw headers string', function(){
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set(`foo:value1\nbar:value2`);
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'value1');
|
||||
assert.strictEqual(headers.get('bar'), 'value2');
|
||||
});
|
||||
|
||||
it('should not rewrite header the header if the value is false', function(){
|
||||
const headers = new AxiosHeaders();
|
||||
@@ -338,4 +347,49 @@ describe('AxiosHeaders', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('AxiosHeaders.concat', function () {
|
||||
it('should concatenate plain headers into an AxiosHeader instance', function () {
|
||||
const a = {a: 1};
|
||||
const b = {b: 2};
|
||||
const c = {c: 3};
|
||||
const headers = AxiosHeaders.concat(a, b, c);
|
||||
|
||||
assert.deepStrictEqual({...headers.toJSON()}, {
|
||||
a: '1',
|
||||
b: '2',
|
||||
c: '3'
|
||||
});
|
||||
});
|
||||
|
||||
it('should concatenate raw headers into an AxiosHeader instance', function () {
|
||||
const a = 'a:1\nb:2';
|
||||
const b = 'c:3\nx:4';
|
||||
const headers = AxiosHeaders.concat(a, b);
|
||||
|
||||
assert.deepStrictEqual({...headers.toJSON()}, {
|
||||
a: '1',
|
||||
b: '2',
|
||||
c: '3',
|
||||
x: '4'
|
||||
});
|
||||
});
|
||||
|
||||
it('should concatenate Axios headers into a new AxiosHeader instance', function () {
|
||||
const a = new AxiosHeaders({x: 1});
|
||||
const b = new AxiosHeaders({y: 2});
|
||||
const headers = AxiosHeaders.concat(a, b);
|
||||
|
||||
assert.deepStrictEqual({...headers.toJSON()}, {
|
||||
x: '1',
|
||||
y: '2'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('toString', function () {
|
||||
it('should serialize AxiosHeader instance to a raw headers string', function () {
|
||||
assert.deepStrictEqual(new AxiosHeaders({x:1, y:2}).toString(), 'x: 1\ny: 2');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user