Add Headers.forEach method.

This commit is contained in:
Tomáš Ehrlich 2015-10-27 06:13:02 +01:00
parent fd3f89fcd9
commit 751cf6cd29
2 changed files with 37 additions and 0 deletions

View File

@ -69,6 +69,20 @@ Headers.prototype.getAll = function(name) {
return this._headers[name.toLowerCase()];
};
/**
* Iterate over all headers
* @param Function callback Executed for each item with parameters (value, name, thisArg)
* @param Boolean thisArg `this` context for callback function
* @return Void
*/
Headers.prototype.forEach = function(callback, thisArg) {
Object.getOwnPropertyNames(this._headers).forEach(function(name) {
this._headers[name].forEach(function(value) {
callback.call(thisArg, value, name, this)
}, this)
}, this)
}
/**
* Overwrite header values given name
*

View File

@ -735,6 +735,29 @@ describe('node-fetch', function() {
});
});
it('should allow iterating through all headers', function() {
var headers = new Headers({
a: 1,
b: [2, 3],
});
var myHeaders = [];
function callback(value, name) {
myHeaders.push([name, value]);
}
expected = [
["a", "1"],
["b", "2"],
["b", "3"]
];
expect(headers.forEach).to.be.defined;
headers.forEach(callback, headers);
expect(myHeaders).to.be.deep.equal(expected);
});
it('should allow deleting header', function() {
url = base + '/cookie';
return fetch(url).then(function(res) {