Skip to content

Commit 9207207

Browse files
committed
tests: improve worker code coverage
1 parent 2bda9f8 commit 9207207

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function escapeHtml(str: string): string {
4545
['"', "&quot"],
4646
["'", "&#x27"],
4747
])
48-
return str.replace(/[&<>]/g, function (tag): string {
48+
return str.replace(/[&<>"']/g, function (tag): string {
4949
return tagsToReplace.get(tag) || tag
5050
})
5151
}

test/basic.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,3 @@ test("custom passwd", async () => {
185185
expect(putResponseJson.url).toStrictEqual(url) // url will not change
186186
expect(putResponseJson.manageUrl).toStrictEqual(`${url}:${wrongPasswd}`) // passwd may change
187187
})
188-
189-
// TODO: add tests for CORS

test/controlHeaders.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,46 @@ test("content disposition with specifying filename", async () => {
102102
`attachment; filename*=UTF-8''${altFilename}`,
103103
)
104104
})
105+
106+
test("other HTTP methods", async () => {
107+
const ctx = createExecutionContext()
108+
const resp = await workerFetch(
109+
ctx,
110+
new Request(BASE_URL, {
111+
method: "PATCH",
112+
}),
113+
)
114+
expect(resp.status).toStrictEqual(405)
115+
expect(resp.headers.has("Allow")).toBeTruthy()
116+
})
117+
118+
test("option method", async () => {
119+
const ctx = createExecutionContext()
120+
121+
const resp = await workerFetch(
122+
ctx,
123+
new Request(BASE_URL, {
124+
method: "OPTIONS",
125+
headers: {
126+
Origin: "https://example.com",
127+
"Access-Control-Request-Method": "PUT",
128+
},
129+
}),
130+
)
131+
expect(resp.status).toStrictEqual(200)
132+
expect(resp.headers.has("Access-Control-Allow-Origin")).toBeTruthy()
133+
expect(resp.headers.has("Access-Control-Allow-Methods")).toBeTruthy()
134+
expect(resp.headers.has("Access-Control-Max-Age")).toBeTruthy()
135+
136+
const resp1 = await workerFetch(
137+
ctx,
138+
new Request(BASE_URL, {
139+
method: "OPTIONS",
140+
headers: {
141+
Origin: "https://example.com",
142+
},
143+
}),
144+
)
145+
expect(resp1.status).toStrictEqual(200)
146+
expect(resp1.headers.has("Allow")).toBeTruthy()
147+
})

test/pages.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ test("url redirect", async () => {
3838
return url.slice(0, splitPoint) + "/u" + url.slice(splitPoint)
3939
}
4040

41-
expect(uploadResp["suggestedUrl"].includes(makeRedirectUrl(url)))
41+
expect(uploadResp.suggestedUrl).toBeDefined()
42+
expect(uploadResp.suggestedUrl!.includes(makeRedirectUrl(url)))
4243

4344
const resp = await workerFetch(ctx, makeRedirectUrl(url))
4445
expect(resp.status).toStrictEqual(302)
@@ -63,12 +64,16 @@ test("url redirect with illegal url", async () => {
6364
})
6465

6566
test("highlight", async () => {
66-
const content = 'print("hello world")'
67+
const content = 'print("<hello world>")'
6768
const ctx = createExecutionContext()
68-
const url = (await upload(ctx, { c: content }))["url"]
69+
const url = (await upload(ctx, { c: content })).url
6970
const resp = await workerFetch(ctx, `${url}?lang=html`)
7071
expect(resp.status).toStrictEqual(200)
7172
const body = await resp.text()
7273
expect(body.includes("language-html")).toBeTruthy()
73-
expect(body.includes(content)).toBeTruthy()
74+
expect(body.includes("print(&quot&lt;hello world&gt;&quot)")).toBeTruthy()
75+
76+
const resp1 = await workerFetch(ctx, `${url}?lang=<html>`)
77+
const body1 = await resp1.text()
78+
expect(body1.includes("language-&lt;html&gt;")).toBeTruthy()
7479
})

0 commit comments

Comments
 (0)