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