Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
3316bdaf
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 搜索 >>
提交
3316bdaf
编写于
10月 15, 2014
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add test server
上级
e61377fe
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
88 addition
and
127 deletion
+88
-127
bin/server
bin/server
+68
-0
test/mock-xhr.js
test/mock-xhr.js
+0
-75
test/test.html
test/test.html
+0
-1
test/test.js
test/test.js
+20
-51
未找到文件。
bin/server
0 → 100755
浏览文件 @
3316bdaf
#!/usr/bin/env node
var
fs
=
require
(
'
fs
'
)
var
http
=
require
(
'
http
'
);
var
url
=
require
(
'
url
'
);
var
routes
=
{
'
/request
'
:
function
(
res
,
req
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/json
'
});
var
data
=
''
req
.
on
(
'
data
'
,
function
(
c
)
{
data
+=
c
})
req
.
on
(
'
end
'
,
function
()
{
res
.
end
(
JSON
.
stringify
({
method
:
req
.
method
,
url
:
req
.
url
,
headers
:
req
.
headers
,
data
:
data
}));
})
},
'
/hello
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
end
(
'
hi
'
);
},
'
/boom
'
:
function
(
res
)
{
res
.
writeHead
(
500
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
end
(
'
boom
'
);
},
'
/form
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
end
(
'
number=1&space=one+two&empty=&encoded=a%2Bb&
'
);
},
'
/json
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/json
'
});
res
.
end
(
JSON
.
stringify
({
name
:
'
Hubot
'
,
login
:
'
hubot
'
}));
},
'
/json-error
'
:
function
(
res
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/json
'
});
res
.
end
(
'
not json {
'
);
},
'
/headers
'
:
function
(
res
)
{
var
headers
=
[
'
Date: Mon, 13 Oct 2014 21:02:27 GMT
'
,
'
Content-Type: text/html; charset=utf-8
'
].
join
(
'
\r\n
'
)
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
end
(
headers
+
'
\r\n
'
);
}
};
http
.
createServer
(
function
(
req
,
res
)
{
var
pathname
=
url
.
parse
(
req
.
url
).
pathname
;
var
route
=
routes
[
pathname
];
if
(
route
)
{
route
(
res
,
req
);
}
else
{
fs
.
readFile
(
__dirname
+
'
/..
'
+
pathname
,
function
(
err
,
data
)
{
if
(
err
)
{
res
.
writeHead
(
404
,
{
'
Content-Type
'
:
'
text/plain
'
});
res
.
end
(
'
Not Found
'
);
}
else
{
res
.
writeHead
(
200
);
res
.
end
(
data
);
}
});
}
}).
listen
(
3000
);
test/mock-xhr.js
已删除
100644 → 0
浏览文件 @
e61377fe
function
MockXHR
()
{
MockXHR
.
requests
.
push
(
this
)
this
.
method
=
null
this
.
url
=
null
this
.
data
=
null
this
.
headers
=
{}
this
.
readyState
=
0
this
.
status
=
0
this
.
responseText
=
null
this
.
responseHeaders
=
''
}
MockXHR
.
requests
=
[]
MockXHR
.
responses
=
{}
MockXHR
.
last
=
function
()
{
return
MockXHR
.
requests
[
MockXHR
.
requests
.
length
-
1
]
}
MockXHR
.
prototype
.
open
=
function
(
method
,
url
)
{
this
.
method
=
method
this
.
url
=
url
}
MockXHR
.
prototype
.
getAllResponseHeaders
=
function
()
{
return
this
.
responseHeaders
}
MockXHR
.
prototype
.
setRequestHeader
=
function
(
name
,
value
)
{
this
.
headers
[
name
]
=
value
}
var
origin
=
(
function
()
{
var
link
=
document
.
createElement
(
'
a
'
)
link
.
href
=
'
/
'
return
link
.
href
})()
MockXHR
.
prototype
.
send
=
function
(
data
)
{
this
.
data
=
data
var
xhr
=
this
setTimeout
(
function
()
{
var
path
=
xhr
.
url
.
replace
(
origin
,
'
/
'
)
var
handle
=
MockXHR
.
responses
[
path
]
if
(
handle
)
{
handle
(
xhr
)
}
else
{
throw
'
missing mocked response:
'
+
path
}
},
100
);
}
MockXHR
.
prototype
.
respond
=
function
(
status
,
body
,
headers
)
{
this
.
readyState
=
4
this
.
status
=
status
this
.
responseText
=
body
this
.
responseHeaders
=
headers
||
''
var
event
=
{}
this
.
onload
(
event
)
}
MockXHR
.
prototype
.
abort
=
function
()
{
// Do nothing.
}
MockXHR
.
prototype
.
slow
=
function
()
{
var
event
=
{}
this
.
ontimeout
(
event
)
}
MockXHR
.
prototype
.
error
=
function
()
{
var
event
=
{}
this
.
onerror
(
event
)
}
test/test.html
浏览文件 @
3316bdaf
...
...
@@ -11,7 +11,6 @@
<script
src=
"../bower_components/es6-promise/promise.js"
></script>
<script
src=
"../fetch.js"
></script>
<script
src=
"../bower_components/qunit/qunit/qunit.js"
></script>
<script
src=
"./mock-xhr.js"
></script>
<script
src=
"./test.js"
></script>
</body>
</html>
test/test.js
浏览文件 @
3316bdaf
MockXHR
.
responses
=
{
'
/hello
'
:
function
(
xhr
)
{
xhr
.
respond
(
200
,
'
hi
'
)
},
'
/boom
'
:
function
(
xhr
)
{
xhr
.
respond
(
500
,
'
boom
'
)
},
'
/error
'
:
function
(
xhr
)
{
xhr
.
error
()
},
'
/form
'
:
function
(
xhr
)
{
xhr
.
respond
(
200
,
'
number=1&space=one+two&empty=&encoded=a%2Bb&
'
)
},
'
/json
'
:
function
(
xhr
)
{
xhr
.
respond
(
200
,
JSON
.
stringify
({
name
:
'
Hubot
'
,
login
:
'
hubot
'
}))
},
'
/json-error
'
:
function
(
xhr
)
{
xhr
.
respond
(
200
,
'
not json {
'
)
},
'
/headers
'
:
function
(
xhr
)
{
var
headers
=
[
'
Date: Mon, 13 Oct 2014 21:02:27 GMT
'
,
'
Content-Type: text/html; charset=utf-8
'
].
join
(
'
\r\n
'
)
xhr
.
respond
(
200
,
'
hi
'
,
headers
+
'
\r\n
'
)
}
}
window
.
XMLHttpRequest
=
MockXHR
asyncTest
(
'
populates response body
'
,
3
,
function
()
{
asyncTest
(
'
populates response body
'
,
2
,
function
()
{
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
equal
(
MockXHR
.
last
().
method
,
'
GET
'
)
equal
(
response
.
status
,
200
)
equal
(
response
.
body
,
'
hi
'
)
start
()
...
...
@@ -38,15 +7,15 @@ asyncTest('populates response body', 3, function() {
})
asyncTest
(
'
sends request headers
'
,
2
,
function
()
{
fetch
(
'
/
hello
'
,
{
fetch
(
'
/
request
'
,
{
headers
:
{
'
Accept
'
:
'
application/json
'
,
'
X-Test
'
:
'
42
'
}
}).
then
(
function
()
{
var
request
=
MockXHR
.
last
()
equal
(
request
.
headers
[
'
A
ccept
'
],
'
application/json
'
)
equal
(
request
.
headers
[
'
X-T
est
'
],
'
42
'
)
}).
then
(
function
(
response
)
{
var
headers
=
JSON
.
parse
(
response
.
body
).
headers
equal
(
headers
[
'
a
ccept
'
],
'
application/json
'
)
equal
(
headers
[
'
x-t
est
'
],
'
42
'
)
start
()
})
})
...
...
@@ -67,12 +36,12 @@ asyncTest('resolves promise on 500 error', 2, function() {
})
})
asyncTest
(
'
rejects promise for network error
'
,
1
,
function
()
{
fetch
(
'
/error
'
).
catch
(
function
()
{
ok
(
true
)
start
()
})
})
//
asyncTest('rejects promise for network error', 1, function() {
//
fetch('/error').catch(function() {
//
ok(true)
//
start()
//
})
//
})
asyncTest
(
'
resolves text promise
'
,
1
,
function
()
{
fetch
(
'
/hello
'
).
then
(
function
(
response
)
{
...
...
@@ -123,7 +92,7 @@ asyncTest('resolves blob promise', 2, function() {
})
asyncTest
(
'
post sends encoded body
'
,
2
,
function
()
{
fetch
(
'
/
hello
'
,
{
fetch
(
'
/
request
'
,
{
method
:
'
post
'
,
body
:
{
name
:
'
Hubot
'
,
...
...
@@ -131,21 +100,21 @@ asyncTest('post sends encoded body', 2, function() {
undef
:
undefined
,
nil
:
null
}
}).
then
(
function
()
{
var
request
=
MockXHR
.
last
()
equal
(
request
.
method
,
'
post
'
)
}).
then
(
function
(
response
)
{
var
request
=
JSON
.
parse
(
response
.
body
);
equal
(
request
.
method
,
'
POST
'
)
equal
(
request
.
data
,
'
name=Hubot&title=Hubot+Robawt&nil=
'
)
start
()
})
})
asyncTest
(
'
post sets content-type header
'
,
1
,
function
()
{
fetch
(
'
/
hello
'
,
{
fetch
(
'
/
request
'
,
{
method
:
'
post
'
,
body
:
{}
}).
then
(
function
()
{
var
request
=
MockXHR
.
last
()
equal
(
request
.
headers
[
'
Content-T
ype
'
],
'
application/x-www-form-urlencoded; charset=UTF-8
'
)
}).
then
(
function
(
response
)
{
var
request
=
JSON
.
parse
(
response
.
body
);
equal
(
request
.
headers
[
'
content-t
ype
'
],
'
application/x-www-form-urlencoded; charset=UTF-8
'
)
start
()
})
})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录