-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
oak_test.ts
194 lines (184 loc) · 5.15 KB
/
oak_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { assertEquals, fail } from "@std/assert";
import { delay } from "@std/async";
import { Application } from "@oak/oak";
import {
createTsMiddleware,
MediaType,
transpile,
tsMiddleware,
} from "./mod.ts";
const port = 8888;
const jsContentType = "text/javascript; charset=UTF-8";
const importMap = {
imports: {
"@oak/oak": "./unknown.ts",
"@std/assert": "./unknown.ts",
"@std/async": "./unknown.ts",
},
};
async function startServer() {
const app = new Application();
app.use(
createTsMiddleware({ importMap }),
);
app.use(async (ctx, next) => {
try {
await ctx.send({ root: "./" });
} catch {
await next();
}
});
const killSignal = new AbortController();
const listener = Promise.withResolvers();
app.addEventListener("listen", (e) => {
listener.resolve(e);
});
// deno-lint-ignore no-unused-vars
const server = app.listen({ port, signal: killSignal.signal });
await listener.promise;
return async function abort() {
killSignal.abort();
await delay(1000);
// Note: waiting to resolve https://github.com/oakserver/oak/issues/686
// await server;
};
}
async function request(path: string) {
const res = await fetch(`http://localhost:${port}${path}`);
return {
result: await res.text(),
contentType: res.headers.get("Content-Type"),
};
}
async function readTextFile(path: string) {
return await Deno.readTextFile(new URL(path, import.meta.url));
}
async function transpileFile(path: string) {
const url = new URL(path, `http://localhost:${port}`);
const mediaType = path.endsWith(".ts")
? MediaType.TypeScript
: path.endsWith(".tsx")
? MediaType.Tsx
: path.endsWith(".jsx")
? MediaType.Jsx
: fail("unknown extension");
return await transpile(
await readTextFile(path),
url,
mediaType,
{ importMap },
);
}
Deno.test({
name: "oak middleware",
async fn() {
const abortServer = await startServer();
{
const { result, contentType } = await request("/oak_test.ts");
assertEquals(result, await transpileFile("./oak_test.ts"));
assertEquals(contentType, jsContentType);
}
{
const { result, contentType } = await request("/oak_test.ts");
assertEquals(result, await transpileFile("./oak_test.ts"));
assertEquals(contentType, jsContentType);
}
{
const { result, contentType } = await request(
"/utils/transpile_bench.ts",
);
assertEquals(result, await transpileFile("./utils/transpile_bench.ts"));
assertEquals(contentType, jsContentType);
}
{
const { result, contentType } = await request("/test/a.tsx");
assertEquals(result, await transpileFile("./test/a.tsx"));
assertEquals(contentType, jsContentType);
}
{
const { result, contentType } = await request("/test/a.jsx");
assertEquals(result, await transpileFile("./test/a.jsx"));
assertEquals(contentType, jsContentType);
}
await abortServer();
},
});
Deno.test({
name: "oak middleware - not ts response",
async fn() {
const app = new Application();
app.use(tsMiddleware);
app.use((ctx) => {
ctx.response.body = "texttexttext!!!";
ctx.response.type = ".md";
});
const res = await app.handle(new Request("http://localhost/"));
assertEquals(await res!.text(), "texttexttext!!!");
},
});
Deno.test({
name: "oak middleware - null response",
async fn() {
const app = new Application();
app.use(tsMiddleware);
app.use((ctx) => {
ctx.response.body = null;
ctx.response.type = ".ts";
});
const res = await app.handle(new Request("http://localhost/"));
assertEquals(await res!.text(), "");
},
});
Deno.test({
name: "oak middleware - string response",
async fn() {
const code = "function name(params:type) {}";
const app = new Application();
app.use(tsMiddleware);
app.use((ctx) => {
ctx.response.body = code;
ctx.response.type = ".ts";
});
const res = await app.handle(new Request("http://localhost/"));
assertEquals(
await res!.text(),
await transpile(code, new URL("http://localhost/"), MediaType.TypeScript),
);
},
});
Deno.test({
name: "oak middleware - u8 response",
async fn() {
const code = "function name(params:type) {}";
const app = new Application();
app.use(tsMiddleware);
app.use((ctx) => {
ctx.response.body = new TextEncoder().encode(code);
ctx.response.type = ".ts";
});
const res = await app.handle(new Request("http://localhost/"));
assertEquals(
await res!.text(),
await transpile(code, new URL("http://localhost/"), MediaType.TypeScript),
);
},
});
Deno.test({
name: "oak middleware - func response",
async fn() {
const code = "function name(params:type) {}";
const app = new Application();
app.use(tsMiddleware);
app.use((ctx) => {
ctx.response.body = () => code;
ctx.response.type = ".ts";
});
const res = await app.handle(new Request("http://localhost/"));
const aaa = await res!.text();
console.log(aaa);
assertEquals(
aaa,
await transpile(code, new URL("http://localhost/"), MediaType.TypeScript),
);
},
});