Skip to content

Commit

Permalink
Allow unsetting headers by passing null (#382) (#1845)
Browse files Browse the repository at this point in the history
Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
codeclown and jasonsaayman committed Jun 4, 2020
1 parent 4b3947a commit 4879416
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
7 changes: 7 additions & 0 deletions lib/core/dispatchRequest.js
Expand Up @@ -47,6 +47,13 @@ module.exports = function dispatchRequest(config) {
}
);

// Remove header where value is null
utils.forEach(config.headers, function deleteNullValueHeaders(value, key) {
if (value === null) {
delete config.headers[key];
}
});

var adapter = config.adapter || defaults.adapter;

return adapter(config).then(function onAdapterResolution(response) {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Expand Up @@ -294,6 +294,11 @@ function deepMerge(/* obj1, obj2, obj3, ... */) {
}
}

var lastArgument = arguments[arguments.length - 1];
if (lastArgument === null || typeof lastArgument === 'undefined') {
return lastArgument;
}

for (var i = 0, l = arguments.length; i < l; i++) {
forEach(arguments[i], assignValue);
}
Expand Down
15 changes: 15 additions & 0 deletions test/specs/headers.spec.js
Expand Up @@ -42,6 +42,21 @@ describe('headers', function () {
});
});

it('should not set header if value is null', function (done) {
expect(axios.defaults.headers.common['Accept']).toEqual('application/json, text/plain, */*');

axios('/foo', {
headers: {
Accept: null
}
});

getAjaxRequest().then(function (request) {
expect(typeof request.requestHeaders['Accept']).toEqual('undefined');
done();
});
});

it('should add extra headers for post', function (done) {
var headers = axios.defaults.headers.common;

Expand Down
12 changes: 6 additions & 6 deletions test/specs/utils/deepMerge.spec.js
Expand Up @@ -26,15 +26,15 @@ describe('utils::deepMerge', function () {

it('should deepMerge recursively', function () {
var a = {foo: {bar: 123}};
var b = {foo: {baz: 456}, bar: {qux: 789}};
var b = {foo: {baz: 456}, bar: {qux: null}};

expect(deepMerge(a, b)).toEqual({
foo: {
bar: 123,
baz: 456
},
bar: {
qux: 789
qux: null
}
});
});
Expand All @@ -54,13 +54,13 @@ describe('utils::deepMerge', function () {
});

it('handles null and undefined arguments', function () {
expect(deepMerge(undefined, undefined)).toEqual({});
expect(deepMerge(undefined, undefined)).toEqual(undefined);
expect(deepMerge(undefined, {foo: 123})).toEqual({foo: 123});
expect(deepMerge({foo: 123}, undefined)).toEqual({foo: 123});
expect(deepMerge({foo: 123}, undefined)).toEqual(undefined);

expect(deepMerge(null, null)).toEqual({});
expect(deepMerge(null, null)).toEqual(null);
expect(deepMerge(null, {foo: 123})).toEqual({foo: 123});
expect(deepMerge({foo: 123}, null)).toEqual({foo: 123});
expect(deepMerge({foo: 123}, null)).toEqual(null);
});
});

0 comments on commit 4879416

Please sign in to comment.