diff --git a/lib/core/Axios.js b/lib/core/Axios.js index 7a6e6f5d44..465d765038 100644 --- a/lib/core/Axios.js +++ b/lib/core/Axios.js @@ -73,15 +73,13 @@ class Axios { // Set config.method config.method = (config.method || this.defaults.method || 'get').toLowerCase(); - let contextHeaders; - // Flatten headers - contextHeaders = headers && utils.merge( + let contextHeaders = headers && utils.merge( headers.common, headers[config.method] ); - contextHeaders && utils.forEach( + headers && utils.forEach( ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'], (method) => { delete headers[method]; diff --git a/lib/defaults/index.js b/lib/defaults/index.js index 0b4760219b..a883bfe5c5 100644 --- a/lib/defaults/index.js +++ b/lib/defaults/index.js @@ -8,10 +8,6 @@ import toURLEncodedForm from '../helpers/toURLEncodedForm.js'; import platform from '../platform/index.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 * of the input @@ -150,17 +146,14 @@ const defaults = { headers: { 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] = {}; }); -utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) { - defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE); -}); - export default defaults; diff --git a/test/specs/defaults.spec.js b/test/specs/defaults.spec.js index 175ff47781..46c957b8ec 100644 --- a/test/specs/defaults.spec.js +++ b/test/specs/defaults.spec.js @@ -151,12 +151,12 @@ describe('defaults', function () { getAjaxRequest().then(function (request) { 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-GET-HEADER': 'getHeaderValue', 'X-FOO-HEADER': 'fooHeaderValue', 'X-BAR-HEADER': 'barHeaderValue' - }) + }).toJSON() ); done(); }); diff --git a/test/specs/headers.spec.js b/test/specs/headers.spec.js index 765fd3c564..ffc725d5ff 100644 --- a/test/specs/headers.spec.js +++ b/test/specs/headers.spec.js @@ -1,3 +1,5 @@ +import assert from "assert"; + const {AxiosHeaders} = axios; function testHeaderValue(headers, key, val) { @@ -44,18 +46,35 @@ describe('headers', function () { }); }); - it('should add extra headers for post', function (done) { - const headers = axios.defaults.headers.common; + it('should respect common Content-Type header', function () { + 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'); - getAjaxRequest().then(function (request) { + return getAjaxRequest().then(function (request) { for (const key in headers) { - if (headers.hasOwnProperty(key)) { - expect(request.requestHeaders[key]).toEqual(headers[key]); - } + expect(request.requestHeaders[key]).toEqual(headers[key]); } - done(); }); });