Skip to content

Commit 6f989f0

Browse files
committed
fixup! Add static route tests
1 parent 2a17651 commit 6f989f0

File tree

1 file changed

+109
-20
lines changed

1 file changed

+109
-20
lines changed

test/unit/routes/static.test.ts

+109-20
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,133 @@ import { tmpdir } from "../../utils/helpers"
44
import * as httpserver from "../../utils/httpserver"
55
import * as integration from "../../utils/integration"
66

7-
describe("static", () => {
8-
let codeServer: httpserver.HttpServer | undefined
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+
916
let testFile: string | undefined
1017
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 = "-"
1125

1226
beforeAll(async () => {
1327
const testDir = await tmpdir("static")
1428
testFile = path.join(testDir, "test")
1529
testFileContent = "static file contents"
30+
nonExistentTestFile = path.join(testDir, "i-am-not-here")
1631
await fs.writeFile(testFile, testFileContent)
1732
})
1833

1934
afterEach(async () => {
20-
if (codeServer) {
21-
await codeServer.close()
22-
codeServer = undefined
35+
if (_codeServer) {
36+
await _codeServer.close()
37+
_codeServer = undefined
2338
}
2439
})
2540

26-
it("/static unauthed", async () => {
27-
codeServer = await integration.setup(["--auth=none"], "")
28-
let resp = await codeServer.fetch("/static")
29-
expect(resp.status).toBe(404)
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+
})
3073

31-
resp = await codeServer.fetch(`/static/-${testFile}`)
32-
expect(resp.status).toBe(200)
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)
3377

34-
const content = await resp.text()
35-
expect(content).toStrictEqual(testFileContent)
78+
const content = await resp.text()
79+
expect(content).toStrictEqual(testFileContent)
80+
})
3681
})
3782

38-
it("/static authed", async () => {
39-
process.env.PASSWORD = "test"
40-
codeServer = await integration.setup(["--auth=password"], "")
41-
let resp = await codeServer.fetch("/static")
42-
expect(resp.status).toBe(404)
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)
43130

44-
resp = await codeServer.fetch(`/static/-${testFile}`)
45-
expect(resp.status).toBe(401)
131+
const content = await resp.json()
132+
expect(content).toStrictEqual({ error: "Unauthorized" })
133+
})
134+
})
46135
})
47136
})

0 commit comments

Comments
 (0)