-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
serve_static_test.ts
63 lines (60 loc) · 2 KB
/
serve_static_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { runIfMain } from "./vendor/https/deno.land/std/testing/mod.ts";
import {
assertEquals,
assertMatch
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { createRecorder } from "./testing.ts";
import { serveStatic } from "./serve_static.ts";
import { it } from "./test_util.ts";
import { createRouter } from "./router.ts";
it("serveStatic", t => {
const func = serveStatic("./fixtures/public", {
contentTypeMap: new Map([[".vue", "application/vue"]])
});
const data: [string, string][] = [
["/", "text/html"],
["/about/", "text/html"],
["/doc", "text/html"],
["/index.css", "text/css"],
["/index.ts", "application/javascript"],
["/index.js", "application/javascript"],
["/sample.vue", "application/vue"],
["/sample.xx", "application/octet-stream"]
];
data.forEach(([path, type]) => {
t.run(path, async () => {
const rec = createRecorder({ url: path });
await func(rec);
const resp = await rec.response();
assertEquals(resp.status, 200);
const contentType = resp.headers.get("content-type");
assertMatch(contentType, new RegExp(type));
});
});
});
it("serveStatic integration", t => {
t.beforeAfterAll(() => {
const router = createRouter();
router.use(serveStatic("./fixtures/public"));
const l = router.listen(":9988");
return () => l.close();
});
t.run("basic", async () => {
const resp = await fetch("http://127.0.0.1:9988/index.html");
assertEquals(resp.status, 200);
});
t.run("not found", async () => {
const resp = await fetch("http://127.0.0.1:9988/no-file");
assertEquals(resp.status, 404);
});
t.run("Capitalized", async () => {
const resp = await fetch("http://127.0.0.1:9988/File.txt");
assertEquals(resp.status, 200);
});
t.run("Multi bytes", async () => {
const resp = await fetch("http://127.0.0.1:9988/日本語.txt");
assertEquals(resp.status, 200);
});
});
runIfMain(import.meta);