Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/test/helper.js
162 views
1
'use strict'
2
3
const { test } = require('node:test')
4
const POV = require('..')
5
const Fastify = require('fastify')
6
const minifier = require('html-minifier-terser')
7
const fs = require('node:fs')
8
const dot = require('dot')
9
const handlebars = require('handlebars')
10
const { Liquid } = require('liquidjs')
11
const nunjucks = require('nunjucks')
12
const pug = require('pug')
13
const Twig = require('twig')
14
const Sqrl = require('squirrelly')
15
16
const data = { text: 'text' }
17
const minifierOpts = {
18
removeComments: true,
19
removeCommentsFromCDATA: true,
20
collapseWhitespace: true,
21
collapseBooleanAttributes: true,
22
removeAttributeQuotes: true,
23
removeEmptyAttributes: true
24
}
25
26
module.exports.dotHtmlMinifierTests = function (compileOptions, withMinifierOptions) {
27
const options = withMinifierOptions ? minifierOpts : {}
28
29
test('reply.view with dot engine and html-minifier-terser', async t => {
30
t.plan(4)
31
const fastify = Fastify()
32
dot.log = false
33
34
fastify.register(POV, {
35
engine: {
36
dot
37
},
38
root: 'templates',
39
options: {
40
useHtmlMinifier: minifier,
41
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
42
}
43
})
44
45
fastify.get('/', (_req, reply) => {
46
reply.view('testdot', data)
47
})
48
49
const address = await fastify.listen({ port: 0 })
50
51
const result = await fetch(address)
52
53
const responseContent = await result.text()
54
55
t.assert.strictEqual(result.status, 200)
56
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
57
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
58
t.assert.strictEqual(await minifier.minify(dot.process(compileOptions).testdot(data), options), responseContent)
59
60
await fastify.close()
61
})
62
test('reply.view with dot engine and paths excluded from html-minifier-terser', async t => {
63
t.plan(4)
64
const fastify = Fastify()
65
dot.log = false
66
67
fastify.register(POV, {
68
engine: {
69
dot
70
},
71
root: 'templates',
72
options: {
73
useHtmlMinifier: minifier,
74
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
75
pathsToExcludeHtmlMinifier: ['/test']
76
}
77
})
78
79
fastify.get('/test', (_req, reply) => {
80
reply.view('testdot', data)
81
})
82
83
const address = await fastify.listen({ port: 0 })
84
85
const result = await fetch(address + '/test')
86
87
const responseContent = await result.text()
88
89
t.assert.strictEqual(result.status, 200)
90
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
91
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
92
t.assert.strictEqual(dot.process(compileOptions).testdot(data), responseContent)
93
94
await fastify.close()
95
})
96
}
97
98
module.exports.etaHtmlMinifierTests = function (withMinifierOptions) {
99
const { Eta } = require('eta')
100
const eta = new Eta()
101
102
const options = withMinifierOptions ? minifierOpts : {}
103
104
test('reply.view with eta engine and html-minifier-terser', async t => {
105
t.plan(4)
106
const fastify = Fastify()
107
108
fastify.register(POV, {
109
engine: {
110
eta
111
},
112
options: {
113
useHtmlMinifier: minifier,
114
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
115
}
116
})
117
118
fastify.get('/', (_req, reply) => {
119
reply.view('templates/index.eta', data)
120
})
121
122
const address = await fastify.listen({ port: 0 })
123
124
const result = await fetch(address)
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(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), options), responseContent)
132
133
await fastify.close()
134
})
135
136
test('reply.view with eta engine and async and html-minifier-terser', async t => {
137
t.plan(4)
138
const fastify = Fastify()
139
140
fastify.register(POV, {
141
engine: {
142
eta
143
},
144
options: {
145
useHtmlMinifier: minifier,
146
async: true,
147
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
148
}
149
})
150
151
fastify.get('/', (_req, reply) => {
152
reply.view('templates/index.eta', data)
153
})
154
155
const address = await fastify.listen({ port: 0 })
156
157
const result = await fetch(address)
158
159
const responseContent = await result.text()
160
161
t.assert.strictEqual(result.status, 200)
162
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
163
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
164
t.assert.strictEqual(await minifier.minify(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), options), responseContent)
165
166
await fastify.close()
167
})
168
test('reply.view with eta engine and paths excluded from html-minifier-terser', async t => {
169
t.plan(4)
170
const fastify = Fastify()
171
172
fastify.register(POV, {
173
engine: {
174
eta
175
},
176
options: {
177
useHtmlMinifier: minifier,
178
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
179
pathsToExcludeHtmlMinifier: ['/test']
180
}
181
})
182
183
fastify.get('/test', (_req, reply) => {
184
reply.view('templates/index.eta', data)
185
})
186
187
const address = await fastify.listen({ port: 0 })
188
189
const result = await fetch(address + '/test')
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(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
197
198
await fastify.close()
199
})
200
}
201
202
module.exports.handleBarsHtmlMinifierTests = function (withMinifierOptions) {
203
const options = withMinifierOptions ? minifierOpts : {}
204
205
test('fastify.view with handlebars engine and html-minifier-terser', (t, end) => {
206
t.plan(2)
207
const fastify = Fastify()
208
209
fastify.register(POV, {
210
engine: {
211
handlebars
212
},
213
options: {
214
useHtmlMinifier: minifier,
215
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
216
partials: { body: './templates/body.hbs' }
217
}
218
})
219
220
fastify.ready(err => {
221
t.assert.ifError(err)
222
223
fastify.view('./templates/index.html', data).then(async compiled => {
224
t.assert.strictEqual(await minifier.minify(handlebars.compile(fs.readFileSync('./templates/index.html', 'utf8'))(data), options), compiled)
225
fastify.close()
226
end()
227
})
228
})
229
})
230
}
231
232
module.exports.liquidHtmlMinifierTests = function (withMinifierOptions) {
233
const options = withMinifierOptions ? minifierOpts : {}
234
235
test('reply.view with liquid engine and html-minifier-terser', async t => {
236
t.plan(4)
237
const fastify = Fastify()
238
const engine = new Liquid()
239
240
fastify.register(POV, {
241
engine: {
242
liquid: engine
243
},
244
options: {
245
useHtmlMinifier: minifier,
246
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
247
}
248
})
249
250
fastify.get('/', (_req, reply) => {
251
reply.view('./templates/index.liquid', data)
252
})
253
254
const address = await fastify.listen({ port: 0 })
255
256
const result = await fetch(address)
257
258
const responseContent = await result.text()
259
260
t.assert.strictEqual(result.status, 200)
261
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
262
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
263
264
const html = await engine.renderFile('./templates/index.liquid', data)
265
266
t.assert.strictEqual(await minifier.minify(html, options), responseContent)
267
268
await fastify.close()
269
})
270
test('reply.view with liquid engine and paths excluded from html-minifier-terser', async t => {
271
t.plan(4)
272
const fastify = Fastify()
273
const engine = new Liquid()
274
275
fastify.register(POV, {
276
engine: {
277
liquid: engine
278
},
279
options: {
280
useHtmlMinifier: minifier,
281
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
282
pathsToExcludeHtmlMinifier: ['/test']
283
}
284
})
285
286
fastify.get('/test', (_req, reply) => {
287
reply.view('./templates/index.liquid', data)
288
})
289
290
const address = await fastify.listen({ port: 0 })
291
292
const result = await fetch(address + '/test')
293
294
const responseContent = await result.text()
295
296
t.assert.strictEqual(result.status, 200)
297
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
298
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
299
300
const html = await engine.renderFile('./templates/index.liquid', data)
301
302
t.assert.strictEqual((await minifier.minify(html, options)).trim(), responseContent.trim())
303
304
await fastify.close()
305
})
306
}
307
308
module.exports.nunjucksHtmlMinifierTests = function (withMinifierOptions) {
309
const options = withMinifierOptions ? minifierOpts : {}
310
311
test('reply.view with nunjucks engine, full path templates folder, and html-minifier-terser', async t => {
312
t.plan(4)
313
const fastify = Fastify()
314
315
fastify.register(POV, {
316
engine: {
317
nunjucks
318
},
319
templates: 'templates',
320
options: {
321
useHtmlMinifier: minifier,
322
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
323
}
324
})
325
326
fastify.get('/', (_req, reply) => {
327
reply.view('./index.njk', data)
328
})
329
330
const address = await fastify.listen({ port: 0 })
331
332
const result = await fetch(address)
333
334
const responseContent = await result.text()
335
336
t.assert.strictEqual(result.status, 200)
337
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
338
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
339
340
t.assert.strictEqual(await minifier.minify(nunjucks.render('./index.njk', data), options), responseContent)
341
342
await fastify.close()
343
})
344
test('reply.view with nunjucks engine, full path templates folder, and paths excluded from html-minifier-terser', async t => {
345
t.plan(4)
346
const fastify = Fastify()
347
348
fastify.register(POV, {
349
engine: {
350
nunjucks
351
},
352
templates: 'templates',
353
options: {
354
useHtmlMinifier: minifier,
355
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
356
pathsToExcludeHtmlMinifier: ['/test']
357
}
358
})
359
360
fastify.get('/test', (_req, reply) => {
361
reply.view('./index.njk', data)
362
})
363
364
const address = await fastify.listen({ port: 0 })
365
366
const result = await fetch(address + '/test')
367
368
const responseContent = await result.text()
369
370
t.assert.strictEqual(result.status, 200)
371
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
372
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
373
374
t.assert.strictEqual(nunjucks.render('./index.njk', data), responseContent)
375
376
await fastify.close()
377
})
378
}
379
380
module.exports.pugHtmlMinifierTests = function (withMinifierOptions) {
381
const options = withMinifierOptions ? minifierOpts : {}
382
383
test('reply.view with pug engine and html-minifier-terser', async t => {
384
t.plan(4)
385
const fastify = Fastify()
386
387
fastify.register(POV, {
388
engine: {
389
pug
390
},
391
options: {
392
useHtmlMinifier: minifier,
393
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
394
}
395
})
396
397
fastify.get('/', (_req, reply) => {
398
reply.view('./templates/index.pug', data)
399
})
400
401
const address = await fastify.listen({ port: 0 })
402
403
const result = await fetch(address)
404
405
const responseContent = await result.text()
406
407
t.assert.strictEqual(result.status, 200)
408
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
409
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
410
411
t.assert.strictEqual(await minifier.minify(pug.render(fs.readFileSync('./templates/index.pug', 'utf8'), data), options), responseContent)
412
413
await fastify.close()
414
})
415
test('reply.view with pug engine and paths excluded from html-minifier-terser', async t => {
416
t.plan(4)
417
const fastify = Fastify()
418
419
fastify.register(POV, {
420
engine: {
421
pug
422
},
423
options: {
424
useHtmlMinifier: minifier,
425
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
426
pathsToExcludeHtmlMinifier: ['/test']
427
}
428
})
429
430
fastify.get('/test', (_req, reply) => {
431
reply.view('./templates/index.pug', data)
432
})
433
434
const address = await fastify.listen({ port: 0 })
435
436
const result = await fetch(address + '/test')
437
438
const responseContent = await result.text()
439
440
t.assert.strictEqual(result.status, 200)
441
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
442
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
443
444
t.assert.strictEqual(pug.render(fs.readFileSync('./templates/index.pug', 'utf8'), data), responseContent)
445
446
await fastify.close()
447
})
448
}
449
450
module.exports.twigHtmlMinifierTests = function (withMinifierOptions) {
451
const options = withMinifierOptions ? minifierOpts : {}
452
453
test('reply.view with twig engine and html-minifier-terser', async t => {
454
t.plan(5)
455
const fastify = Fastify()
456
457
fastify.register(POV, {
458
engine: {
459
twig: Twig
460
},
461
options: {
462
useHtmlMinifier: minifier,
463
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
464
}
465
})
466
467
fastify.get('/', (_req, reply) => {
468
reply.view('./templates/index.twig', data)
469
})
470
471
const address = await fastify.listen({ port: 0 })
472
473
const result = await fetch(address)
474
475
const responseContent = await result.text()
476
477
t.assert.strictEqual(result.status, 200)
478
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
479
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
480
481
await new Promise((resolve) => {
482
Twig.renderFile('./templates/index.twig', data, async (err, html) => {
483
t.assert.ifError(err)
484
t.assert.strictEqual(await minifier.minify(html, options), responseContent)
485
resolve()
486
})
487
})
488
489
await fastify.close()
490
})
491
test('reply.view with twig engine and paths excluded from html-minifier-terser', async t => {
492
t.plan(5)
493
const fastify = Fastify()
494
495
fastify.register(POV, {
496
engine: {
497
twig: Twig
498
},
499
options: {
500
useHtmlMinifier: minifier,
501
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
502
pathsToExcludeHtmlMinifier: ['/test']
503
}
504
})
505
506
fastify.get('/test', (_req, reply) => {
507
reply.view('./templates/index.twig', data)
508
})
509
510
const address = await fastify.listen({ port: 0 })
511
512
const result = await fetch(address + '/test')
513
514
const responseContent = await result.text()
515
516
t.assert.strictEqual(result.status, 200)
517
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
518
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
519
520
await new Promise((resolve) => {
521
Twig.renderFile('./templates/index.twig', data, async (err, html) => {
522
t.assert.ifError(err)
523
t.assert.strictEqual(html, responseContent)
524
resolve()
525
})
526
})
527
528
await fastify.close()
529
})
530
}
531
532
module.exports.squirrellyHtmlMinifierTests = function (withMinifierOptions) {
533
const options = withMinifierOptions ? minifierOpts : {}
534
535
test('reply.view with squirrelly engine and html-minifier-terser', async t => {
536
t.plan(4)
537
const fastify = Fastify()
538
539
fastify.register(POV, {
540
engine: {
541
squirrelly: Sqrl
542
},
543
options: {
544
useHtmlMinifier: minifier,
545
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts })
546
}
547
})
548
549
fastify.get('/', (_req, reply) => {
550
reply.view('templates/index.squirrelly', data)
551
})
552
553
const address = await fastify.listen({ port: 0 })
554
555
const result = await fetch(address)
556
557
const responseContent = await result.text()
558
559
t.assert.strictEqual(result.status, 200)
560
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
561
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
562
t.assert.strictEqual(await minifier.minify(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), options), responseContent)
563
564
await fastify.close()
565
})
566
test('reply.view with squirrelly engine and paths excluded from html-minifier-terser', async t => {
567
t.plan(4)
568
const fastify = Fastify()
569
570
fastify.register(POV, {
571
engine: {
572
squirrelly: Sqrl
573
},
574
options: {
575
useHtmlMinifier: minifier,
576
...(withMinifierOptions && { htmlMinifierOptions: minifierOpts }),
577
pathsToExcludeHtmlMinifier: ['/test']
578
}
579
})
580
581
fastify.get('/test', (_req, reply) => {
582
reply.view('templates/index.squirrelly', data)
583
})
584
585
const address = await fastify.listen({ port: 0 })
586
587
const result = await fetch(address + '/test')
588
589
const responseContent = await result.text()
590
591
t.assert.strictEqual(result.status, 200)
592
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
593
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
594
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
595
596
await fastify.close()
597
})
598
}
599
600