diff --git a/fetch.js b/fetch.js index f39a983a..6d372304 100644 --- a/fetch.js +++ b/fetch.js @@ -107,8 +107,11 @@ export function Headers(headers) { Headers.prototype.append = function(name, value) { name = normalizeName(name) value = normalizeValue(value) - var oldValue = this.map[name] - this.map[name] = oldValue ? oldValue + ', ' + value : value + if (this.has(name)) { + this.map[name].push(value) + } else { + this.map[name] = [value] + } } Headers.prototype['delete'] = function(name) { @@ -117,7 +120,7 @@ Headers.prototype['delete'] = function(name) { Headers.prototype.get = function(name) { name = normalizeName(name) - return this.has(name) ? this.map[name] : null + return this.has(name) ? this.map[name].join(', ') : null } Headers.prototype.has = function(name) { @@ -125,13 +128,19 @@ Headers.prototype.has = function(name) { } Headers.prototype.set = function(name, value) { - this.map[normalizeName(name)] = normalizeValue(value) + this.map[normalizeName(name)] = [normalizeValue(value)] } Headers.prototype.forEach = function(callback, thisArg) { for (var name in this.map) { if (this.map.hasOwnProperty(name)) { - callback.call(thisArg, this.map[name], name, this) + if (name === 'set-cookie') { + for (var index in this.map[name]) { + callback.call(thisArg, this.map[name][index], name, this) + } + } else { + callback.call(thisArg, this.map[name].join(', '), name, this) + } } } } @@ -160,6 +169,16 @@ Headers.prototype.entries = function() { return iteratorFor(items) } +Headers.prototype.getSetCookie = function() { + var items = [] + if (this.has('set-cookie')) { + for (var index in this.map['set-cookie']) { + items.push(this.map['set-cookie'][index]) + } + } + return items +} + if (support.iterable) { Headers.prototype[Symbol.iterator] = Headers.prototype.entries } diff --git a/test/test.js b/test/test.js index ce0007c0..285dea59 100644 --- a/test/test.js +++ b/test/test.js @@ -337,6 +337,28 @@ exercise.forEach(function(exerciseMode) { assert.deepEqual({done: false, value: ['content-type', 'text/html']}, iterator.next()) assert.deepEqual({done: true, value: undefined}, iterator.next()) }) + test('getSetCookie', function() { + var headers = new Headers(); + + headers.append("Set-Cookie", "x=x"); + headers.append("Set-Cookie", "y=y"); + + assert.equal(headers.get("Set-Cookie"), "x=x, y=y"); + + var forEach = [] + headers.forEach(function(value, key) { + forEach.push({value: value, key: key}) + }) + + assert.equal(forEach.length, 2) + assert.deepEqual({key: 'set-cookie', value: 'x=x'}, forEach[0]) + assert.deepEqual({key: 'set-cookie', value: 'y=y'}, forEach[1]) + + var getSetCookie = headers.getSetCookie(); + assert.equal(getSetCookie.length, 2); + assert.equal(getSetCookie[0], "x=x"); + assert.equal(getSetCookie[1], "y=y"); + }) }) // https://fetch.spec.whatwg.org/#request-class