forked from keroxp/servest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_static_test.ts
64 lines (62 loc) · 2.02 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
64
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import {
assertEquals,
assertMatch,
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { createRecorder } from "./testing.ts";
import { serveStatic } from "./serve_static.ts";
import { group } from "./_test_util.ts";
import { createApp } from "./app.ts";
group("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.test(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));
});
});
});
group("serveStatic integration", (t) => {
t.setupAll(() => {
const router = createApp();
router.use(serveStatic("./fixtures/public"));
const l = router.listen({ port: 9988 });
return () => l.close();
});
t.test("basic", async () => {
const resp = await fetch("http://127.0.0.1:9988/index.html");
assertEquals(resp.status, 200);
await resp.text();
});
t.test("not found", async () => {
const resp = await fetch("http://127.0.0.1:9988/no-file");
assertEquals(resp.status, 404);
await resp.text();
});
t.test("Capitalized", async () => {
const resp = await fetch("http://127.0.0.1:9988/File.txt");
assertEquals(resp.status, 200);
await resp.text();
});
t.test("Multi bytes", async () => {
const resp = await fetch("http://127.0.0.1:9988/日本語.txt");
assertEquals(resp.status, 200);
await resp.text();
});
});