提交 9b1ddc9e 编写于 作者: J Joshua Peek

Merge pull request #54 from github/optional-form-data-support

Mark FormData support as optional
...@@ -76,9 +76,20 @@ ...@@ -76,9 +76,20 @@
throw new Error('Not implemented yet') throw new Error('Not implemented yet')
} }
this.blob = function() { var blobSupport = (function() {
var rejected = consumed(this) try {
return rejected ? rejected : Promise.resolve(new Blob([this._body])) new Blob();
return true
} catch(e) {
return false
}
})();
if (blobSupport) {
this.blob = function() {
var rejected = consumed(this)
return rejected ? rejected : Promise.resolve(new Blob([this._body]))
}
} }
this.formData = function() { this.formData = function() {
......
...@@ -21,6 +21,9 @@ ...@@ -21,6 +21,9 @@
"MockXHR": false, "MockXHR": false,
"QUnit": false, "QUnit": false,
"fetch": false, "fetch": false,
"Headers": false,
"Request": false,
"Response": false,
"module": false, "module": false,
"test": false, "test": false,
"asyncTest": false, "asyncTest": false,
......
var blobSupport = (function() {
try {
new Blob();
return true
} catch(e) {
return false
}
})();
promiseTest('populates response body', 2, function() { promiseTest('populates response body', 2, function() {
return fetch('/hello').then(function(response) { return fetch('/hello').then(function(response) {
equal(response.status, 200) equal(response.status, 200)
...@@ -72,13 +63,15 @@ promiseTest('resolves text promise', 1, function() { ...@@ -72,13 +63,15 @@ promiseTest('resolves text promise', 1, function() {
}) })
}) })
promiseTest('parses form encoded response', 1, function() { if (Response.prototype.formData) {
return fetch('/form').then(function(response) { promiseTest('parses form encoded response', 1, function() {
return response.formData() return fetch('/form').then(function(response) {
}).then(function(form) { return response.formData()
ok(form instanceof FormData, 'Parsed a FormData object') }).then(function(form) {
ok(form instanceof FormData, 'Parsed a FormData object')
})
}) })
}) }
promiseTest('parses json response', 2, function() { promiseTest('parses json response', 2, function() {
return fetch('/json').then(function(response) { return fetch('/json').then(function(response) {
...@@ -98,7 +91,7 @@ promiseTest('handles json parse error', 2, function() { ...@@ -98,7 +91,7 @@ promiseTest('handles json parse error', 2, function() {
}) })
}) })
if (blobSupport) { if (Response.prototype.blob) {
promiseTest('resolves blob promise', 2, function() { promiseTest('resolves blob promise', 2, function() {
return fetch('/hello').then(function(response) { return fetch('/hello').then(function(response) {
return response.blob() return response.blob()
...@@ -121,7 +114,7 @@ promiseTest('post sets content-type header', 2, function() { ...@@ -121,7 +114,7 @@ promiseTest('post sets content-type header', 2, function() {
}) })
}) })
if (blobSupport) { if (Response.prototype.blob) {
promiseTest('rejects blob promise after body is consumed', 2, function() { promiseTest('rejects blob promise after body is consumed', 2, function() {
return fetch('/hello').then(function(response) { return fetch('/hello').then(function(response) {
ok(response.blob, 'Body does not implement blob') ok(response.blob, 'Body does not implement blob')
...@@ -153,15 +146,17 @@ promiseTest('rejects text promise after body is consumed', 2, function() { ...@@ -153,15 +146,17 @@ promiseTest('rejects text promise after body is consumed', 2, function() {
}) })
}) })
promiseTest('rejects formData promise after body is consumed', 2, function() { if (Response.prototype.formData) {
return fetch('/json').then(function(response) { promiseTest('rejects formData promise after body is consumed', 2, function() {
ok(response.formData, 'Body does not implement formData') return fetch('/json').then(function(response) {
response.formData() ok(response.formData, 'Body does not implement formData')
return response.formData() response.formData()
}).catch(function(error) { return response.formData()
ok(error instanceof TypeError, 'Promise rejected after body consumed') }).catch(function(error) {
ok(error instanceof TypeError, 'Promise rejected after body consumed')
})
}) })
}) }
promiseTest('supports HTTP PUT', 2, function() { promiseTest('supports HTTP PUT', 2, function() {
return fetch('/request', { return fetch('/request', {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册