Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Headers iterators #295

Merged
merged 5 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 80 additions & 19 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
return
}

var support = {
iterable: 'Symbol' in self && 'iterator' in Symbol,
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
new Blob()
return true
} catch(e) {
return false
}
})(),
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
}

function normalizeName(name) {
if (typeof name !== 'string') {
name = String(name)
Expand Down Expand Up @@ -77,6 +91,67 @@
}, this)
}

Headers.prototype.keys = function() {
var items = []
this.forEach(function(value, name) { items.push(name) })
var iterator = {
next: function() {
var value = items.shift()
return {done: value === undefined, value: value}
}
}

if (support.iterable) {
iterator[Symbol.iterator] = function() {
return iterator
}
}

return iterator
}

Headers.prototype.values = function() {
var items = []
this.forEach(function(value) { items.push(value) })
var iterator = {
next: function() {
var value = items.shift()
return {done: value === undefined, value: value}
}
}

if (support.iterable) {
iterator[Symbol.iterator] = function() {
return iterator
}
}

return iterator
}

Headers.prototype.entries = function() {
var items = []
this.forEach(function(value, name) { items.push([name, value]) })
var iterator = {
next: function() {
var value = items.shift()
return {done: value === undefined, value: value}
}
}

if (support.iterable) {
iterator[Symbol.iterator] = function() {
return iterator
}
}

return iterator
}

if (support.iterable) {
Headers.prototype[Symbol.iterator] = Headers.prototype.entries
}

function consumed(body) {
if (body.bodyUsed) {
return Promise.reject(new TypeError('Already read'))
Expand Down Expand Up @@ -107,23 +182,9 @@
return fileReaderReady(reader)
}

var support = {
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
new Blob();
return true
} catch(e) {
return false
}
})(),
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
}

function Body() {
this.bodyUsed = false


this._initBody = function(body) {
this._bodyInit = body
if (typeof body === 'string') {
Expand Down Expand Up @@ -320,9 +381,9 @@
return new Response(null, {status: status, headers: {location: url}})
}

self.Headers = Headers;
self.Request = Request;
self.Response = Response;
self.Headers = Headers
self.Request = Request
self.Response = Response

self.fetch = function(input, init) {
return new Promise(function(resolve, reject) {
Expand All @@ -345,7 +406,7 @@
return xhr.getResponseHeader('X-Request-URL')
}

return;
return
}

xhr.onload = function() {
Expand All @@ -355,7 +416,7 @@
headers: headers(xhr),
url: responseURL()
}
var body = 'response' in xhr ? xhr.response : xhr.responseText;
var body = 'response' in xhr ? xhr.response : xhr.responseText
resolve(new Response(body, options))
}

Expand Down
60 changes: 48 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ suite('Headers', function() {
test('converts field value to string on set and get', function() {
var headers = new Headers()
headers.set('Content-Type', 1)
headers.set('X-CSRF-Token', undefined);
headers.set('X-CSRF-Token', undefined)
assert.equal(headers.get('Content-Type'), '1')
assert.equal(headers.get('X-CSRF-Token'), 'undefined')
})
test('throws TypeError on invalid character in field name', function() {
assert.throws(function() { new Headers({'<Accept>': ['application/json']}) }, TypeError)
assert.throws(function() { new Headers({'Accept:': ['application/json']}) }, TypeError)
assert.throws(function() {
var headers = new Headers();
headers.set({field: 'value'}, 'application/json');
var headers = new Headers()
headers.set({field: 'value'}, 'application/json')
}, TypeError)
})
featureDependent(test, !nativeFirefox, 'is iterable with forEach', function() {
Expand All @@ -220,6 +220,42 @@ suite('Headers', function() {
assert.equal(this, thisArg)
}, thisArg)
})
test('is iterable with keys', function() {
var headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Accept', 'text/plain')
headers.append('Content-Type', 'text/html')

var iterator = headers.keys()
assert.deepEqual({done: false, value: 'accept'}, iterator.next())
assert.deepEqual({done: false, value: 'accept'}, iterator.next())
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())
})
test('is iterable with entries', function() {
var headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Accept', 'text/plain')
headers.append('Content-Type', 'text/html')

var iterator = headers.entries()
assert.deepEqual({done: false, value: ['accept', 'application/json']}, iterator.next())
assert.deepEqual({done: false, value: ['accept', 'text/plain']}, iterator.next())
assert.deepEqual({done: false, value: ['content-type', 'text/html']}, iterator.next())
assert.deepEqual({done: true, value: undefined}, iterator.next())
})
})

// https://fetch.spec.whatwg.org/#request-class
Expand Down Expand Up @@ -527,11 +563,11 @@ suite('Response', function() {
})

test('creates Headers object from raw headers', function() {
var r = new Response('{"foo":"bar"}', {headers: {'content-type': 'application/json'}});
assert.equal(r.headers instanceof Headers, true);
var r = new Response('{"foo":"bar"}', {headers: {'content-type': 'application/json'}})
assert.equal(r.headers instanceof Headers, true)
return r.json().then(function(json){
assert.equal(json.foo, 'bar');
return json;
assert.equal(json.foo, 'bar')
return json
})
})

Expand Down Expand Up @@ -574,7 +610,7 @@ suite('Response', function() {

test('redirect creates redirect Response', function() {
var r = Response.redirect('https://fetch.spec.whatwg.org/', 301)
assert(r instanceof Response);
assert(r instanceof Response)
assert.equal(r.status, 301)
assert.equal(r.headers.get('Location'), 'https://fetch.spec.whatwg.org/')
})
Expand Down Expand Up @@ -949,7 +985,7 @@ suite('Atomic HTTP redirect handling', function() {
// https://fetch.spec.whatwg.org/#concept-request-credentials-mode
suite('credentials mode', function() {
setup(function() {
return fetch('/cookie?name=foo&value=reset', {credentials: 'same-origin'});
return fetch('/cookie?name=foo&value=reset', {credentials: 'same-origin'})
})

featureDependent(suite, exerciseMode === 'native', 'omit', function() {
Expand All @@ -960,7 +996,7 @@ suite('credentials mode', function() {

test('does not accept cookies with implicit omit credentials', function() {
return fetch('/cookie?name=foo&value=bar').then(function() {
return fetch('/cookie?name=foo', {credentials: 'same-origin'});
return fetch('/cookie?name=foo', {credentials: 'same-origin'})
}).then(function(response) {
return response.text()
}).then(function(data) {
Expand All @@ -970,7 +1006,7 @@ suite('credentials mode', function() {

test('does not accept cookies with omit credentials', function() {
return fetch('/cookie?name=foo&value=bar', {credentials: 'omit'}).then(function() {
return fetch('/cookie?name=foo', {credentials: 'same-origin'});
return fetch('/cookie?name=foo', {credentials: 'same-origin'})
}).then(function(response) {
return response.text()
}).then(function(data) {
Expand All @@ -980,7 +1016,7 @@ suite('credentials mode', function() {

test('does not send cookies with implicit omit credentials', function() {
return fetch('/cookie?name=foo&value=bar', {credentials: 'same-origin'}).then(function() {
return fetch('/cookie?name=foo');
return fetch('/cookie?name=foo')
}).then(function(response) {
return response.text()
}).then(function(data) {
Expand Down