forked from denoland/deno_blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog_test.ts
366 lines (343 loc) · 11.9 KB
/
blog_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
364
365
366
// Copyright 2022 the Deno authors. All rights reserved. MIT license.
import { configureBlog, createBlogHandler, redirects } from "./blog.tsx";
import {
assert,
assertEquals,
assertStringIncludes,
} from "https://deno.land/std@0.193.0/testing/asserts.ts";
import { fromFileUrl, join } from "https://deno.land/std@0.193.0/path/mod.ts";
const BLOG_URL = new URL("./testdata/main.js", import.meta.url).href;
const TESTDATA_PATH = fromFileUrl(new URL("./testdata/", import.meta.url));
const BLOG_SETTINGS = await configureBlog(BLOG_URL, false, {
author: "The author",
title: "Test blog",
description: "This is some description.",
lang: "en-GB",
middlewares: [
redirects({
"/to_second": "second",
"/to_second_with_slash": "/second",
"/external_redirect": "https://example.com",
"second.html": "second",
}),
],
readtime: true,
});
const CONN_INFO = {
localAddr: {
transport: "tcp" as const,
hostname: "0.0.0.0",
port: 8000,
},
remoteAddr: {
transport: "tcp" as const,
hostname: "0.0.0.0",
port: 8001,
},
};
const blogHandler = createBlogHandler(BLOG_SETTINGS);
const testHandler = (req: Request): Response | Promise<Response> => {
return blogHandler(req, CONN_INFO);
};
Deno.test("index page", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/" />`,
);
assertStringIncludes(body, `Test blog`);
assertStringIncludes(body, `This is some description.`);
assertStringIncludes(body, `href="/first"`);
assertStringIncludes(body, `href="/second"`);
});
Deno.test("posts/ first", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/first"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/first" />`,
);
assertStringIncludes(body, `First post`);
assertStringIncludes(body, `The author`);
assertStringIncludes(body, `<time dateTime="2022-03-20T00:00:00.000Z">`);
assertStringIncludes(body, `<img src="first/hello.png" />`);
assertStringIncludes(body, `<p>Lorem Ipsum is simply dummy text`);
assertStringIncludes(body, `$100, $200, $300, $400, $500`);
assertStringIncludes(body, `min read`);
});
Deno.test("posts/ first (check canonical with params)", async () => {
const resp = await testHandler(
new Request("https://blog.deno.dev/first?foo=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,
`<link rel="canonical" href="https://blog.deno.dev/first" />`,
);
});
Deno.test("posts/ second", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/second"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/second" />`,
);
assertStringIncludes(body, `Second post`);
assertStringIncludes(body, `CUSTOM AUTHOR NAME`);
assertStringIncludes(body, `<time dateTime="2022-05-02T00:00:00.000Z">`);
assertStringIncludes(body, `<img src="second/hello2.png" />`);
assertStringIncludes(body, `<p>Lorem Ipsum is simply dummy text`);
});
Deno.test("posts/ third", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/third"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/third" />`,
);
assertStringIncludes(body, `Third post`);
assertStringIncludes(body, `CUSTOM AUTHOR NAME`);
assertStringIncludes(body, `<time dateTime="2022-08-19T00:00:00.000Z">`);
assertStringIncludes(body, `<iframe width="560" height="315"`);
assertStringIncludes(body, `<p>Lorem Ipsum is simply dummy text`);
});
Deno.test("posts/ fourth", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/fourth"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/fourth" />`,
);
assertStringIncludes(body, `Fourth post`);
assertStringIncludes(
body,
`<time dateTime="2023-01-30T00:00:00.000Z">`,
);
assertStringIncludes(
body,
`<button onclick="alert('hi!')">Click me!!!!!!</button>`,
);
});
Deno.test("posts/ seventh", async () => {
const resp = await testHandler(
new Request("https://blog.deno.dev/uses-pathname"),
);
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/uses-pathname" />`,
);
assertStringIncludes(body, `seventh post`);
assertStringIncludes(body, `<time dateTime="2022-05-02T00:00:00.000Z">`);
assertStringIncludes(body, `<p>Lorem Ipsum is simply dummy text`);
});
Deno.test("posts/ 中文", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/中文"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/%E4%B8%AD%E6%96%87" />`,
);
assertStringIncludes(body, `中文`);
assertStringIncludes(body, `<p>你好,世界!`);
});
Deno.test("posts/ sixth", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/sixth"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(
body,
`<a class="text-bluegray-500 font-bold" href="/?tag=sample">#sample</a>`,
);
assertStringIncludes(
body,
`<a class="text-bluegray-500 font-bold" href="/?tag=tags">#tags</a>`,
);
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/sixth" />`,
);
assertStringIncludes(body, `Sixth post`);
assertStringIncludes(body, `<time dateTime="2023-08-17T00:00:00.000Z">`);
assertStringIncludes(body, `Tags make it easier for readers`);
});
Deno.test("posts/ trailing slash redirects", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/second/"));
assert(resp);
assertEquals(resp.status, 307);
assertEquals(resp.headers.get("location"), "https://blog.deno.dev/second");
await resp.text();
});
Deno.test("external redirects", async () => {
const resp = await testHandler(
new Request("https://blog.deno.dev/external_redirect"),
);
assert(resp);
assertEquals(resp.status, 307);
assertEquals(resp.headers.get("location"), "https://example.com");
await resp.text();
});
Deno.test("redirect map", async () => {
{
const resp = await testHandler(
new Request("https://blog.deno.dev/second.html"),
);
assert(resp);
assertEquals(resp.status, 307);
assertEquals(resp.headers.get("location"), "/second");
await resp.text();
}
{
const resp = await testHandler(
new Request("https://blog.deno.dev/to_second"),
);
assert(resp);
assertEquals(resp.status, 307);
assertEquals(resp.headers.get("location"), "/second");
await resp.text();
}
{
const resp = await testHandler(
new Request("https://blog.deno.dev/to_second_with_slash"),
);
assert(resp);
assertEquals(resp.status, 307);
assertEquals(resp.headers.get("location"), "/second");
await resp.text();
}
});
Deno.test("static files in posts/ directory", async () => {
{
const resp = await testHandler(
new Request("https://blog.deno.dev/first/hello.png"),
);
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "image/png");
const bytes = new Uint8Array(await resp.arrayBuffer());
assertEquals(
bytes,
await Deno.readFile(join(TESTDATA_PATH, "./posts/first/hello.png")),
);
}
{
const resp = await testHandler(
new Request("https://blog.deno.dev/second/hello2.png"),
);
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "image/png");
const bytes = new Uint8Array(await resp.arrayBuffer());
assertEquals(
bytes,
await Deno.readFile(join(TESTDATA_PATH, "./posts/second/hello2.png")),
);
}
});
Deno.test("static files in root directory", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/cat.png"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "image/png");
const bytes = new Uint8Array(await resp.arrayBuffer());
assertEquals(bytes, await Deno.readFile(join(TESTDATA_PATH, "./cat.png")));
});
Deno.test("RSS feed", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/feed"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(
resp.headers.get("content-type"),
"application/atom+xml; charset=utf-8",
);
const body = await resp.text();
assertStringIncludes(body, `<title>Test blog</title>`);
assertStringIncludes(body, `First post`);
assertStringIncludes(body, `https://blog.deno.dev/first`);
assertStringIncludes(body, `Second post`);
assertStringIncludes(body, `https://blog.deno.dev/second`);
});
Deno.test(
"theme-color meta tag when dark theme is used [index page]",
async () => {
const darkThemeBlogHandler = createBlogHandler({
...BLOG_SETTINGS,
theme: "dark",
});
const darkThemeTestHandler = (req: Request) => {
return darkThemeBlogHandler(req, CONN_INFO);
};
const resp = await darkThemeTestHandler(
new Request("https://blog.deno.dev"),
);
const body = await resp.text();
assertStringIncludes(body, `<meta name="theme-color" content="#000" />`);
},
);
Deno.test(
"theme-color meta tag when dark theme is used [post page]",
async () => {
const darkThemeBlogHandler = createBlogHandler({
...BLOG_SETTINGS,
theme: "dark",
});
const darkThemeTestHandler = (req: Request) => {
return darkThemeBlogHandler(req, CONN_INFO);
};
const resp = await darkThemeTestHandler(
new Request("https://blog.deno.dev/first"),
);
const body = await resp.text();
assertStringIncludes(body, `<meta name="theme-color" content="#000" />`);
},
);
Deno.test("Plaintext response", async () => {
const plaintext = new Headers({
Accept: "text/plain",
});
const resp = await testHandler(
new Request("https://blog.deno.dev/first", {
headers: plaintext,
}),
);
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/plain;charset=UTF-8");
const body = await resp.text();
assert(body.startsWith("It was popularised in the 1960s"));
});