-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
67 lines (56 loc) · 1.76 KB
/
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
// Copyright 2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertStringIncludes } from "./deps_test.ts";
import { app } from "./main.ts";
import { assert } from "./util.ts";
let start: Promise<string> | undefined;
let close: AbortController | undefined;
function setup(): Promise<string> {
if (start) {
return start;
}
let resolve: (value: string | PromiseLike<string>) => void;
start = new Promise((res) => resolve = res);
close = new AbortController();
// deno-lint-ignore no-explicit-any
function onListen({ secure, hostname, port }: any) {
app.removeEventListener("listen", onListen);
const url = `${secure ? "https" : "http"}://${hostname}:${port}/`;
resolve(url);
}
app.addEventListener("listen", onListen);
app.listen({ signal: close.signal, port: 5001 });
return start;
}
function teardown() {
if (!close) {
return;
}
close.abort();
close = undefined;
}
Deno.test({
name: "route testing",
async fn() {
const server = await setup();
let res = await fetch(server);
assertEquals(res.status, 200);
assertStringIncludes(await res.text(), ">Deno Doc<");
// validate that badge.svg is available
res = await fetch(`${server}badge.svg`);
assertEquals(res.status, 200);
assertEquals(res.headers.get("content-type"), "image/svg+xml");
await res.arrayBuffer();
// doc query URLs are redirected to perm-link of the redirected URL
res = await fetch(
`${server}doc?url=${encodeURIComponent("https://esm.sh/preact")}`,
);
assertEquals(res.status, 200);
assert(
res.url.match(
/http:\/{2}(0\.){3}0:5001\/https:\/{2}esm\.sh\/v\d{1,3}\/preact@\d+\.\d+\.\d+\/src\/index\.d\.ts/,
),
);
await res.text();
teardown();
},
});