提交 b9178556 编写于 作者: N Nikhil Marathe

Merge branch 'master' of github.com:nikhilm/fetch

......@@ -156,4 +156,4 @@ header hack.
![Chrome](https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png)
--- | --- | --- | --- | --- |
Latest ✔ | Latest ✔ | 10+ ✔ | Latest ✔ | 6.1+ ✔ |
Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | 6.1+ ✔ |
......@@ -187,6 +187,19 @@
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest()
function responseURL() {
if ('responseURL' in xhr) {
return xhr.responseURL
}
// Avoid security warnings on getResponseHeader when not allowed by CORS
if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
return xhr.getResponseHeader('X-Request-URL')
}
return;
}
xhr.onload = function() {
var status = (xhr.status === 1223) ? 204 : xhr.status
if (status < 100 || status > 599) {
......@@ -197,9 +210,10 @@
status: status,
statusText: xhr.statusText,
headers: headers(xhr),
url: xhr.responseURL || xhr.getResponseHeader('X-Request-URL')
url: responseURL()
}
resolve(new Response(blobSupport ? xhr.response : xhr.responseText, options))
var body = 'response' in xhr ? xhr.response : xhr.responseText;
resolve(new Response(body, options))
}
xhr.onerror = function() {
......@@ -207,7 +221,7 @@
}
xhr.open(self.method, self.url)
if (blobSupport) {
if ('responseType' in xhr && blobSupport) {
xhr.responseType = 'blob'
}
......
......@@ -28,6 +28,22 @@ var routes = {
});
res.end('hi');
},
'/hello/utf8': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
// "hello"
var buf = new Buffer([104, 101, 108, 108, 111]);
res.end(buf);
},
'/hello/utf16le': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-16le'
});
// "hello"
var buf = new Buffer([104, 0, 101, 0, 108, 0, 108, 0, 111, 0]);
res.end(buf);
},
'/binary': function(res) {
res.writeHead(200, {'Content-Type': 'application/octet-stream'});
var buf = new Buffer(256);
......
function readBlobAsText(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
resolve(reader.result)
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsText(blob)
})
}
function readBlobAsBytes(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
var view = new Uint8Array(reader.result)
resolve(Array.prototype.slice.call(view))
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsArrayBuffer(blob)
})
}
test('resolves promise on 500 error', function() {
return fetch('/boom').then(function(response) {
assert.equal(response.status, 500)
......@@ -44,19 +71,6 @@ suite('Request', function() {
// https://fetch.spec.whatwg.org/#response-class
suite('Response', function() {
function readBlobAsText(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
resolve(reader.result)
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsText(blob)
})
}
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite('BodyInit extract', function() {
;(Response.prototype.blob ? suite : suite.skip)('type Blob', function() {
......@@ -134,10 +148,34 @@ suite('Body mixin', function() {
})
})
test('arrayBuffer handles utf-8 data', function() {
return fetch('/hello/utf8').then(function(response) {
return response.arrayBuffer()
}).then(function(buf) {
assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance')
assert.equal(buf.byteLength, 5, 'buf.byteLength is correct')
var octets = Array.prototype.slice.call(new Uint8Array(buf))
assert.deepEqual(octets, [104, 101, 108, 108, 111])
})
})
test('arrayBuffer handles utf-16le data', function() {
return fetch('/hello/utf16le').then(function(response) {
return response.arrayBuffer()
}).then(function(buf) {
assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance')
assert.equal(buf.byteLength, 10, 'buf.byteLength is correct')
var octets = Array.prototype.slice.call(new Uint8Array(buf))
assert.deepEqual(octets, [104, 0, 101, 0, 108, 0, 108, 0, 111, 0])
})
})
test('rejects arrayBuffer promise after body is consumed', function() {
return fetch('/hello').then(function(response) {
assert(response.arrayBuffer, 'Body does not implement arrayBuffer')
assert.equal(response.bodyUsed, false)
response.blob()
assert.equal(response.bodyUsed, true)
return response.arrayBuffer()
}).catch(function(error) {
assert(error instanceof TypeError, 'Promise rejected after body consumed')
......@@ -164,10 +202,30 @@ suite('Body mixin', function() {
})
})
test('blob handles utf-8 data', function() {
return fetch('/hello/utf8').then(function(response) {
return response.blob()
}).then(readBlobAsBytes).then(function(octets) {
assert.equal(octets.length, 5, 'blob.size is correct')
assert.deepEqual(octets, [104, 101, 108, 108, 111])
})
})
test('blob handles utf-16le data', function() {
return fetch('/hello/utf16le').then(function(response) {
return response.blob()
}).then(readBlobAsBytes).then(function(octets) {
assert.equal(octets.length, 10, 'blob.size is correct')
assert.deepEqual(octets, [104, 0, 101, 0, 108, 0, 108, 0, 111, 0])
})
})
test('rejects blob promise after body is consumed', function() {
return fetch('/hello').then(function(response) {
assert(response.blob, 'Body does not implement blob')
response.blob()
assert.equal(response.bodyUsed, false)
response.text()
assert.equal(response.bodyUsed, true)
return response.blob()
}).catch(function(error) {
assert(error instanceof TypeError, 'Promise rejected after body consumed')
......@@ -220,7 +278,9 @@ suite('Body mixin', function() {
test('rejects json promise after body is consumed', function() {
return fetch('/json').then(function(response) {
assert(response.json, 'Body does not implement json')
response.json()
assert.equal(response.bodyUsed, false)
response.text()
assert.equal(response.bodyUsed, true)
return response.json()
}).catch(function(error) {
assert(error instanceof TypeError, 'Promise rejected after body consumed')
......@@ -258,7 +318,9 @@ suite('Body mixin', function() {
test('rejects text promise after body is consumed', function() {
return fetch('/hello').then(function(response) {
assert(response.text, 'Body does not implement text')
response.text()
assert.equal(response.bodyUsed, false)
response.json()
assert.equal(response.bodyUsed, true)
return response.text()
}).catch(function(error) {
assert(error instanceof TypeError, 'Promise rejected after body consumed')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册