Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
261411d5
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,发现更多精彩内容 >>
提交
261411d5
编写于
2月 02, 2015
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #85 from mattking17/master
Follow spec on Headers to throw TypeError, add tests for Headers
上级
c3fc0d43
ddb29e58
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
92 addition
and
8 deletion
+92
-8
fetch.js
fetch.js
+25
-6
test/test.js
test/test.js
+67
-2
未找到文件。
fetch.js
浏览文件 @
261411d5
...
...
@@ -5,6 +5,23 @@
return
}
function
normalizeName
(
name
)
{
if
(
typeof
name
!==
'
string
'
)
{
name
=
name
.
toString
();
}
if
(
/
[^
a-z0-9
\-
#$%&'*+.
\^
_`|~
]
/i
.
test
(
name
))
{
throw
new
TypeError
(
'
Invalid character in header field name
'
)
}
return
name
.
toLowerCase
()
}
function
normalizeValue
(
value
)
{
if
(
typeof
value
!==
'
string
'
)
{
value
=
value
.
toString
();
}
return
value
}
function
Headers
(
headers
)
{
this
.
map
=
{}
...
...
@@ -24,7 +41,8 @@
}
Headers
.
prototype
.
append
=
function
(
name
,
value
)
{
name
=
name
.
toLowerCase
()
name
=
normalizeName
(
name
)
value
=
normalizeValue
(
value
)
var
list
=
this
.
map
[
name
]
if
(
!
list
)
{
list
=
[]
...
...
@@ -34,24 +52,24 @@
}
Headers
.
prototype
[
'
delete
'
]
=
function
(
name
)
{
delete
this
.
map
[
n
ame
.
toLowerCase
(
)]
delete
this
.
map
[
n
ormalizeName
(
name
)]
}
Headers
.
prototype
.
get
=
function
(
name
)
{
var
values
=
this
.
map
[
n
ame
.
toLowerCase
(
)]
var
values
=
this
.
map
[
n
ormalizeName
(
name
)]
return
values
?
values
[
0
]
:
null
}
Headers
.
prototype
.
getAll
=
function
(
name
)
{
return
this
.
map
[
n
ame
.
toLowerCase
(
)]
||
[]
return
this
.
map
[
n
ormalizeName
(
name
)]
||
[]
}
Headers
.
prototype
.
has
=
function
(
name
)
{
return
this
.
map
.
hasOwnProperty
(
n
ame
.
toLowerCase
(
))
return
this
.
map
.
hasOwnProperty
(
n
ormalizeName
(
name
))
}
Headers
.
prototype
.
set
=
function
(
name
,
value
)
{
this
.
map
[
n
ame
.
toLowerCase
()]
=
[
value
]
this
.
map
[
n
ormalizeName
(
name
)]
=
[
normalizeValue
(
value
)
]
}
// Instead of iterable for now.
...
...
@@ -281,6 +299,7 @@
}
xhr
.
open
(
self
.
method
,
self
.
url
,
true
)
if
(
'
responseType
'
in
xhr
&&
support
.
blob
)
{
xhr
.
responseType
=
'
blob
'
}
...
...
test/test.js
浏览文件 @
261411d5
...
...
@@ -44,13 +44,78 @@ test('rejects promise for network error', function() {
// https://fetch.spec.whatwg.org/#headers-class
suite
(
'
Headers
'
,
function
()
{
test
(
'
headers are case insensitve
'
,
function
()
{
test
(
'
headers are case insensit
i
ve
'
,
function
()
{
var
headers
=
new
Headers
({
'
Accept
'
:
'
application/json
'
})
assert
.
equal
(
headers
.
get
(
'
ACCEPT
'
),
'
application/json
'
)
assert
.
equal
(
headers
.
get
(
'
Accept
'
),
'
application/json
'
)
assert
.
equal
(
headers
.
get
(
'
accept
'
),
'
application/json
'
)
})
})
test
(
'
appends to existing
'
,
function
()
{
var
headers
=
new
Headers
({
'
Accept
'
:
'
application/json
'
})
assert
.
isFalse
(
headers
.
has
(
'
Content-Type
'
))
headers
.
append
(
'
Content-Type
'
,
'
application/json
'
)
assert
.
isTrue
(
headers
.
has
(
'
Content-Type
'
))
assert
.
equal
(
headers
.
get
(
'
Content-Type
'
),
'
application/json
'
)
})
test
(
'
appends values to existing header name
'
,
function
()
{
var
headers
=
new
Headers
({
'
Accept
'
:
'
application/json
'
})
headers
.
append
(
'
Accept
'
,
'
text/plain
'
)
assert
.
equal
(
headers
.
getAll
(
'
Accept
'
).
length
,
2
)
assert
.
equal
(
headers
.
getAll
(
'
Accept
'
)[
0
],
'
application/json
'
)
assert
.
equal
(
headers
.
getAll
(
'
Accept
'
)[
1
],
'
text/plain
'
)
})
test
(
'
sets header name and value
'
,
function
()
{
var
headers
=
new
Headers
()
headers
.
set
(
'
Content-Type
'
,
'
application/json
'
)
assert
.
equal
(
headers
.
get
(
'
Content-Type
'
),
'
application/json
'
)
})
test
(
'
returns null on no header found
'
,
function
()
{
var
headers
=
new
Headers
()
assert
.
isNull
(
headers
.
get
(
'
Content-Type
'
))
})
test
(
'
has headers that are set
'
,
function
()
{
var
headers
=
new
Headers
()
headers
.
set
(
'
Content-Type
'
,
'
application/json
'
)
assert
.
isTrue
(
headers
.
has
(
'
Content-Type
'
))
})
test
(
'
deletes headers
'
,
function
()
{
var
headers
=
new
Headers
()
headers
.
set
(
'
Content-Type
'
,
'
application/json
'
)
assert
.
isTrue
(
headers
.
has
(
'
Content-Type
'
))
headers
.
delete
(
'
Content-Type
'
)
assert
.
isFalse
(
headers
.
has
(
'
Content-Type
'
))
assert
.
isNull
(
headers
.
get
(
'
Content-Type
'
))
})
test
(
'
returns list on getAll when header found
'
,
function
()
{
var
headers
=
new
Headers
({
'
Content-Type
'
:
'
application/json
'
})
assert
.
isArray
(
headers
.
getAll
(
'
Content-Type
'
))
assert
.
equal
(
headers
.
getAll
(
'
Content-Type
'
).
length
,
1
)
assert
.
equal
(
headers
.
getAll
(
'
Content-Type
'
)[
0
],
'
application/json
'
)
})
test
(
'
returns empty list on getAll when no header found
'
,
function
()
{
var
headers
=
new
Headers
()
assert
.
isArray
(
headers
.
getAll
(
'
Content-Type
'
))
assert
.
equal
(
headers
.
getAll
(
'
Content-Type
'
).
length
,
0
)
})
test
(
'
converts field name to string on set and get
'
,
function
()
{
var
headers
=
new
Headers
()
headers
.
set
(
1
,
'
application/json
'
)
assert
.
equal
(
headers
.
get
(
1
),
'
application/json
'
)
})
test
(
'
converts field value to string on set and get
'
,
function
()
{
var
headers
=
new
Headers
()
headers
.
set
(
'
Content-Type
'
,
1
)
assert
.
equal
(
headers
.
get
(
'
Content-Type
'
),
'
1
'
)
})
test
(
'
throws TypeError on invalid character in field name
'
,
function
()
{
assert
.
throws
(
function
()
{
new
Headers
({
'
<Accept>
'
:
[
'
application/json
'
]})
},
TypeError
)
assert
.
throws
(
function
()
{
new
Headers
({
'
Accept:
'
:
[
'
application/json
'
]})
},
TypeError
)
assert
.
throws
(
function
()
{
var
headers
=
new
Headers
();
headers
.
set
({
field
:
'
value
'
},
'
application/json
'
);
},
TypeError
)
})
})
// https://fetch.spec.whatwg.org/#request-class
suite
(
'
Request
'
,
function
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录