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

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

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