Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/test/ejs-async.test.js
107 views
1
'use strict'
2
3
const { test } = require('node:test')
4
const Fastify = require('fastify')
5
const fs = require('node:fs')
6
const minifier = require('html-minifier-terser')
7
8
test('reply.view with ejs engine and async: true (global option)', async t => {
9
t.plan(4)
10
const fastify = Fastify()
11
const ejs = require('ejs')
12
13
fastify.register(require('../index'), {
14
engine: { ejs },
15
options: { async: true },
16
templates: 'templates'
17
})
18
19
fastify.get('/', (_req, reply) => {
20
reply.view('ejs-async.ejs')
21
})
22
23
await fastify.listen({ port: 0 })
24
25
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
26
27
const responseContent = await result.text()
28
29
t.assert.strictEqual(result.status, 200)
30
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
31
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
32
t.assert.strictEqual(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), responseContent)
33
34
await fastify.close()
35
})
36
37
test('reply.view with ejs engine, async: true (global option), and production: true', async t => {
38
const numTests = 2
39
t.plan(numTests * 4)
40
const fastify = Fastify()
41
const ejs = require('ejs')
42
43
fastify.register(require('../index'), {
44
engine: { ejs },
45
production: true,
46
options: { async: true },
47
templates: 'templates'
48
})
49
50
fastify.get('/', (_req, reply) => {
51
reply.view('ejs-async.ejs')
52
})
53
54
await fastify.listen({ port: 0 })
55
56
for (let i = 0; i < numTests; i++) {
57
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
58
59
const responseContent = await result.text()
60
61
t.assert.strictEqual(result.status, 200)
62
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
63
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
64
t.assert.strictEqual(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), responseContent)
65
66
if (i === numTests - 1) await fastify.close()
67
}
68
})
69
70
const minifierOpts = { collapseWhitespace: true }
71
test('reply.view with ejs engine, async: true (global option), and html-minifier-terser', async t => {
72
t.plan(4)
73
const fastify = Fastify()
74
const ejs = require('ejs')
75
76
fastify.register(require('../index'), {
77
engine: { ejs },
78
options: {
79
async: true,
80
useHtmlMinifier: minifier,
81
htmlMinifierOptions: minifierOpts
82
},
83
templates: 'templates'
84
})
85
86
fastify.get('/', (_req, reply) => {
87
reply.view('ejs-async.ejs')
88
})
89
90
await fastify.listen({ port: 0 })
91
92
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
93
94
const responseContent = await result.text()
95
96
t.assert.strictEqual(result.status, 200)
97
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
98
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
99
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), minifierOpts), responseContent)
100
101
await fastify.close()
102
})
103
104
test('reply.view with ejs engine, async: true (global option), and html-minifier without option', async t => {
105
t.plan(4)
106
const fastify = Fastify()
107
const ejs = require('ejs')
108
109
fastify.register(require('../index'), {
110
engine: { ejs },
111
options: {
112
async: true,
113
useHtmlMinifier: minifier
114
},
115
templates: 'templates'
116
})
117
118
fastify.get('/', (_req, reply) => {
119
reply.view('ejs-async.ejs')
120
})
121
122
await fastify.listen({ port: 0 })
123
124
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
125
126
const responseContent = await result.text()
127
128
t.assert.strictEqual(result.status, 200)
129
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
130
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
131
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true })), responseContent)
132
133
await fastify.close()
134
})
135
136
test('reply.view with ejs engine, async: true (global option), and html-minifier in production mode', async t => {
137
const numTests = 3
138
t.plan(numTests * 4)
139
const fastify = Fastify()
140
const ejs = require('ejs')
141
142
fastify.register(require('../index'), {
143
engine: { ejs },
144
production: true,
145
options: {
146
async: true,
147
useHtmlMinifier: minifier,
148
htmlMinifierOptions: minifierOpts
149
},
150
templates: 'templates'
151
})
152
153
fastify.get('/', (_req, reply) => {
154
reply.view('ejs-async.ejs')
155
})
156
157
await fastify.listen({ port: 0 })
158
159
for (let i = 0; i < numTests; i++) {
160
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
161
162
const responseContent = await result.text()
163
164
t.assert.strictEqual(result.status, 200)
165
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
166
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
167
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), minifierOpts), responseContent)
168
169
if (i === numTests - 1) await fastify.close()
170
}
171
})
172
173
test('reply.view with ejs engine and async: true (local option)', async t => {
174
t.plan(4)
175
const fastify = Fastify()
176
const ejs = require('ejs')
177
178
fastify.register(require('../index'), {
179
engine: { ejs },
180
templates: 'templates'
181
})
182
183
fastify.get('/', (_req, reply) => {
184
reply.view('ejs-async.ejs', {}, { async: true })
185
})
186
187
await fastify.listen({ port: 0 })
188
189
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
190
191
const responseContent = await result.text()
192
193
t.assert.strictEqual(result.status, 200)
194
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
195
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
196
t.assert.strictEqual(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), responseContent)
197
198
await fastify.close()
199
})
200
201
test('reply.view with ejs engine, async: true (local option), and production: true', async t => {
202
const numTests = 5
203
t.plan(numTests * 4)
204
const fastify = Fastify()
205
const ejs = require('ejs')
206
207
fastify.register(require('../index'), {
208
engine: { ejs },
209
production: true,
210
templates: 'templates'
211
})
212
213
fastify.get('/', (_req, reply) => {
214
reply.view('ejs-async.ejs', {}, { async: true })
215
})
216
217
await fastify.listen({ port: 0 })
218
219
for (let i = 0; i < numTests; i++) {
220
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
221
222
const responseContent = await result.text()
223
224
t.assert.strictEqual(result.status, 200)
225
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
226
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
227
t.assert.strictEqual(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), responseContent)
228
229
if (i === numTests - 1) await fastify.close()
230
}
231
})
232
233
test('reply.view with ejs engine, async: true (local override), and html-minifier-terser', async t => {
234
t.plan(4)
235
const fastify = Fastify()
236
const ejs = require('ejs')
237
238
fastify.register(require('../index'), {
239
engine: { ejs },
240
options: {
241
async: false,
242
useHtmlMinifier: minifier,
243
htmlMinifierOptions: minifierOpts
244
},
245
templates: 'templates'
246
})
247
248
fastify.get('/', (_req, reply) => {
249
reply.view('ejs-async.ejs', {}, { async: true })
250
})
251
252
await fastify.listen({ port: 0 })
253
254
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
255
256
const responseContent = await result.text()
257
258
t.assert.strictEqual(result.status, 200)
259
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
260
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
261
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), minifierOpts), responseContent)
262
263
await fastify.close()
264
})
265
266
test('reply.view with ejs engine, async: false (local override), and html-minifier-terser', async t => {
267
t.plan(4)
268
const fastify = Fastify()
269
const ejs = require('ejs')
270
271
fastify.register(require('../index'), {
272
engine: { ejs },
273
options: {
274
async: true,
275
useHtmlMinifier: minifier,
276
htmlMinifierOptions: minifierOpts
277
},
278
templates: 'templates'
279
})
280
281
fastify.get('/', (_req, reply) => {
282
reply.view('index.ejs', { text: 'text' }, { async: false })
283
})
284
285
await fastify.listen({ port: 0 })
286
287
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
288
289
const responseContent = await result.text()
290
291
t.assert.strictEqual(result.status, 200)
292
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
293
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
294
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/index.ejs', 'utf8'), { text: 'text' }, { async: false }), minifierOpts), responseContent)
295
296
await fastify.close()
297
})
298
299
test('reply.view with ejs engine, async: true (local override), and html-minifier-terser in production mode', async t => {
300
const numTests = 3
301
t.plan(numTests * 4)
302
const fastify = Fastify()
303
const ejs = require('ejs')
304
305
fastify.register(require('../index'), {
306
engine: { ejs },
307
production: true,
308
options: {
309
async: false,
310
useHtmlMinifier: minifier,
311
htmlMinifierOptions: minifierOpts
312
},
313
templates: 'templates'
314
})
315
316
fastify.get('/', (_req, reply) => {
317
reply.view('ejs-async.ejs', {}, { async: true })
318
})
319
320
await fastify.listen({ port: 0 })
321
322
for (let i = 0; i < numTests; i++) {
323
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
324
325
const responseContent = await result.text()
326
327
t.assert.strictEqual(result.status, 200)
328
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
329
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
330
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), minifierOpts), responseContent)
331
332
if (i === numTests - 1) await fastify.close()
333
}
334
})
335
336
test('reply.view with ejs engine, async: false (local override), and html-minifier-terser in production mode', async t => {
337
const numTests = 2
338
t.plan(numTests * 4)
339
const fastify = Fastify()
340
const ejs = require('ejs')
341
342
fastify.register(require('../index'), {
343
engine: { ejs },
344
production: true,
345
options: {
346
async: true,
347
useHtmlMinifier: minifier,
348
htmlMinifierOptions: minifierOpts
349
},
350
templates: 'templates'
351
})
352
353
fastify.get('/', (_req, reply) => {
354
reply.view('index.ejs', { text: 'text' }, { async: false })
355
})
356
357
await fastify.listen({ port: 0 })
358
359
for (let i = 0; i < numTests; i++) {
360
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
361
362
const responseContent = await result.text()
363
364
t.assert.strictEqual(result.status, 200)
365
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
366
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
367
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/index.ejs', 'utf8'), { text: 'text' }, { async: false }), minifierOpts), responseContent)
368
369
if (i === numTests - 1) await fastify.close()
370
}
371
})
372
373
test('reply.view with ejs engine and async: true and raw template', async t => {
374
t.plan(4)
375
const fastify = Fastify()
376
const ejs = require('ejs')
377
378
fastify.register(require('../index'), {
379
engine: { ejs }
380
})
381
382
fastify.get('/', (_req, reply) => {
383
reply.view({ raw: fs.readFileSync('./templates/ejs-async.ejs', 'utf8') }, {}, { async: true })
384
})
385
386
await fastify.listen({ port: 0 })
387
388
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
389
390
const responseContent = await result.text()
391
392
t.assert.strictEqual(result.status, 200)
393
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
394
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
395
t.assert.strictEqual(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), responseContent)
396
397
await fastify.close()
398
})
399
400
test('reply.view with ejs engine and async: true and function template', async t => {
401
t.plan(4)
402
const fastify = Fastify()
403
const ejs = require('ejs')
404
405
fastify.register(require('../index'), {
406
engine: { ejs }
407
})
408
409
fastify.get('/', (_req, reply) => {
410
reply.view(ejs.compile(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), { async: true }), {})
411
})
412
413
await fastify.listen({ port: 0 })
414
415
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
416
417
const responseContent = await result.text()
418
419
t.assert.strictEqual(result.status, 200)
420
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
421
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
422
t.assert.strictEqual(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), responseContent)
423
424
await fastify.close()
425
})
426
427
test('reply.view with promise error', async t => {
428
t.plan(1)
429
const fastify = Fastify()
430
const ejs = require('ejs')
431
432
fastify.register(require('../index'), {
433
engine: { ejs }
434
})
435
436
fastify.get('/', (_req, reply) => {
437
reply.view(() => Promise.reject(new Error('error')), {})
438
})
439
440
await fastify.listen({ port: 0 })
441
442
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
443
444
t.assert.strictEqual(result.status, 500)
445
446
await fastify.close()
447
})
448
449
test('reply.viewAsync with ejs engine and async: true (global option)', async t => {
450
t.plan(4)
451
const fastify = Fastify()
452
const ejs = require('ejs')
453
454
fastify.register(require('../index'), {
455
engine: { ejs },
456
options: { async: true },
457
templates: 'templates'
458
})
459
460
fastify.get('/', async (_req, reply) => {
461
return reply.viewAsync('ejs-async.ejs')
462
})
463
464
await fastify.listen({ port: 0 })
465
466
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
467
468
const responseContent = await result.text()
469
470
t.assert.strictEqual(result.status, 200)
471
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
472
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
473
t.assert.strictEqual(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), responseContent)
474
475
await fastify.close()
476
})
477
478
test('reply.viewAsync with ejs engine, async: true (global option), and html-minifier-terser', async t => {
479
t.plan(4)
480
const fastify = Fastify()
481
const ejs = require('ejs')
482
483
fastify.register(require('../index'), {
484
engine: { ejs },
485
options: {
486
async: true,
487
useHtmlMinifier: minifier,
488
htmlMinifierOptions: minifierOpts
489
},
490
templates: 'templates'
491
})
492
493
fastify.get('/', (_req, reply) => {
494
return reply.viewAsync('ejs-async.ejs')
495
})
496
497
await fastify.listen({ port: 0 })
498
499
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
500
501
const responseContent = await result.text()
502
503
t.assert.strictEqual(result.status, 200)
504
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
505
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
506
t.assert.strictEqual(await minifier.minify(await ejs.render(fs.readFileSync('./templates/ejs-async.ejs', 'utf8'), {}, { async: true }), minifierOpts), responseContent)
507
508
await fastify.close()
509
})
510
511