Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
4e00ced5
F
fetch
项目概览
github
/
fetch
大约 1 年 前同步成功
通知
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,发现更多精彩内容 >>
提交
4e00ced5
编写于
1月 18, 2016
作者:
M
Mislav Marohnić
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #258 from mislav/exercise-polyfill
Exercise both polyfill and native `fetch` in test suite
上级
1df62350
4dce072f
变更
10
显示空白变更内容
内联
并排
Showing
10 changed file
with
122 addition
and
69 deletion
+122
-69
.jshintrc
.jshintrc
+4
-1
package.json
package.json
+2
-2
script/phantomjs
script/phantomjs
+13
-2
script/saucelabs
script/saucelabs
+1
-1
script/server
script/server
+7
-2
test/.jshintrc
test/.jshintrc
+4
-17
test/test-worker.html
test/test-worker.html
+3
-3
test/test.html
test/test.html
+12
-9
test/test.js
test/test.js
+71
-26
test/worker.js
test/worker.js
+5
-6
未找到文件。
.jshintrc
浏览文件 @
4e00ced5
...
...
@@ -17,5 +17,8 @@
"esnext": true,
"eqnull": true,
"browser": true,
"worker": true
"worker": true,
"globals": {
"JSON": false
}
}
package.json
浏览文件 @
4e00ced5
...
...
@@ -13,10 +13,10 @@
"devDependencies"
:
{
"
bower
"
:
"
1.3.8
"
,
"
chai
"
:
"
1.10.0
"
,
"
jshint
"
:
"
2.
5.2
"
,
"
jshint
"
:
"
2.
8.0
"
,
"
mocha-phantomjs
"
:
"
3.5.2
"
,
"
mocha
"
:
"
2.1.0
"
,
"
phantomjs
"
:
"
1.9.1
3
"
"
phantomjs
"
:
"
1.9.1
9
"
},
"files"
:
[
"LICENSE"
,
...
...
script/phantomjs
浏览文件 @
4e00ced5
...
...
@@ -12,5 +12,16 @@ node ./script/server $port &>/dev/null &
server_pid
=
$!
trap
"kill
$server_pid
"
INT EXIT
node ./node_modules/.bin/mocha-phantomjs
-s
localToRemoteUrlAccessEnabled
=
true
-s
webSecurityEnabled
=
false
"http://localhost:
$port
/test/test.html"
node ./node_modules/.bin/mocha-phantomjs
-s
localToRemoteUrlAccessEnabled
=
true
-s
webSecurityEnabled
=
false
"http://localhost:
$port
/test/test-worker.html"
STATUS
=
0
run
()
{
node ./node_modules/.bin/mocha-phantomjs
\
-s
localToRemoteUrlAccessEnabled
=
true
\
-s
webSecurityEnabled
=
false
\
"
$@
"
||
STATUS
=
$?
}
run
"http://localhost:
$port
/"
run
"http://localhost:
$port
/test/test-worker.html"
exit
$STATUS
script/saucelabs
浏览文件 @
4e00ced5
...
...
@@ -25,7 +25,7 @@ job="$(./script/saucelabs-api --raw "js-tests" <<JSON
"tags": ["
$TRAVIS_PULL_REQUEST
", "
$TRAVIS_BRANCH
"],
"tunnel-identifier": "
$TRAVIS_JOB_NUMBER
",
"platforms": [["
$SAUCE_PLATFORM
", "
$SAUCE_BROWSER
", "
$SAUCE_VERSION
"]],
"url": "http://localhost:
$port
/
test/test.html
",
"url": "http://localhost:
$port
/",
"framework": "mocha"
}
JSON
...
...
script/server
浏览文件 @
4e00ced5
...
...
@@ -123,12 +123,13 @@ var types = {
txt
:
'
text/plain
'
};
http
.
createServer
(
function
(
req
,
res
)
{
server
=
http
.
createServer
(
function
(
req
,
res
)
{
var
pathname
=
url
.
parse
(
req
.
url
).
pathname
;
var
route
=
routes
[
pathname
];
if
(
route
)
{
route
(
res
,
req
);
}
else
{
if
(
pathname
==
'
/
'
)
pathname
=
'
/test/test.html
'
fs
.
readFile
(
__dirname
+
'
/..
'
+
pathname
,
function
(
err
,
data
)
{
if
(
err
)
{
res
.
writeHead
(
404
,
{
'
Content-Type
'
:
types
.
txt
});
...
...
@@ -140,4 +141,8 @@ http.createServer(function(req, res) {
}
});
}
}).
listen
(
port
);
});
console
.
warn
(
"
Started test server on localhost:
"
+
port
);
server
.
listen
(
port
);
test/.jshintrc
浏览文件 @
4e00ced5
{
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": true,
"undef": true,
"unused": true,
"trailing": true,
"asi": true,
"boss": true,
"esnext": true,
"extends": "../.jshintrc",
"es3": false,
"strict": false,
"sub": true,
"eqnull": true,
"browser": true,
"worker": true,
"globals": {
"fetch": false,
"Headers": false,
...
...
@@ -26,6 +12,7 @@
"chai": false,
"suite": false,
"setup": false,
"suiteSetup": false,
"test": false,
"assert": false
}
...
...
test/test-worker.html
浏览文件 @
4e00ced5
...
...
@@ -3,16 +3,16 @@
<head>
<meta
charset=
"utf-8"
>
<title>
Fetch Worker Tests
</title>
<link
rel=
"stylesheet"
href=
"
..
/node_modules/mocha/mocha.css"
/>
<link
rel=
"stylesheet"
href=
"/node_modules/mocha/mocha.css"
/>
</head>
<body>
<div
id=
"mocha"
></div>
<script
src=
"
..
/node_modules/mocha/mocha.js"
></script>
<script
src=
"/node_modules/mocha/mocha.js"
></script>
<script>
mocha
.
setup
(
'
tdd
'
)
var
worker
=
new
Worker
(
'
worker.js
'
)
var
worker
=
new
Worker
(
'
/test/
worker.js
'
)
worker
.
addEventListener
(
'
message
'
,
function
(
e
)
{
switch
(
e
.
data
.
name
)
{
...
...
test/test.html
浏览文件 @
4e00ced5
...
...
@@ -3,21 +3,24 @@
<head>
<meta
charset=
"utf-8"
>
<title>
Fetch Tests
</title>
<link
rel=
"stylesheet"
href=
"
..
/node_modules/mocha/mocha.css"
/>
<link
rel=
"stylesheet"
href=
"/node_modules/mocha/mocha.css"
/>
</head>
<body>
<div
id=
"mocha"
></div>
<script
src=
"
..
/node_modules/chai/chai.js"
></script>
<script
src=
"
..
/node_modules/mocha/mocha.js"
></script>
<script
src=
"/node_modules/chai/chai.js"
></script>
<script
src=
"/node_modules/mocha/mocha.js"
></script>
<script>
if
(
self
.
mocha
&&
mocha
.
setup
)
{
mocha
.
setup
(
'
tdd
'
)
self
.
assert
=
chai
.
assert
}
else
{
document
.
write
(
'
<p>Error: please run <code>make</code> to install dependencies and try again.</p>
'
)
}
</script>
<script
src=
"../bower_components/es6-promise/promise.js"
></script>
<script
src=
"../fetch.js"
></script>
<script
src=
"test.js"
></script>
<script
src=
"/bower_components/es6-promise/promise.js"
></script>
<script
src=
"/test/test.js"
></script>
<script
src=
"/fetch.js"
></script>
<script>
if
(
self
.
mochaPhantomJS
)
{
...
...
test/test.js
浏览文件 @
4e00ced5
var
support
=
{
blob
:
'
FileReader
'
in
self
&&
'
Blob
'
in
self
&&
(
function
()
{
try
{
new
Blob
()
return
true
}
catch
(
e
)
{
return
false
}
})(),
formData
:
'
FormData
'
in
self
,
arrayBuffer
:
'
ArrayBuffer
'
in
self
,
patch
:
!
/PhantomJS/
.
test
(
navigator
.
userAgent
),
permanentRedirect
:
!
/PhantomJS|Trident/
.
test
(
navigator
.
userAgent
)
}
function
readBlobAsText
(
blob
)
{
if
(
'
FileReader
'
in
self
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
...
...
@@ -37,6 +52,40 @@ function readBlobAsBytes(blob) {
}
}
var
native
=
{}
var
keepGlobals
=
[
'
fetch
'
,
'
Headers
'
,
'
Request
'
,
'
Response
'
]
var
exercise
=
[
'
polyfill
'
]
// If native fetch implementation exists, save it and allow it to be replaced
// by the polyfill. Native implementation will be exercised additionally.
if
(
self
.
fetch
)
{
keepGlobals
.
forEach
(
function
(
name
)
{
native
[
name
]
=
self
[
name
]
})
self
.
fetch
=
undefined
exercise
.
push
(
'
native
'
)
}
var
slice
=
Array
.
prototype
.
slice
function
featureDependent
(
testOrSuite
,
condition
)
{
(
condition
?
testOrSuite
:
testOrSuite
.
skip
).
apply
(
this
,
slice
.
call
(
arguments
,
2
))
}
exercise
.
forEach
(
function
(
exerciseMode
)
{
suite
(
exerciseMode
,
function
()
{
if
(
exerciseMode
===
'
native
'
)
{
suiteSetup
(
function
()
{
keepGlobals
.
forEach
(
function
(
name
)
{
self
[
name
]
=
native
[
name
]
})
})
}
var
nativeChrome
=
/Chrome
\/
/
.
test
(
navigator
.
userAgent
)
&&
exerciseMode
===
'
native
'
var
nativeFirefox
=
/Firefox
\/
/
.
test
(
navigator
.
userAgent
)
&&
exerciseMode
===
'
native
'
var
polyfillFirefox
=
/Firefox
\/
/
.
test
(
navigator
.
userAgent
)
&&
exerciseMode
===
'
polyfill
'
test
(
'
resolves promise on 500 error
'
,
function
()
{
return
fetch
(
'
/boom
'
).
then
(
function
(
response
)
{
assert
.
equal
(
response
.
status
,
500
)
...
...
@@ -148,7 +197,7 @@ suite('Headers', function() {
headers
.
set
({
field
:
'
value
'
},
'
application/json
'
);
},
TypeError
)
})
test
(
'
is iterable with forEach
'
,
function
()
{
featureDependent
(
test
,
!
nativeFirefox
,
'
is iterable with forEach
'
,
function
()
{
var
headers
=
new
Headers
()
headers
.
append
(
'
Accept
'
,
'
application/json
'
)
headers
.
append
(
'
Accept
'
,
'
text/plain
'
)
...
...
@@ -164,7 +213,7 @@ suite('Headers', function() {
assert
.
deepEqual
({
key
:
'
accept
'
,
value
:
'
text/plain
'
,
object
:
headers
},
results
[
1
])
assert
.
deepEqual
({
key
:
'
content-type
'
,
value
:
'
text/html
'
,
object
:
headers
},
results
[
2
])
})
test
(
'
forEach accepts second thisArg argument
'
,
function
()
{
featureDependent
(
test
,
!
nativeFirefox
,
'
forEach accepts second thisArg argument
'
,
function
()
{
var
headers
=
new
Headers
({
'
Accept
'
:
'
application/json
'
})
var
thisArg
=
42
headers
.
forEach
(
function
()
{
...
...
@@ -205,7 +254,7 @@ suite('Request', function() {
})
})
;(
ArrayBuffer
in
self
?
test
:
test
.
skip
)(
'
sends ArrayBuffer body
'
,
function
()
{
featureDependent
(
test
,
support
.
arrayBuffer
,
'
sends ArrayBuffer body
'
,
function
()
{
var
text
=
'
name=Hubot
'
var
buf
=
new
ArrayBuffer
(
text
.
length
)
...
...
@@ -294,7 +343,7 @@ suite('Request', function() {
})
})
;(
/Chrome
\/
/
.
test
(
navigator
.
userAgent
)
&&
!
fetch
.
polyfill
?
test
.
skip
:
test
)(
'
construct with used Request body
'
,
function
()
{
featureDependent
(
test
,
!
nativeChrome
,
'
construct with used Request body
'
,
function
()
{
var
request1
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
body
:
'
I work out
'
...
...
@@ -324,7 +373,7 @@ suite('Request', function() {
})
})
;(
/Chrome
\/
/
.
test
(
navigator
.
userAgent
)
&&
!
fetch
.
polyfill
?
test
.
skip
:
test
)(
'
clone with used Request body
'
,
function
()
{
featureDependent
(
test
,
!
nativeChrome
,
'
clone with used Request body
'
,
function
()
{
var
req
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
body
:
'
I work out
'
...
...
@@ -339,7 +388,7 @@ suite('Request', function() {
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite
(
'
BodyInit extract
'
,
function
()
{
;(
Request
.
prototype
.
blob
?
suite
:
suite
.
skip
)(
'
type Blob
'
,
function
()
{
featureDependent
(
suite
,
support
.
blob
,
'
type Blob
'
,
function
()
{
test
(
'
consume as blob
'
,
function
()
{
var
request
=
new
Request
(
null
,
{
method
:
'
POST
'
,
body
:
new
Blob
([
'
hello
'
])})
return
request
.
blob
().
then
(
readBlobAsText
).
then
(
function
(
text
)
{
...
...
@@ -363,7 +412,7 @@ suite('Request', function() {
})
})
;(
Request
.
prototype
.
blob
?
test
:
test
.
skip
)(
'
consume as blob
'
,
function
()
{
featureDependent
(
test
,
support
.
blob
,
'
consume as blob
'
,
function
()
{
var
request
=
new
Request
(
null
,
{
method
:
'
POST
'
,
body
:
'
hello
'
})
return
request
.
blob
().
then
(
readBlobAsText
).
then
(
function
(
text
)
{
assert
.
equal
(
text
,
'
hello
'
)
...
...
@@ -377,7 +426,7 @@ suite('Request', function() {
suite
(
'
Response
'
,
function
()
{
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite
(
'
BodyInit extract
'
,
function
()
{
;(
Response
.
prototype
.
blob
?
suite
:
suite
.
skip
)(
'
type Blob
'
,
function
()
{
featureDependent
(
suite
,
support
.
blob
,
'
type Blob
'
,
function
()
{
test
(
'
consume as blob
'
,
function
()
{
var
response
=
new
Response
(
new
Blob
([
'
hello
'
]))
return
response
.
blob
().
then
(
readBlobAsText
).
then
(
function
(
text
)
{
...
...
@@ -401,7 +450,7 @@ suite('Response', function() {
})
})
;(
Response
.
prototype
.
blob
?
test
:
test
.
skip
)(
'
consume as blob
'
,
function
()
{
featureDependent
(
test
,
support
.
blob
,
'
consume as blob
'
,
function
()
{
var
response
=
new
Response
(
'
hello
'
)
return
response
.
blob
().
then
(
readBlobAsText
).
then
(
function
(
text
)
{
assert
.
equal
(
text
,
'
hello
'
)
...
...
@@ -450,7 +499,7 @@ suite('Response', function() {
})
})
;(
Response
.
prototype
.
arrayBuffer
?
test
:
test
.
skip
)(
'
clone blob response
'
,
function
()
{
featureDependent
(
test
,
support
.
blob
,
'
clone blob response
'
,
function
()
{
return
fetch
(
'
/binary
'
).
then
(
function
(
response
)
{
return
Promise
.
all
([
response
.
clone
().
arrayBuffer
(),
response
.
arrayBuffer
()]).
then
(
function
(
bufs
){
bufs
.
forEach
(
function
(
buf
){
...
...
@@ -483,7 +532,7 @@ suite('Response', function() {
// https://fetch.spec.whatwg.org/#body-mixin
suite
(
'
Body mixin
'
,
function
()
{
;(
Response
.
prototype
.
arrayBuffer
?
suite
:
suite
.
skip
)(
'
arrayBuffer
'
,
function
()
{
featureDependent
(
suite
,
support
.
blob
,
'
arrayBuffer
'
,
function
()
{
test
(
'
resolves arrayBuffer promise
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
return
response
.
arrayBuffer
()
...
...
@@ -530,7 +579,6 @@ suite('Body mixin', function() {
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
)
...
...
@@ -541,7 +589,7 @@ suite('Body mixin', function() {
})
})
;(
Response
.
prototype
.
blob
?
suite
:
suite
.
skip
)(
'
blob
'
,
function
()
{
featureDependent
(
suite
,
support
.
blob
,
'
blob
'
,
function
()
{
test
(
'
resolves blob promise
'
,
function
()
{
return
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
return
response
.
blob
()
...
...
@@ -591,7 +639,7 @@ suite('Body mixin', function() {
})
})
;(
Response
.
prototype
.
formData
?
suite
:
suite
.
skip
)(
'
formData
'
,
function
()
{
featureDependent
(
suite
,
support
.
formData
,
'
formData
'
,
function
()
{
test
(
'
post sets content-type header
'
,
function
()
{
return
fetch
(
'
/request
'
,
{
method
:
'
post
'
,
...
...
@@ -604,7 +652,7 @@ suite('Body mixin', function() {
})
})
test
(
'
rejects formData promise after body is consumed
'
,
function
()
{
featureDependent
(
test
,
!
nativeChrome
,
'
rejects formData promise after body is consumed
'
,
function
()
{
return
fetch
(
'
/json
'
).
then
(
function
(
response
)
{
assert
(
response
.
formData
,
'
Body does not implement formData
'
)
response
.
formData
()
...
...
@@ -618,7 +666,7 @@ suite('Body mixin', function() {
})
})
test
(
'
parses form encoded response
'
,
function
()
{
featureDependent
(
test
,
!
nativeChrome
,
'
parses form encoded response
'
,
function
()
{
return
fetch
(
'
/form
'
).
then
(
function
(
response
)
{
return
response
.
formData
()
}).
then
(
function
(
form
)
{
...
...
@@ -649,7 +697,7 @@ suite('Body mixin', function() {
})
})
test
(
'
handles json parse error
'
,
function
()
{
featureDependent
(
test
,
!
polyfillFirefox
,
'
handles json parse error
'
,
function
()
{
return
fetch
(
'
/json-error
'
).
then
(
function
(
response
)
{
return
response
.
json
()
}).
catch
(
function
(
error
)
{
...
...
@@ -746,9 +794,7 @@ suite('Methods', function() {
})
})
var
patchSupported
=
!
/PhantomJS/
.
test
(
navigator
.
userAgent
)
;(
patchSupported
?
test
:
test
.
skip
)(
'
supports HTTP PATCH
'
,
function
()
{
featureDependent
(
test
,
support
.
patch
,
'
supports HTTP PATCH
'
,
function
()
{
return
fetch
(
'
/request
'
,
{
method
:
'
PATCH
'
,
body
:
'
name=Hubot
'
...
...
@@ -818,9 +864,7 @@ suite('Atomic HTTP redirect handling', function() {
})
})
var
permanentRedirectSupported
=
!
/PhantomJS|Trident/
.
test
(
navigator
.
userAgent
)
;(
permanentRedirectSupported
?
test
:
test
.
skip
)(
'
handles 308 redirect response
'
,
function
()
{
featureDependent
(
test
,
support
.
permanentRedirect
,
'
handles 308 redirect response
'
,
function
()
{
return
fetch
(
'
/redirect/308
'
).
then
(
function
(
response
)
{
assert
.
equal
(
response
.
status
,
200
)
assert
.
equal
(
response
.
ok
,
true
)
...
...
@@ -834,13 +878,11 @@ suite('Atomic HTTP redirect handling', function() {
// https://fetch.spec.whatwg.org/#concept-request-credentials-mode
suite
(
'
credentials mode
'
,
function
()
{
var
omitSupported
=
!
self
.
fetch
.
polyfill
setup
(
function
()
{
return
fetch
(
'
/cookie?name=foo&value=reset
'
,
{
credentials
:
'
same-origin
'
});
})
;(
omitSupported
?
suite
:
suite
.
skip
)(
'
omit
'
,
function
()
{
featureDependent
(
suite
,
exerciseMode
===
'
native
'
,
'
omit
'
,
function
()
{
test
(
'
request credentials defaults to omit
'
,
function
()
{
var
request
=
new
Request
(
''
)
assert
.
equal
(
request
.
credentials
,
'
omit
'
)
...
...
@@ -916,3 +958,6 @@ suite('credentials mode', function() {
})
})
})
})
})
test/worker.js
浏览文件 @
4e00ced5
importScripts
(
'
..
/node_modules/chai/chai.js
'
)
importScripts
(
'
..
/node_modules/mocha/mocha.js
'
)
importScripts
(
'
/node_modules/chai/chai.js
'
)
importScripts
(
'
/node_modules/mocha/mocha.js
'
)
mocha
.
setup
(
'
tdd
'
)
self
.
assert
=
chai
.
assert
importScripts
(
'
../bower_components/es6-promise/promise.js
'
)
importScripts
(
'
../fetch.js
'
)
importScripts
(
'
test.js
'
)
importScripts
(
'
/bower_components/es6-promise/promise.js
'
)
importScripts
(
'
/test/test.js
'
)
importScripts
(
'
/fetch.js
'
)
function
title
(
test
)
{
return
test
.
fullTitle
().
replace
(
/#/g
,
''
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录