Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
5ba74516
F
fetch
项目概览
github
/
fetch
10 个月 前同步成功
通知
30
Star
25765
Fork
3216
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
fetch
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5ba74516
编写于
1月 05, 2015
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add promise test helper
上级
8654a43f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
50 addition
and
57 deletion
+50
-57
test/test.html
test/test.html
+12
-0
test/test.js
test/test.js
+38
-57
未找到文件。
test/test.html
浏览文件 @
5ba74516
...
...
@@ -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>
...
...
test/test.js
浏览文件 @
5ba74516
...
...
@@ -7,18 +7,17 @@ var blobSupport = (function() {
}
})();
async
Test
(
'
populates response body
'
,
2
,
function
()
{
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
sends request headers
'
,
2
,
function
()
{
fetch
(
'
/request
'
,
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
parses response headers
'
,
2
,
function
()
{
fetch
(
'
/headers?
'
+
new
Date
().
getTime
()).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
resolves promise on 500 error
'
,
2
,
function
()
{
fetch
(
'
/boom
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
rejects promise for network error
'
,
1
,
function
()
{
fetch
(
'
/error
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
handles 204 No Content response
'
,
2
,
function
()
{
fetch
(
'
/empty
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
resolves text promise
'
,
1
,
function
()
{
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
promise
Test
(
'
resolves text promise
'
,
1
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
return
response
.
text
()
}).
then
(
function
(
text
)
{
equal
(
text
,
'
hi
'
)
start
()
})
})
async
Test
(
'
parses form encoded response
'
,
1
,
function
()
{
fetch
(
'
/form
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
parses json response
'
,
2
,
function
()
{
fetch
(
'
/json
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
handles json parse error
'
,
2
,
function
()
{
fetch
(
'
/json-error
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
)
{
async
Test
(
'
resolves blob promise
'
,
2
,
function
()
{
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
}
async
Test
(
'
post sets content-type header
'
,
2
,
function
()
{
fetch
(
'
/request
'
,
{
promise
Test
(
'
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
)
{
async
Test
(
'
rejects blob promise after body is consumed
'
,
2
,
function
()
{
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
}
async
Test
(
'
rejects json promise after body is consumed
'
,
2
,
function
()
{
fetch
(
'
/json
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
rejects text promise after body is consumed
'
,
2
,
function
()
{
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
rejects formData promise after body is consumed
'
,
2
,
function
()
{
fetch
(
'
/json
'
).
then
(
function
(
response
)
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
supports HTTP PUT
'
,
2
,
function
()
{
fetch
(
'
/request
'
,
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
supports HTTP PATCH
'
,
2
,
function
()
{
fetch
(
'
/request
'
,
{
promise
Test
(
'
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
()
})
})
async
Test
(
'
supports HTTP DELETE
'
,
2
,
function
()
{
fetch
(
'
/request
'
,
{
promise
Test
(
'
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录