Merge pull request #54 from tricoder42/foreach

Add Headers.forEach method.
This commit is contained in:
David Frank 2015-10-28 17:37:56 +08:00
commit 38b1fc0450
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()]; 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 * 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() { it('should allow deleting header', function() {
url = base + '/cookie'; url = base + '/cookie';
return fetch(url).then(function(res) { return fetch(url).then(function(res) {