diff --git a/fetch.js b/fetch.js index a7c0c484..1551d654 100644 --- a/fetch.js +++ b/fetch.js @@ -88,6 +88,17 @@ } } + Headers.prototype.values = function() { + var items = [] + this.forEach(function(value) { items.push(value) }) + return { + next: function() { + var value = items.shift() + return {done: value === undefined, value: value} + } + } + } + function consumed(body) { if (body.bodyUsed) { return Promise.reject(new TypeError('Already read')) diff --git a/test/test.js b/test/test.js index 4dbb7cd2..9e54aa97 100644 --- a/test/test.js +++ b/test/test.js @@ -232,6 +232,18 @@ suite('Headers', function() { assert.deepEqual({done: false, value: 'content-type'}, iterator.next()) assert.deepEqual({done: true, value: undefined}, iterator.next()) }) + test('is iterable with values', function() { + var headers = new Headers() + headers.append('Accept', 'application/json') + headers.append('Accept', 'text/plain') + headers.append('Content-Type', 'text/html') + + var iterator = headers.values() + assert.deepEqual({done: false, value: 'application/json'}, iterator.next()) + assert.deepEqual({done: false, value: 'text/plain'}, iterator.next()) + assert.deepEqual({done: false, value: 'text/html'}, iterator.next()) + assert.deepEqual({done: true, value: undefined}, iterator.next()) + }) }) // https://fetch.spec.whatwg.org/#request-class