Skip to content

Commit 4925e97

Browse files
committed
Add static route tests
1 parent 1789cd1 commit 4925e97

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

test/unit/routes/static.test.ts

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { promises as fs } from "fs"
2+
import * as path from "path"
3+
import { tmpdir } from "../../utils/helpers"
4+
import * as httpserver from "../../utils/httpserver"
5+
import * as integration from "../../utils/integration"
6+
7+
describe("/static", () => {
8+
let _codeServer: httpserver.HttpServer | undefined
9+
function codeServer(): httpserver.HttpServer {
10+
if (!_codeServer) {
11+
throw new Error("tried to use code-server before setting it up")
12+
}
13+
return _codeServer
14+
}
15+
16+
let testFile: string | undefined
17+
let testFileContent: string | undefined
18+
let nonExistentTestFile: string | undefined
19+
20+
// The static endpoint expects a commit and then the full path of the file.
21+
// The commit is just for cache busting so we can use anything we want. `-`
22+
// and `development` are specially recognized in that they will cause the
23+
// static endpoint to avoid sending cache headers.
24+
const commit = "-"
25+
26+
beforeAll(async () => {
27+
const testDir = await tmpdir("static")
28+
testFile = path.join(testDir, "test")
29+
testFileContent = "static file contents"
30+
nonExistentTestFile = path.join(testDir, "i-am-not-here")
31+
await fs.writeFile(testFile, testFileContent)
32+
})
33+
34+
afterEach(async () => {
35+
if (_codeServer) {
36+
await _codeServer.close()
37+
_codeServer = undefined
38+
}
39+
})
40+
41+
function commonTests() {
42+
it("should return a 404 when a commit and file are not provided", async () => {
43+
const resp = await codeServer().fetch("/static")
44+
expect(resp.status).toBe(404)
45+
46+
const content = await resp.json()
47+
expect(content).toStrictEqual({ error: "Not Found" })
48+
})
49+
50+
it("should return a 404 when a file is not provided", async () => {
51+
const resp = await codeServer().fetch(`/static/${commit}`)
52+
expect(resp.status).toBe(404)
53+
54+
const content = await resp.json()
55+
expect(content).toStrictEqual({ error: "Not Found" })
56+
})
57+
}
58+
59+
describe("disabled authentication", () => {
60+
beforeEach(async () => {
61+
_codeServer = await integration.setup(["--auth=none"], "")
62+
})
63+
64+
commonTests()
65+
66+
it("should return a 404 for a nonexistent file", async () => {
67+
const resp = await codeServer().fetch(`/static/${commit}/${nonExistentTestFile}`)
68+
expect(resp.status).toBe(404)
69+
70+
const content = await resp.json()
71+
expect(content.error).toMatch("ENOENT")
72+
})
73+
74+
it("should return a 200 and file contents for an existent file", async () => {
75+
const resp = await codeServer().fetch(`/static/${commit}${testFile}`)
76+
expect(resp.status).toBe(200)
77+
78+
const content = await resp.text()
79+
expect(content).toStrictEqual(testFileContent)
80+
})
81+
})
82+
83+
describe("enabled authentication", () => {
84+
// Store whatever might be in here so we can restore it afterward.
85+
// TODO: We should probably pass this as an argument somehow instead of
86+
// manipulating the environment.
87+
const previousEnvPassword = process.env.PASSWORD
88+
89+
beforeEach(async () => {
90+
process.env.PASSWORD = "test"
91+
_codeServer = await integration.setup(["--auth=password"], "")
92+
})
93+
94+
afterEach(() => {
95+
process.env.PASSWORD = previousEnvPassword
96+
})
97+
98+
commonTests()
99+
100+
describe("inside code-server root", () => {
101+
it("should return a 404 for a nonexistent file", async () => {
102+
const resp = await codeServer().fetch(`/static/${commit}/${__filename}-does-not-exist`)
103+
expect(resp.status).toBe(404)
104+
105+
const content = await resp.json()
106+
expect(content.error).toMatch("ENOENT")
107+
})
108+
109+
it("should return a 200 and file contents for an existent file", async () => {
110+
const resp = await codeServer().fetch(`/static/${commit}${__filename}`)
111+
expect(resp.status).toBe(200)
112+
113+
const content = await resp.text()
114+
expect(content).toStrictEqual(await fs.readFile(__filename, "utf8"))
115+
})
116+
})
117+
118+
describe("outside code-server root", () => {
119+
it("should return a 401 for a nonexistent file", async () => {
120+
const resp = await codeServer().fetch(`/static/${commit}/${nonExistentTestFile}`)
121+
expect(resp.status).toBe(401)
122+
123+
const content = await resp.json()
124+
expect(content).toStrictEqual({ error: "Unauthorized" })
125+
})
126+
127+
it("should return a 401 for an existent file", async () => {
128+
const resp = await codeServer().fetch(`/static/${commit}${testFile}`)
129+
expect(resp.status).toBe(401)
130+
131+
const content = await resp.json()
132+
expect(content).toStrictEqual({ error: "Unauthorized" })
133+
})
134+
})
135+
})
136+
})

0 commit comments

Comments
 (0)