Skip to content

Commit 8fda276

Browse files
fix(headers): fixed common Content-Type header merging; (#5832)
1 parent d8b4ca0 commit 8fda276

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

lib/core/Axios.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ class Axios {
7373
// Set config.method
7474
config.method = (config.method || this.defaults.method || 'get').toLowerCase();
7575

76-
let contextHeaders;
77-
7876
// Flatten headers
79-
contextHeaders = headers && utils.merge(
77+
let contextHeaders = headers && utils.merge(
8078
headers.common,
8179
headers[config.method]
8280
);
8381

84-
contextHeaders && utils.forEach(
82+
headers && utils.forEach(
8583
['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
8684
(method) => {
8785
delete headers[method];

lib/defaults/index.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import toURLEncodedForm from '../helpers/toURLEncodedForm.js';
88
import platform from '../platform/index.js';
99
import formDataToJSON from '../helpers/formDataToJSON.js';
1010

11-
const DEFAULT_CONTENT_TYPE = {
12-
'Content-Type': undefined
13-
};
14-
1511
/**
1612
* It takes a string, tries to parse it, and if it fails, it returns the stringified version
1713
* of the input
@@ -150,17 +146,14 @@ const defaults = {
150146

151147
headers: {
152148
common: {
153-
'Accept': 'application/json, text/plain, */*'
149+
'Accept': 'application/json, text/plain, */*',
150+
'Content-Type': undefined
154151
}
155152
}
156153
};
157154

158-
utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
155+
utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch'], (method) => {
159156
defaults.headers[method] = {};
160157
});
161158

162-
utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
163-
defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
164-
});
165-
166159
export default defaults;

test/specs/defaults.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ describe('defaults', function () {
151151

152152
getAjaxRequest().then(function (request) {
153153
expect(request.requestHeaders).toEqual(
154-
utils.merge(defaults.headers.common, defaults.headers.get, {
154+
AxiosHeaders.concat(defaults.headers.common, defaults.headers.get, {
155155
'X-COMMON-HEADER': 'commonHeaderValue',
156156
'X-GET-HEADER': 'getHeaderValue',
157157
'X-FOO-HEADER': 'fooHeaderValue',
158158
'X-BAR-HEADER': 'barHeaderValue'
159-
})
159+
}).toJSON()
160160
);
161161
done();
162162
});

test/specs/headers.spec.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import assert from "assert";
2+
13
const {AxiosHeaders} = axios;
24

35
function testHeaderValue(headers, key, val) {
@@ -44,18 +46,35 @@ describe('headers', function () {
4446
});
4547
});
4648

47-
it('should add extra headers for post', function (done) {
48-
const headers = axios.defaults.headers.common;
49+
it('should respect common Content-Type header', function () {
50+
const instance = axios.create();
51+
52+
instance.defaults.headers.common['Content-Type'] = 'application/custom';
53+
54+
instance.patch('/foo', "");
55+
56+
const expectedHeaders = {
57+
'Content-Type': "application/custom"
58+
};
59+
60+
return getAjaxRequest().then(function (request) {
61+
for (const key in expectedHeaders) {
62+
if (expectedHeaders.hasOwnProperty(key)) {
63+
expect(request.requestHeaders[key]).toEqual(expectedHeaders[key]);
64+
}
65+
}
66+
});
67+
});
68+
69+
it('should add extra headers for post', function () {
70+
const headers = AxiosHeaders.from(axios.defaults.headers.common).toJSON();
4971

5072
axios.post('/foo', 'fizz=buzz');
5173

52-
getAjaxRequest().then(function (request) {
74+
return getAjaxRequest().then(function (request) {
5375
for (const key in headers) {
54-
if (headers.hasOwnProperty(key)) {
55-
expect(request.requestHeaders[key]).toEqual(headers[key]);
56-
}
76+
expect(request.requestHeaders[key]).toEqual(headers[key]);
5777
}
58-
done();
5978
});
6079
});
6180

0 commit comments

Comments
 (0)