提交 5ba74516 编写于 作者: J Joshua Peek

Add promise test helper

上级 8654a43f
......@@ -11,6 +11,18 @@
<script src="../bower_components/es6-promise/promise.js"></script>
<script src="../fetch.js"></script>
<script src="../bower_components/qunit/qunit/qunit.js"></script>
<script>
QUnit.promiseTest = function(testName, expected, callback) {
QUnit.test(testName, expected, function() {
stop();
Promise.resolve().then(callback).then(start, function(error) {
ok(false, error);
start();
});
});
}
window.promiseTest = QUnit.promiseTest;
</script>
<script>QUnit.config.testTimeout = 1000</script>
<script src="./test.js"></script>
</body>
......
......@@ -7,18 +7,17 @@ var blobSupport = (function() {
}
})();
asyncTest('populates response body', 2, function() {
fetch('/hello').then(function(response) {
promiseTest('populates response body', 2, function() {
return fetch('/hello').then(function(response) {
equal(response.status, 200)
return response.text()
}).then(function(body) {
equal(body, 'hi')
start()
})
})
asyncTest('sends request headers', 2, function() {
fetch('/request', {
promiseTest('sends request headers', 2, function() {
return fetch('/request', {
headers: {
'Accept': 'application/json',
'X-Test': '42'
......@@ -28,100 +27,90 @@ asyncTest('sends request headers', 2, function() {
}).then(function(json) {
equal(json.headers['accept'], 'application/json')
equal(json.headers['x-test'], '42')
start()
})
})
asyncTest('parses response headers', 2, function() {
fetch('/headers?' + new Date().getTime()).then(function(response) {
promiseTest('parses response headers', 2, function() {
return fetch('/headers?' + new Date().getTime()).then(function(response) {
equal(response.headers.get('Date'), 'Mon, 13 Oct 2014 21:02:27 GMT')
equal(response.headers.get('Content-Type'), 'text/html; charset=utf-8')
start()
})
})
asyncTest('resolves promise on 500 error', 2, function() {
fetch('/boom').then(function(response) {
promiseTest('resolves promise on 500 error', 2, function() {
return fetch('/boom').then(function(response) {
equal(response.status, 500)
return response.text()
}).then(function(body) {
equal(body, 'boom')
start()
})
})
asyncTest('rejects promise for network error', 1, function() {
fetch('/error').then(function(response) {
promiseTest('rejects promise for network error', 1, function() {
return fetch('/error').then(function(response) {
ok(false, 'HTTP status ' + response.status + ' was treated as success')
start()
}).catch(function(error) {
ok(error instanceof TypeError, 'Rejected with Error')
start()
})
})
asyncTest('handles 204 No Content response', 2, function() {
fetch('/empty').then(function(response) {
promiseTest('handles 204 No Content response', 2, function() {
return fetch('/empty').then(function(response) {
equal(response.status, 204)
return response.text()
}).then(function(body) {
equal(body, '')
start()
})
})
asyncTest('resolves text promise', 1, function() {
fetch('/hello').then(function(response) {
promiseTest('resolves text promise', 1, function() {
return fetch('/hello').then(function(response) {
return response.text()
}).then(function(text) {
equal(text, 'hi')
start()
})
})
asyncTest('parses form encoded response', 1, function() {
fetch('/form').then(function(response) {
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')
start()
})
})
asyncTest('parses json response', 2, function() {
fetch('/json').then(function(response) {
promiseTest('parses json response', 2, function() {
return fetch('/json').then(function(response) {
return response.json()
}).then(function(json) {
equal(json.name, 'Hubot')
equal(json.login, 'hubot')
start()
})
})
asyncTest('handles json parse error', 2, function() {
fetch('/json-error').then(function(response) {
promiseTest('handles json parse error', 2, function() {
return fetch('/json-error').then(function(response) {
return response.json()
}).catch(function(error) {
ok(error instanceof Error, 'JSON exception is an Error instance')
ok(error.message, 'JSON exception has an error message')
start()
})
})
if (blobSupport) {
asyncTest('resolves blob promise', 2, function() {
fetch('/hello').then(function(response) {
promiseTest('resolves blob promise', 2, function() {
return fetch('/hello').then(function(response) {
return response.blob()
}).then(function(blob) {
ok(blob instanceof Blob, 'blob is a Blob instance')
equal(blob.size, 2)
start()
})
})
}
asyncTest('post sets content-type header', 2, function() {
fetch('/request', {
promiseTest('post sets content-type header', 2, function() {
return fetch('/request', {
method: 'post',
body: new FormData()
}).then(function(response) {
......@@ -129,58 +118,53 @@ asyncTest('post sets content-type header', 2, function() {
}).then(function(json) {
equal(json.method, 'POST')
ok(/^multipart\/form-data;/.test(json.headers['content-type']))
start()
})
})
if (blobSupport) {
asyncTest('rejects blob promise after body is consumed', 2, function() {
fetch('/hello').then(function(response) {
promiseTest('rejects blob promise after body is consumed', 2, function() {
return fetch('/hello').then(function(response) {
ok(response.blob, 'Body does not implement blob')
response.blob()
return response.blob()
}).catch(function(error) {
ok(error instanceof TypeError, 'Promise rejected after body consumed')
start()
})
})
}
asyncTest('rejects json promise after body is consumed', 2, function() {
fetch('/json').then(function(response) {
promiseTest('rejects json promise after body is consumed', 2, function() {
return fetch('/json').then(function(response) {
ok(response.json, 'Body does not implement json')
response.json()
return response.json()
}).catch(function(error) {
ok(error instanceof TypeError, 'Promise rejected after body consumed')
start()
})
})
asyncTest('rejects text promise after body is consumed', 2, function() {
fetch('/hello').then(function(response) {
promiseTest('rejects text promise after body is consumed', 2, function() {
return fetch('/hello').then(function(response) {
ok(response.text, 'Body does not implement text')
response.text()
return response.text()
}).catch(function(error) {
ok(error instanceof TypeError, 'Promise rejected after body consumed')
start()
})
})
asyncTest('rejects formData promise after body is consumed', 2, function() {
fetch('/json').then(function(response) {
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')
start()
})
})
asyncTest('supports HTTP PUT', 2, function() {
fetch('/request', {
promiseTest('supports HTTP PUT', 2, function() {
return fetch('/request', {
method: 'put',
body: 'name=Hubot'
}).then(function(response) {
......@@ -188,12 +172,11 @@ asyncTest('supports HTTP PUT', 2, function() {
}).then(function(request) {
equal(request.method, 'PUT')
equal(request.data, 'name=Hubot')
start()
})
})
asyncTest('supports HTTP PATCH', 2, function() {
fetch('/request', {
promiseTest('supports HTTP PATCH', 2, function() {
return fetch('/request', {
method: 'PATCH',
body: 'name=Hubot'
}).then(function(response) {
......@@ -205,18 +188,16 @@ asyncTest('supports HTTP PATCH', 2, function() {
} else {
equal(request.data, 'name=Hubot')
}
start()
})
})
asyncTest('supports HTTP DELETE', 2, function() {
fetch('/request', {
promiseTest('supports HTTP DELETE', 2, function() {
return fetch('/request', {
method: 'delete',
}).then(function(response) {
return response.json()
}).then(function(request) {
equal(request.method, 'DELETE')
equal(request.data, '')
start()
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册