From ea87ebfe6d1699af072b9e7cd40faf8f14b0ab93 Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Sat, 11 Feb 2023 16:31:03 +0200 Subject: [PATCH] fix(headers): fixed the filtering logic of the clear method; (#5542) --- lib/core/AxiosHeaders.js | 8 ++++++-- test/unit/core/AxiosHeaders.js | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/core/AxiosHeaders.js b/lib/core/AxiosHeaders.js index 1cf84b9458..138384caf3 100644 --- a/lib/core/AxiosHeaders.js +++ b/lib/core/AxiosHeaders.js @@ -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)) { @@ -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; } diff --git a/test/unit/core/AxiosHeaders.js b/test/unit/core/AxiosHeaders.js index e52efb78c5..23f824ed94 100644 --- a/test/unit/core/AxiosHeaders.js +++ b/test/unit/core/AxiosHeaders.js @@ -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'}); }); });