-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_test.ts
120 lines (96 loc) · 3.15 KB
/
mod_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
assertEquals,
assertStringIncludes,
} from "https://deno.land/std@0.103.0/testing/asserts.ts";
import { accumulate, Cache } from "./mod.ts";
Deno.test("accumulate - accumulates the file from the iterable", async () => {
const cache = {} as Cache;
await accumulate(gen(), cache);
assertEquals(Object.keys(cache), [
"/foo.txt",
"/foo/bar.html",
"/foo/bar/baz.txt",
]);
});
/* TODO(kt3k): Enable this when https://github.com/denoland/deno/issues/10508 is resolved.
Deno.test("serveFromCache - serves items from cache", async () => {
const cache = {} as Cache;
await accumulate(gen(), cache);
const listener = Deno.listen({ port: 3030 });
await new Promise((resolve) => { setTimeout(resolve, 300); });
const closer = serveFromCache(listener, cache, {});
await new Promise((resolve) => { setTimeout(resolve, 300); });
const resp = await fetch("http://0.0.0.0:3030/foo.txt");
assertEquals(await resp.text(), "foo");
await new Promise((resolve) => { setTimeout(resolve, 300); });
closer();
listener.close();
await new Promise((resolve) => { setTimeout(resolve, 300); });
});
*/
Deno.test("serve - serves the given assets", async () => {
const p = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--allow-net",
"--unstable",
"test_server.ts",
],
});
await new Promise((resolve) => setTimeout(resolve, 2000));
let res: Response;
res = await fetch("http://0.0.0.0:3030/__debug__");
assertStringIncludes(await res.text(), "debug page");
res = await fetch("http://0.0.0.0:3030/foo.txt");
assertEquals(await res.text(), "foo");
res = await fetch("http://0.0.0.0:3030/foo/bar.html");
assertEquals(await res.text(), "bar");
res = await fetch("http://0.0.0.0:3030/foo/bar/baz.txt");
assertEquals(await res.text(), "baz");
res = await fetch("http://0.0.0.0:3030/");
assertEquals(await res.text(), "index");
res = await fetch("http://0.0.0.0:3030/foo");
assertStringIncludes(await res.text(), "404 Not Found");
res = await fetch("http://0.0.0.0:3030/foo/");
assertEquals(await res.text(), "foo/index");
res = await fetch("http://0.0.0.0:3030/asdf.txt");
assertStringIncludes(await res.text(), "404 Not Found");
Deno.kill(p.pid, "SIGINT");
p.close();
});
Deno.test("serve - custom debug page path", async () => {
const p = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--allow-net",
"--unstable",
"test_server2.ts",
],
});
await new Promise((resolve) => setTimeout(resolve, 2000));
const res = await fetch("http://0.0.0.0:3031/__mypath__");
assertStringIncludes(await res.text(), "debug page");
p.close();
});
Deno.test("serve - custom 404 page", async () => {
const p = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--allow-net",
"--unstable",
"test_server3.ts",
],
});
await new Promise((resolve) => setTimeout(resolve, 2000));
const res = await fetch("http://0.0.0.0:3032/asdf");
assertEquals(await res.text(), "custom 404");
p.close();
});
async function* gen() {
yield new File(["foo"], "foo.txt");
yield new File(["bar"], "foo/bar.html");
yield new File(["baz"], "foo/bar/baz.txt");
}