Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
镜像
Coder
code-server
提交
4925e970
C
code-server
项目概览
镜像
/
Coder
/
code-server
2022-09-21 03:15:05同步失败
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
code-server
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
4925e970
编写于
5月 05, 2021
作者:
A
Asher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add static route tests
上级
1789cd1b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
136 addition
and
0 deletion
+136
-0
test/unit/routes/static.test.ts
test/unit/routes/static.test.ts
+136
-0
未找到文件。
test/unit/routes/static.test.ts
0 → 100644
浏览文件 @
4925e970
import
{
promises
as
fs
}
from
"
fs
"
import
*
as
path
from
"
path
"
import
{
tmpdir
}
from
"
../../utils/helpers
"
import
*
as
httpserver
from
"
../../utils/httpserver
"
import
*
as
integration
from
"
../../utils/integration
"
describe
(
"
/static
"
,
()
=>
{
let
_codeServer
:
httpserver
.
HttpServer
|
undefined
function
codeServer
():
httpserver
.
HttpServer
{
if
(
!
_codeServer
)
{
throw
new
Error
(
"
tried to use code-server before setting it up
"
)
}
return
_codeServer
}
let
testFile
:
string
|
undefined
let
testFileContent
:
string
|
undefined
let
nonExistentTestFile
:
string
|
undefined
// The static endpoint expects a commit and then the full path of the file.
// The commit is just for cache busting so we can use anything we want. `-`
// and `development` are specially recognized in that they will cause the
// static endpoint to avoid sending cache headers.
const
commit
=
"
-
"
beforeAll
(
async
()
=>
{
const
testDir
=
await
tmpdir
(
"
static
"
)
testFile
=
path
.
join
(
testDir
,
"
test
"
)
testFileContent
=
"
static file contents
"
nonExistentTestFile
=
path
.
join
(
testDir
,
"
i-am-not-here
"
)
await
fs
.
writeFile
(
testFile
,
testFileContent
)
})
afterEach
(
async
()
=>
{
if
(
_codeServer
)
{
await
_codeServer
.
close
()
_codeServer
=
undefined
}
})
function
commonTests
()
{
it
(
"
should return a 404 when a commit and file are not provided
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
"
/static
"
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Not Found
"
})
})
it
(
"
should return a 404 when a file is not provided
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
`
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Not Found
"
})
})
}
describe
(
"
disabled authentication
"
,
()
=>
{
beforeEach
(
async
()
=>
{
_codeServer
=
await
integration
.
setup
([
"
--auth=none
"
],
""
)
})
commonTests
()
it
(
"
should return a 404 for a nonexistent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
/
${
nonExistentTestFile
}
`
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
.
error
).
toMatch
(
"
ENOENT
"
)
})
it
(
"
should return a 200 and file contents for an existent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}${
testFile
}
`
)
expect
(
resp
.
status
).
toBe
(
200
)
const
content
=
await
resp
.
text
()
expect
(
content
).
toStrictEqual
(
testFileContent
)
})
})
describe
(
"
enabled authentication
"
,
()
=>
{
// Store whatever might be in here so we can restore it afterward.
// TODO: We should probably pass this as an argument somehow instead of
// manipulating the environment.
const
previousEnvPassword
=
process
.
env
.
PASSWORD
beforeEach
(
async
()
=>
{
process
.
env
.
PASSWORD
=
"
test
"
_codeServer
=
await
integration
.
setup
([
"
--auth=password
"
],
""
)
})
afterEach
(()
=>
{
process
.
env
.
PASSWORD
=
previousEnvPassword
})
commonTests
()
describe
(
"
inside code-server root
"
,
()
=>
{
it
(
"
should return a 404 for a nonexistent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
/
${
__filename
}
-does-not-exist`
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
.
error
).
toMatch
(
"
ENOENT
"
)
})
it
(
"
should return a 200 and file contents for an existent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}${
__filename
}
`
)
expect
(
resp
.
status
).
toBe
(
200
)
const
content
=
await
resp
.
text
()
expect
(
content
).
toStrictEqual
(
await
fs
.
readFile
(
__filename
,
"
utf8
"
))
})
})
describe
(
"
outside code-server root
"
,
()
=>
{
it
(
"
should return a 401 for a nonexistent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
/
${
nonExistentTestFile
}
`
)
expect
(
resp
.
status
).
toBe
(
401
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Unauthorized
"
})
})
it
(
"
should return a 401 for an existent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}${
testFile
}
`
)
expect
(
resp
.
status
).
toBe
(
401
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Unauthorized
"
})
})
})
})
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录