Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(headers): fixed common Content-Type header merging; #5832

Merged
merged 23 commits into from Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3f2f496
chore(ci): Add release-it script;
DigitalBrainJS Dec 10, 2022
1bfc41b
Merge branch 'chore/release-it' into v1.x
DigitalBrainJS Dec 10, 2022
acaf0f4
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 15, 2022
8a5de86
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 15, 2022
cff9271
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 17, 2022
67afe20
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 19, 2022
dc7b66d
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 22, 2022
716eb59
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 23, 2022
10216bf
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 29, 2022
3b199f4
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 7, 2023
f3d444f
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 19, 2023
077c381
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 26, 2023
f598657
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 31, 2023
7bf5713
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Feb 5, 2023
81a8bd6
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Mar 8, 2023
f8bb158
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 5, 2023
3f1c768
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 18, 2023
a9b0418
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 25, 2023
1a16f4e
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 27, 2023
5e22e2f
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS May 10, 2023
b7c77b0
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 25, 2023
2200038
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 25, 2023
0329fd2
fix(headers): fixed common `Content-Type` header merging;
DigitalBrainJS Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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";
Dismissed Show dismissed Hide dismissed

const {AxiosHeaders} = axios;

function testHeaderValue(headers, key, val) {
Expand Down Expand Up @@ -44,18 +46,35 @@
});
});

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