mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(headers): fixed common Content-Type header merging; (#5832)
This commit is contained in:
+2
-4
@@ -73,15 +73,13 @@ class Axios {
|
|||||||
// Set config.method
|
// Set config.method
|
||||||
config.method = (config.method || this.defaults.method || 'get').toLowerCase();
|
config.method = (config.method || this.defaults.method || 'get').toLowerCase();
|
||||||
|
|
||||||
let contextHeaders;
|
|
||||||
|
|
||||||
// Flatten headers
|
// Flatten headers
|
||||||
contextHeaders = headers && utils.merge(
|
let contextHeaders = headers && utils.merge(
|
||||||
headers.common,
|
headers.common,
|
||||||
headers[config.method]
|
headers[config.method]
|
||||||
);
|
);
|
||||||
|
|
||||||
contextHeaders && utils.forEach(
|
headers && utils.forEach(
|
||||||
['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
|
['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
|
||||||
(method) => {
|
(method) => {
|
||||||
delete headers[method];
|
delete headers[method];
|
||||||
|
|||||||
+3
-10
@@ -8,10 +8,6 @@ import toURLEncodedForm from '../helpers/toURLEncodedForm.js';
|
|||||||
import platform from '../platform/index.js';
|
import platform from '../platform/index.js';
|
||||||
import formDataToJSON from '../helpers/formDataToJSON.js';
|
import formDataToJSON from '../helpers/formDataToJSON.js';
|
||||||
|
|
||||||
const DEFAULT_CONTENT_TYPE = {
|
|
||||||
'Content-Type': undefined
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It takes a string, tries to parse it, and if it fails, it returns the stringified version
|
* It takes a string, tries to parse it, and if it fails, it returns the stringified version
|
||||||
* of the input
|
* of the input
|
||||||
@@ -150,17 +146,14 @@ const defaults = {
|
|||||||
|
|
||||||
headers: {
|
headers: {
|
||||||
common: {
|
common: {
|
||||||
'Accept': 'application/json, text/plain, */*'
|
'Accept': 'application/json, text/plain, */*',
|
||||||
|
'Content-Type': undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
|
utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch'], (method) => {
|
||||||
defaults.headers[method] = {};
|
defaults.headers[method] = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
|
|
||||||
defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
|
|
||||||
});
|
|
||||||
|
|
||||||
export default defaults;
|
export default defaults;
|
||||||
|
|||||||
@@ -151,12 +151,12 @@ describe('defaults', function () {
|
|||||||
|
|
||||||
getAjaxRequest().then(function (request) {
|
getAjaxRequest().then(function (request) {
|
||||||
expect(request.requestHeaders).toEqual(
|
expect(request.requestHeaders).toEqual(
|
||||||
utils.merge(defaults.headers.common, defaults.headers.get, {
|
AxiosHeaders.concat(defaults.headers.common, defaults.headers.get, {
|
||||||
'X-COMMON-HEADER': 'commonHeaderValue',
|
'X-COMMON-HEADER': 'commonHeaderValue',
|
||||||
'X-GET-HEADER': 'getHeaderValue',
|
'X-GET-HEADER': 'getHeaderValue',
|
||||||
'X-FOO-HEADER': 'fooHeaderValue',
|
'X-FOO-HEADER': 'fooHeaderValue',
|
||||||
'X-BAR-HEADER': 'barHeaderValue'
|
'X-BAR-HEADER': 'barHeaderValue'
|
||||||
})
|
}).toJSON()
|
||||||
);
|
);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import assert from "assert";
|
||||||
|
|
||||||
const {AxiosHeaders} = axios;
|
const {AxiosHeaders} = axios;
|
||||||
|
|
||||||
function testHeaderValue(headers, key, val) {
|
function testHeaderValue(headers, key, val) {
|
||||||
@@ -44,18 +46,35 @@ describe('headers', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add extra headers for post', function (done) {
|
it('should respect common Content-Type header', function () {
|
||||||
const headers = axios.defaults.headers.common;
|
const instance = axios.create();
|
||||||
|
|
||||||
|
instance.defaults.headers.common['Content-Type'] = 'application/custom';
|
||||||
|
|
||||||
|
instance.patch('/foo', "");
|
||||||
|
|
||||||
|
const expectedHeaders = {
|
||||||
|
'Content-Type': "application/custom"
|
||||||
|
};
|
||||||
|
|
||||||
|
return getAjaxRequest().then(function (request) {
|
||||||
|
for (const key in expectedHeaders) {
|
||||||
|
if (expectedHeaders.hasOwnProperty(key)) {
|
||||||
|
expect(request.requestHeaders[key]).toEqual(expectedHeaders[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add extra headers for post', function () {
|
||||||
|
const headers = AxiosHeaders.from(axios.defaults.headers.common).toJSON();
|
||||||
|
|
||||||
axios.post('/foo', 'fizz=buzz');
|
axios.post('/foo', 'fizz=buzz');
|
||||||
|
|
||||||
getAjaxRequest().then(function (request) {
|
return getAjaxRequest().then(function (request) {
|
||||||
for (const key in headers) {
|
for (const key in headers) {
|
||||||
if (headers.hasOwnProperty(key)) {
|
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user