Merge pull request #54 from tricoder42/foreach
Add Headers.forEach method.
This commit is contained in:
commit
38b1fc0450
|
@ -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
|
||||||
*
|
*
|
||||||
|
|
23
test/test.js
23
test/test.js
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue