-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
108 lines (105 loc) · 2.57 KB
/
test.ts
File metadata and controls
108 lines (105 loc) · 2.57 KB
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
import { app } from "./mod.ts";
import { assertEquals, assertStrictEquals } from "jsr:@std/assert";
async function testEndpoint(
url: string,
expectedStatus: number,
expectedContentType: string,
expectedBodyTypes: Record<string, string> = {},
) {
const req = new Request(url);
const res = await app.request(req);
let body;
try {
body = await res.json();
} catch (_e) {
body = {};
}
assertEquals(
res.status,
expectedStatus,
`Expected status ${expectedStatus}, got ${res.status}`,
);
if (expectedContentType) {
assertEquals(
res.headers.get("Content-Type")?.split(";")[0],
expectedContentType,
`Expected Content-Type ${expectedContentType}, got ${
res.headers.get("Content-Type")
}`,
);
}
for (const [key, type] of Object.entries(expectedBodyTypes)) {
assertStrictEquals(
typeof body[key],
type,
`Expected body.${key} to be ${type}, got ${typeof body[key]}`,
);
}
}
const testCases: Array<{
url: string;
expectedStatus: number;
expectedContentType: string;
expectedBodyTypes: Record<string, string>;
}> = [
{
url: "http://localhost/",
expectedStatus: 200,
expectedContentType: "application/json",
expectedBodyTypes: {
progress: "string",
day: "number",
"remaining.percentage": "undefined",
"remaining.daysLeft": "undefined",
},
},
{
url: "http://localhost/days",
expectedStatus: 200,
expectedContentType: "application/json",
expectedBodyTypes: { dayOfYear: "number" },
},
{
url: "http://localhost/percentage",
expectedStatus: 200,
expectedContentType: "application/json",
expectedBodyTypes: { percentage: "string" },
},
{
url: "http://localhost/remaining",
expectedStatus: 200,
expectedContentType: "application/json",
expectedBodyTypes: { remaining: "string" },
},
{
url: "http://localhost/decimal",
expectedStatus: 200,
expectedContentType: "application/json",
expectedBodyTypes: { decimal: "string" },
},
{
url: "http://localhost/remaining/days",
expectedStatus: 200,
expectedContentType: "application/json",
expectedBodyTypes: { remaining: "number" },
},
{
url: "http://localhost/notfound",
expectedStatus: 404,
expectedContentType: "",
expectedBodyTypes: {},
},
];
for (
const { url, expectedStatus, expectedContentType, expectedBodyTypes }
of testCases
) {
Deno.test(`GET ${url}`, async () => {
await testEndpoint(
url,
expectedStatus,
expectedContentType,
expectedBodyTypes,
);
});
}