-
Notifications
You must be signed in to change notification settings - Fork 592
/
main_test.ts
363 lines (330 loc) · 11.6 KB
/
main_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import { ServerContext } from "../server.ts";
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import manifest from "./fixture/fresh.gen.ts";
import options from "./fixture/options.ts";
const ctx = await ServerContext.fromManifest(manifest, options);
const router = (req: Request) => {
return ctx.handler()(req, {
localAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
remoteAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
});
};
Deno.test("/ page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
assertEquals(resp.headers.get("server"), "fresh test server");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en">`);
assertStringIncludes(body, "test.js");
assertStringIncludes(body, "<p>Hello!</p>");
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
assertStringIncludes(body, `>[{"message":"Hello!"}]</script>`);
assertStringIncludes(
body,
`<meta name="description" content="Hello world!" />`,
);
});
Deno.test("/props/123 page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/props/123"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(
body,
`{"params":{"id":"123"},"url":"https://fresh.deno.dev/props/123","route":"/props/:id"}`,
);
});
Deno.test("/[name] page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/bar"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, "<div>Hello bar</div>");
});
Deno.test("/intercept - GET html", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
headers: { "accept": "text/html" },
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertStringIncludes(body, "<div>This is HTML</div>");
});
Deno.test("/intercept - GET text", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
headers: { "accept": "text/plain" },
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "This is plain text");
});
Deno.test("/intercept - POST", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
method: "POST",
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "POST response");
});
Deno.test("/intercept - DELETE", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
method: "DELETE",
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 405);
});
Deno.test("/intercept_args - GET html", async () => {
const req = new Request("https://fresh.deno.dev/intercept_args", {
headers: { "accept": "text/html" },
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertStringIncludes(body, "<div>intercepted</div>");
});
Deno.test("/api/get_only - NOTAMETHOD", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/api/get_only", {
method: "NOTAMETHOD",
}),
);
assert(resp);
assertEquals(resp.status, 405);
});
Deno.test("/api/xyz not found", async () => {
const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
assert(resp);
assertEquals(resp.status, 404);
const body = await resp.text();
assertStringIncludes(body, "404 not found: /api/xyz");
});
Deno.test("/static page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/static"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assert(!body.includes(`main.js`));
assert(!body.includes(`island-test.js`));
assertStringIncludes(body, "<p>This is a static page.</p>");
assertStringIncludes(body, `src="/image.png?__frsh_c=`);
assert(!body.includes("__FRSH_ISLAND_PROPS"));
});
Deno.test("/books/:id page - /books/123", async () => {
const resp = await router(new Request("https://fresh.deno.dev/books/123"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, "<div>Book 123</div>");
});
Deno.test("/books/:id page - /books/abc", async () => {
const resp = await router(new Request("https://fresh.deno.dev/books/abc"));
assert(resp);
assertEquals(resp.status, 404);
});
Deno.test("redirect /pages/fresh/ to /pages/fresh", async () => {
const resp = await router(new Request("https://fresh.deno.dev/pages/fresh/"));
assert(resp);
assertEquals(resp.status, 307);
assertEquals(
resp.headers.get("location"),
"https://fresh.deno.dev/pages/fresh",
);
});
Deno.test("/failure", async () => {
const resp = await router(new Request("https://fresh.deno.dev/failure"));
assert(resp);
assertEquals(resp.status, 500);
const body = await resp.text();
assert(body.includes("500 internal error: it errored!"));
});
Deno.test("/foo/:path*", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo/bar/baz"));
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assert(body.includes("bar/baz"));
});
Deno.test("static file - by file path", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo.txt"));
assertEquals(resp.status, 200);
const body = await resp.text();
assert(body.startsWith("bar"));
const etag = resp.headers.get("etag");
assert(etag);
// The etag is not weak, because this did not go through content encoding, so
// this is not a real server.
assert(!etag.startsWith("W/"), "etag should be weak");
assertEquals(resp.headers.get("content-type"), "text/plain");
const resp2 = await router(
new Request("https://fresh.deno.dev/foo.txt", {
headers: {
"if-none-match": etag,
},
}),
);
assertEquals(resp2.status, 304);
assertEquals(resp2.headers.get("etag"), etag);
assertEquals(resp2.headers.get("content-type"), "text/plain");
const resp3 = await router(
new Request("https://fresh.deno.dev/foo.txt", {
headers: {
"if-none-match": `W/${etag}`,
},
}),
);
assertEquals(resp3.status, 304);
assertEquals(resp3.headers.get("etag"), etag);
assertEquals(resp3.headers.get("content-type"), "text/plain");
});
Deno.test("static file - by 'hashed' path", async () => {
// Check that the file path have the BUILD_ID
const resp = await router(
new Request("https://fresh.deno.dev/assetsCaching"),
);
const body = await resp.text();
const imgFilePath = body.match(/img id="img-with-hashing" src="(.*?)"/)?.[1];
assert(imgFilePath);
assert(imgFilePath.includes(`?__frsh_c=${globalThis.__FRSH_BUILD_ID}`));
// check the static file is served corectly under its cacheable route
const resp2 = await router(
new Request(`https://fresh.deno.dev${imgFilePath}`),
);
const _ = await resp2.text();
assertEquals(resp2.status, 200);
assertEquals(
resp2.headers.get("cache-control"),
"public, max-age=31536000, immutable",
);
const resp3 = await router(
new Request(`https://fresh.deno.dev${imgFilePath}`, {
headers: {
"if-none-match": resp2.headers.get("etag")!,
},
}),
);
assertEquals(resp3.status, 304);
// ensure asset hook is not applied on file explicitly excluded with attribute
const imgFilePathWithNoCache = body.match(
/img id="img-without-hashing" src="(.*?)"/,
)?.[1];
assert(imgFilePathWithNoCache);
assert(
!imgFilePathWithNoCache.includes(globalThis.__FRSH_BUILD_ID),
"img-without-hashing",
);
// ensure asset hook is applied on img within an island
const imgInIsland = body.match(/img id="img-in-island" src="(.*?)"/)?.[1];
assert(imgInIsland);
assert(imgInIsland.includes(globalThis.__FRSH_BUILD_ID), "img-in-island");
// verify that the asset hook is applied to the srcset
const imgInIslandSrcSet = body.match(/srcset="(.*?)"/)?.[1];
assert(imgInIslandSrcSet);
assert(
imgInIslandSrcSet.includes(globalThis.__FRSH_BUILD_ID),
"img-in-island-srcset",
);
// verify that the asset hook is not applied to img outside reference out of the static folder
const imgMissing = body.match(/img id="img-missing" src="(.*?)"/)?.[1];
assert(imgMissing);
assert(
!imgMissing.includes(globalThis.__FRSH_BUILD_ID),
"Applying hash on unknown asset",
);
});
Deno.test("/params/:path*", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/params/bar/baz"),
);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "bar/baz");
});
Deno.test("/connInfo", async () => {
const resp = await router(new Request("https://fresh.deno.dev/connInfo"));
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "127.0.0.1");
});
Deno.test({
name: "/middleware - root",
fn: async () => {
const resp = await router(
new Request("https://fresh.deno.dev/middleware_root"),
);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertStringIncludes(body, "root_mw");
assert(!body.includes("layer1_mw"));
},
});
Deno.test({
name: "/middleware - layer 2 middleware",
fn: async () => {
const resp = await router(
new Request("https://fresh.deno.dev/layeredMdw/layer2/abc"),
);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertStringIncludes(body, "root_mw");
assertStringIncludes(body, "layer1_mw");
assertStringIncludes(body, "layer2_mw");
// layered 2 should not run layer 3 middleware
assert(!body.includes("layer3_mw"));
const resp1 = await router(
new Request("https://fresh.deno.dev/layeredMdw/layer2-no-mw/without_mw"),
);
assert(resp1);
assertEquals(resp1.status, 200);
const body1 = await resp1.text();
assertStringIncludes(body1, "root_mw");
assertStringIncludes(body1, "layer1_mw");
// layered 2 should not run layer 2 or 3 middleware
assert(!body1.includes("layer2_mw"));
assert(!body1.includes("layer3_mw"));
},
});
Deno.test({
name: "/middleware - layer 3 middleware",
fn: async () => {
// layered 3 should contain layer 3 middleware data
const resp = await router(
new Request("https://fresh.deno.dev/layeredMdw/layer2/layer3/abc"),
);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertStringIncludes(body, "root_mw");
assertStringIncludes(body, "layer1_mw");
assertStringIncludes(body, "layer3_mw");
assertEquals(resp.headers.get("layer3"), "fresh test server layer3");
// the below ensure that the middlewware are applied in the correct order.
// i.e response header set from layer3 middleware is overwritten
// by the reponse header in layer 0
assertEquals(resp.headers.get("server"), "fresh test server");
},
});