Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(headers): fixed common Content-Type header merging; (#5832)
  • Loading branch information
DigitalBrainJS committed Aug 26, 2023
1 parent d8b4ca0 commit 8fda276
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
6 changes: 2 additions & 4 deletions lib/core/Axios.js
Expand Up @@ -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];
Expand Down
13 changes: 3 additions & 10 deletions lib/defaults/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;
4 changes: 2 additions & 2 deletions test/specs/defaults.spec.js
Expand Up @@ -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();
});
Expand Down
33 changes: 26 additions & 7 deletions test/specs/headers.spec.js
@@ -1,3 +1,5 @@
import assert from "assert";

const {AxiosHeaders} = axios;

function testHeaderValue(headers, key, val) {
Expand Down Expand Up @@ -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();
});
});

Expand Down

0 comments on commit 8fda276

Please sign in to comment.