mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(headers): fixed the filtering logic of the clear method; (#5542)
This commit is contained in:
@@ -33,11 +33,15 @@ function isValidHeaderName(str) {
|
|||||||
return /^[-_a-zA-Z]+$/.test(str.trim());
|
return /^[-_a-zA-Z]+$/.test(str.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchHeaderValue(context, value, header, filter) {
|
function matchHeaderValue(context, value, header, filter, isHeaderNameFilter) {
|
||||||
if (utils.isFunction(filter)) {
|
if (utils.isFunction(filter)) {
|
||||||
return filter.call(this, value, header);
|
return filter.call(this, value, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isHeaderNameFilter) {
|
||||||
|
value = header;
|
||||||
|
}
|
||||||
|
|
||||||
if (!utils.isString(value)) return;
|
if (!utils.isString(value)) return;
|
||||||
|
|
||||||
if (utils.isString(filter)) {
|
if (utils.isString(filter)) {
|
||||||
@@ -181,7 +185,7 @@ class AxiosHeaders {
|
|||||||
|
|
||||||
while (i--) {
|
while (i--) {
|
||||||
const key = keys[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];
|
delete this[key];
|
||||||
deleted = true;
|
deleted = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -242,17 +242,17 @@ describe('AxiosHeaders', function () {
|
|||||||
|
|
||||||
headers.clear();
|
headers.clear();
|
||||||
|
|
||||||
assert.notDeepStrictEqual(headers, {});
|
assert.deepStrictEqual({...headers.toJSON()}, {});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should clear matching headers if a matcher was specified', () => {
|
it('should clear matching headers if a matcher was specified', () => {
|
||||||
const headers = new AxiosHeaders({foo: 1, 'x-foo': 2, bar: 3});
|
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-/);
|
headers.clear(/^x-/);
|
||||||
|
|
||||||
assert.notDeepStrictEqual(headers, {foo: 1, bar: 3});
|
assert.deepStrictEqual({...headers.toJSON()}, {foo: '1', bar: '3'});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user