Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
d16df665
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,发现更多精彩内容 >>
提交
d16df665
编写于
7月 31, 2015
作者:
M
Mislav Marohnić
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #179 from github/request-to-request
Allow passing a Request instance to Request constructor
上级
aa21bda8
52561a07
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
109 addition
and
10 deletion
+109
-10
fetch.js
fetch.js
+28
-9
test/test.js
test/test.js
+81
-1
未找到文件。
fetch.js
浏览文件 @
d16df665
...
...
@@ -200,20 +200,40 @@
return
(
methods
.
indexOf
(
upcased
)
>
-
1
)
?
upcased
:
method
}
function
Request
(
url
,
options
)
{
function
Request
(
input
,
options
)
{
options
=
options
||
{}
this
.
url
=
url
var
body
=
options
.
body
if
(
Request
.
prototype
.
isPrototypeOf
(
input
))
{
if
(
input
.
bodyUsed
)
{
throw
new
TypeError
(
'
Already read
'
)
}
this
.
url
=
input
.
url
this
.
credentials
=
input
.
credentials
if
(
!
options
.
headers
)
{
this
.
headers
=
new
Headers
(
input
.
headers
)
}
this
.
method
=
input
.
method
this
.
mode
=
input
.
mode
if
(
!
body
)
{
body
=
input
.
_bodyInit
input
.
bodyUsed
=
true
}
}
else
{
this
.
url
=
input
}
this
.
credentials
=
options
.
credentials
||
'
omit
'
this
.
headers
=
new
Headers
(
options
.
headers
)
this
.
method
=
normalizeMethod
(
options
.
method
||
'
GET
'
)
this
.
mode
=
options
.
mode
||
null
this
.
credentials
=
options
.
credentials
||
this
.
credentials
||
'
omit
'
if
(
options
.
headers
||
!
this
.
headers
)
{
this
.
headers
=
new
Headers
(
options
.
headers
)
}
this
.
method
=
normalizeMethod
(
options
.
method
||
this
.
method
||
'
GET
'
)
this
.
mode
=
options
.
mode
||
this
.
mode
||
null
this
.
referrer
=
null
if
((
this
.
method
===
'
GET
'
||
this
.
method
===
'
HEAD
'
)
&&
options
.
body
)
{
if
((
this
.
method
===
'
GET
'
||
this
.
method
===
'
HEAD
'
)
&&
body
)
{
throw
new
TypeError
(
'
Body not allowed for GET or HEAD requests
'
)
}
this
.
_initBody
(
options
.
body
)
this
.
_initBody
(
body
)
}
function
decode
(
body
)
{
...
...
@@ -265,7 +285,6 @@
self
.
Response
=
Response
;
self
.
fetch
=
function
(
input
,
init
)
{
// TODO: Request constructor should accept input, init
var
request
if
(
Request
.
prototype
.
isPrototypeOf
(
input
)
&&
!
init
)
{
request
=
input
...
...
test/test.js
浏览文件 @
d16df665
...
...
@@ -202,6 +202,82 @@ suite('Request', function() {
assert
.
equal
(
request
.
url
,
'
https://fetch.spec.whatwg.org/
'
)
})
test
(
'
construct with Request
'
,
function
()
{
var
request1
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
body
:
'
I work out
'
,
headers
:
{
accept
:
'
application/json
'
,
'
Content-Type
'
:
'
text/plain
'
}
})
var
request2
=
new
Request
(
request1
)
return
request2
.
text
().
then
(
function
(
body2
)
{
assert
.
equal
(
body2
,
'
I work out
'
)
assert
.
equal
(
request2
.
method
,
'
POST
'
)
assert
.
equal
(
request2
.
url
,
'
https://fetch.spec.whatwg.org/
'
)
assert
.
equal
(
request2
.
headers
.
get
(
'
accept
'
),
'
application/json
'
)
assert
.
equal
(
request2
.
headers
.
get
(
'
content-type
'
),
'
text/plain
'
)
return
request1
.
text
().
then
(
function
()
{
assert
(
false
,
'
original request body should have been consumed
'
)
},
function
(
error
)
{
assert
(
error
instanceof
TypeError
,
'
expected TypeError for already read body
'
)
})
})
})
test
(
'
construct with Request and override headers
'
,
function
()
{
var
request1
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
body
:
'
I work out
'
,
headers
:
{
accept
:
'
application/json
'
,
'
Content-Type
'
:
'
text/plain
'
}
})
var
request2
=
new
Request
(
request1
,
{
headers
:
{
'
x-test
'
:
'
42
'
}
})
assert
.
equal
(
request2
.
headers
.
get
(
'
accept
'
),
undefined
)
assert
.
equal
(
request2
.
headers
.
get
(
'
content-type
'
),
undefined
)
assert
.
equal
(
request2
.
headers
.
get
(
'
x-test
'
),
'
42
'
)
})
test
(
'
construct with Request and override body
'
,
function
()
{
var
request1
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
body
:
'
I work out
'
,
headers
:
{
'
Content-Type
'
:
'
text/plain
'
}
})
var
request2
=
new
Request
(
request1
,
{
body
:
'
{"wiggles": 5}
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
}
})
return
request2
.
json
().
then
(
function
(
data
)
{
assert
.
equal
(
data
.
wiggles
,
5
)
assert
.
equal
(
request2
.
headers
.
get
(
'
content-type
'
),
'
application/json
'
)
})
})
;(
/Chrome
\/
/
.
test
(
navigator
.
userAgent
)
?
test
.
skip
:
test
)(
'
construct with used Request body
'
,
function
()
{
var
request1
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
body
:
'
I work out
'
})
return
request1
.
text
().
then
(
function
()
{
assert
.
throws
(
function
()
{
new
Request
(
request1
)
},
TypeError
)
})
})
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite
(
'
BodyInit extract
'
,
function
()
{
;(
Request
.
prototype
.
blob
?
suite
:
suite
.
skip
)(
'
type Blob
'
,
function
()
{
...
...
@@ -431,7 +507,11 @@ suite('Body mixin', function() {
response
.
formData
()
return
response
.
formData
()
}).
catch
(
function
(
error
)
{
assert
(
error
instanceof
TypeError
,
'
Promise rejected after body consumed
'
)
if
(
error
instanceof
chai
.
AssertionError
)
{
throw
error
}
else
{
assert
(
error
instanceof
TypeError
,
'
Promise rejected after body consumed
'
)
}
})
})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录