Skip to content

Commit

Permalink
fix(headers): fixed common Content-Type header merging;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Aug 25, 2023
1 parent 2200038 commit 5864b35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
10 changes: 1 addition & 9 deletions lib/defaults/index.js
Original file line number Diff line number Diff line change
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 @@ -155,12 +151,8 @@ const defaults = {
}
};

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;
22 changes: 22 additions & 0 deletions test/specs/headers.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert from "assert";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import assert.

const {AxiosHeaders} = axios;

function testHeaderValue(headers, key, val) {
Expand Down Expand Up @@ -44,6 +46,26 @@ describe('headers', function () {
});
});

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 (done) {
const headers = axios.defaults.headers.common;

Expand Down

0 comments on commit 5864b35

Please sign in to comment.