Skip to content

Commit

Permalink
fix(headers): fixed the filtering logic of the clear method; (#5542)
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Feb 11, 2023
1 parent 0b44929 commit ea87ebf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/core/AxiosHeaders.js
Expand Up @@ -33,11 +33,15 @@ function isValidHeaderName(str) {
return /^[-_a-zA-Z]+$/.test(str.trim());
}

function matchHeaderValue(context, value, header, filter) {
function matchHeaderValue(context, value, header, filter, isHeaderNameFilter) {
if (utils.isFunction(filter)) {
return filter.call(this, value, header);
}

if (isHeaderNameFilter) {
value = header;
}

if (!utils.isString(value)) return;

if (utils.isString(filter)) {
Expand Down Expand Up @@ -181,7 +185,7 @@ class AxiosHeaders {

while (i--) {
const key = keys[i];
if(!matcher || matchHeaderValue(this, this[key], key, matcher)) {
if(!matcher || matchHeaderValue(this, this[key], key, matcher, true)) {
delete this[key];
deleted = true;
}
Expand Down
6 changes: 3 additions & 3 deletions test/unit/core/AxiosHeaders.js
Expand Up @@ -242,17 +242,17 @@ describe('AxiosHeaders', function () {

headers.clear();

assert.notDeepStrictEqual(headers, {});
assert.deepStrictEqual({...headers.toJSON()}, {});
});

it('should clear matching headers if a matcher was specified', () => {
const headers = new AxiosHeaders({foo: 1, 'x-foo': 2, bar: 3});

assert.notDeepStrictEqual(headers, {foo: 1, 'x-foo': 2, bar: 3});
assert.deepStrictEqual({...headers.toJSON()}, {foo: '1', 'x-foo': '2', bar: '3'});

headers.clear(/^x-/);

assert.notDeepStrictEqual(headers, {foo: 1, bar: 3});
assert.deepStrictEqual({...headers.toJSON()}, {foo: '1', bar: '3'});
});
});

Expand Down

0 comments on commit ea87ebf

Please sign in to comment.