-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
file_server_test.ts
249 lines (235 loc) · 6.4 KB
/
file_server_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { assertEquals, fail } from "@std/assert";
import {
MediaType,
serveDirWithTs,
serveFileWithTs,
transpile,
} from "./mod.ts";
async function readTextFile(path: string) {
return await Deno.readTextFile(new URL(path, import.meta.url));
}
async function transpileFile(path: string, requestUrl: string) {
const url = new URL(`ts-serve:///${requestUrl}`);
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);
}
Deno.test({
name: "file server - serveFileWithTs",
async fn() {
const controller = new AbortController();
const serverPromise = Deno.serve({
signal: controller.signal,
port: 8886,
onListen() {},
}, (request) => {
const { pathname } = new URL(request.url);
if (pathname === "/mod.ts") {
return serveFileWithTs(request, "./mod.ts");
}
if (pathname === "/test/a.tsx") {
return serveFileWithTs(request, "./test/a.tsx");
}
if (pathname === "/test/a.jsx") {
return serveFileWithTs(request, "./test/a.jsx");
}
throw new Error("unreachable");
});
{
const res = await fetch("http://localhost:8886/mod.ts");
assertEquals(
await res.text(),
await transpileFile("./mod.ts", "http://localhost:8886/mod.ts"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
}
{
const res = await fetch("http://localhost:8886/test/a.tsx");
assertEquals(
await res.text(),
await transpileFile("./test/a.tsx", "http://localhost:8886/test/a.tsx"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
}
{
const res = await fetch("http://localhost:8886/test/a.jsx");
assertEquals(
await res.text(),
await transpileFile("./test/a.jsx", "http://localhost:8886/test/a.jsx"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
}
controller.abort();
await serverPromise.finished;
},
});
Deno.test({
name: "file server - serveFileWithTs (not ts file)",
async fn() {
const res = await serveFileWithTs(
new Request("http://localhost/README.md"),
"./README.md",
);
assertEquals(
await res.text(),
await readTextFile("./README.md"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/markdown; charset=UTF-8",
);
},
});
Deno.test({
name: "file server - serveFileWithTs (invalid url)",
async fn() {
const request = new Request("http://localhost/");
Object.defineProperty(request, "url", {
value: "http://",
});
// Determine file type from file path and don't throw error
const res = await serveFileWithTs(request, "./mod.ts");
assertEquals(
await res.text(),
await transpileFile("./mod.ts", "http://"),
);
assertEquals(res.status, 200);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
},
});
Deno.test({
name: "file server - serveDirWithTs",
async fn() {
const controller = new AbortController();
const serverPromise = Deno.serve(
{
signal: controller.signal,
port: 8887,
onListen() {},
},
(request) => serveDirWithTs(request, { quiet: true }),
);
{
const res = await fetch("http://localhost:8887/mod.ts");
assertEquals(
await res.text(),
await transpileFile("./mod.ts", "http://localhost:8887/mod.ts"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
}
{
const res = await fetch("http://localhost:8887/test/a.tsx");
assertEquals(
await res.text(),
await transpileFile("./test/a.tsx", "http://localhost:8887/test/a.tsx"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
}
{
const res = await fetch("http://localhost:8887/test/a.jsx");
assertEquals(
await res.text(),
await transpileFile("./test/a.jsx", "http://localhost:8887/test/a.jsx"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
}
controller.abort();
await serverPromise.finished;
},
});
Deno.test({
name: "file server - serveDirWithTs (not ts file)",
async fn() {
const res = await serveDirWithTs(
new Request("http://localhost/README.md"),
{ quiet: true },
);
assertEquals(
await res.text(),
await readTextFile("./README.md"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/markdown; charset=UTF-8",
);
},
});
Deno.test({
name: "file server - serveDirWithTs (not found)",
async fn() {
const res = await serveDirWithTs(
new Request("http://localhost/NOT_FOUND"),
{ quiet: true },
);
assertEquals(await res.text(), "Not Found");
assertEquals(res.status, 404);
assertEquals(
res.headers.get("Content-Type"),
"text/plain;charset=UTF-8",
);
},
});
Deno.test({
// original serveDir throws since 0.166.0~??, maybe
ignore: true,
name: "file server - serveDirWithTs (relative url)",
async fn() {
const request = new Request("http://localhost/");
Object.defineProperty(request, "url", {
value: "/mod.ts",
});
const res = await serveDirWithTs(request, { quiet: true });
assertEquals(
await res.text(),
await transpileFile("./mod.ts", "/mod.ts"),
);
assertEquals(
res.headers.get("Content-Type"),
"text/javascript; charset=UTF-8",
);
},
});
Deno.test({
// original serveDir throws since 0.166.0~??, maybe
ignore: true,
name: "file server - serveDirWithTs (invalid url)",
async fn() {
const request = new Request("http://localhost/");
Object.defineProperty(request, "url", {
value: "http://",
});
const res = await serveDirWithTs(request, { quiet: true });
assertEquals(await res.text(), "Bad Request");
assertEquals(res.status, 400);
assertEquals(
res.headers.get("Content-Type"),
"text/plain;charset=UTF-8",
);
},
});