Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/test/mustache.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
const minifierOpts = {
8
removeComments: true,
9
removeCommentsFromCDATA: true,
10
collapseWhitespace: true,
11
collapseBooleanAttributes: true,
12
removeAttributeQuotes: true,
13
removeEmptyAttributes: true
14
}
15
16
test('reply.view with mustache engine', async t => {
17
t.plan(4)
18
const fastify = Fastify()
19
const mustache = require('mustache')
20
const data = { text: 'text' }
21
22
fastify.register(require('../index'), {
23
engine: {
24
mustache
25
}
26
})
27
28
fastify.get('/', (_req, reply) => {
29
reply.view('./templates/index.html', data)
30
})
31
32
await fastify.listen({ port: 0 })
33
34
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
35
const responseContent = await result.text()
36
37
t.assert.strictEqual(result.status, 200)
38
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
39
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
40
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
41
42
await fastify.close()
43
})
44
45
test('reply.view for mustache without data-parameter but defaultContext', async t => {
46
t.plan(4)
47
const fastify = Fastify()
48
const mustache = require('mustache')
49
const data = { text: 'text' }
50
51
fastify.register(require('../index'), {
52
engine: {
53
mustache
54
},
55
defaultContext: data
56
})
57
58
fastify.get('/', (_req, reply) => {
59
reply.view('./templates/index.html')
60
})
61
62
await fastify.listen({ port: 0 })
63
64
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
65
const responseContent = await result.text()
66
67
t.assert.strictEqual(result.status, 200)
68
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
69
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
70
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
71
72
await fastify.close()
73
})
74
75
test('reply.view for mustache without data-parameter and without defaultContext', async t => {
76
t.plan(4)
77
const fastify = Fastify()
78
const mustache = require('mustache')
79
80
fastify.register(require('../index'), {
81
engine: {
82
mustache
83
}
84
})
85
86
fastify.get('/', (_req, reply) => {
87
// Reusing the ejs-template is possible because it contains no tags
88
reply.view('./templates/index-bare.html')
89
})
90
91
await fastify.listen({ port: 0 })
92
93
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
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(mustache.render(fs.readFileSync('./templates/index-bare.html', 'utf8')), responseContent)
100
101
await fastify.close()
102
})
103
104
test('reply.view with mustache engine and defaultContext', async t => {
105
t.plan(4)
106
const fastify = Fastify()
107
const mustache = require('mustache')
108
const data = { text: 'text' }
109
110
fastify.register(require('../index'), {
111
engine: {
112
mustache
113
},
114
defaultContext: data
115
})
116
117
fastify.get('/', (_req, reply) => {
118
reply.view('./templates/index.html', {})
119
})
120
121
await fastify.listen({ port: 0 })
122
123
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
124
const responseContent = await result.text()
125
126
t.assert.strictEqual(result.status, 200)
127
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
128
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
129
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
130
131
await fastify.close()
132
})
133
134
test('reply.view for mustache engine without data-parameter and defaultContext but with reply.locals', async t => {
135
t.plan(4)
136
const fastify = Fastify()
137
const mustache = require('mustache')
138
const localsData = { text: 'text from locals' }
139
140
fastify.register(require('../index'), {
141
engine: {
142
mustache
143
}
144
})
145
146
fastify.addHook('preHandler', function (_request, reply, done) {
147
reply.locals = localsData
148
done()
149
})
150
151
fastify.get('/', (_req, reply) => {
152
reply.view('./templates/index.html')
153
})
154
155
await fastify.listen({ port: 0 })
156
157
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
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(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), localsData), responseContent)
164
165
await fastify.close()
166
})
167
168
test('reply.view for mustache engine without defaultContext but with reply.locals and data-parameter', async t => {
169
t.plan(4)
170
const fastify = Fastify()
171
const mustache = require('mustache')
172
const localsData = { text: 'text from locals' }
173
const data = { text: 'text' }
174
175
fastify.register(require('../index'), {
176
engine: {
177
mustache
178
}
179
})
180
181
fastify.addHook('preHandler', function (_request, reply, done) {
182
reply.locals = localsData
183
done()
184
})
185
186
fastify.get('/', (_req, reply) => {
187
reply.view('./templates/index.html', data)
188
})
189
190
await fastify.listen({ port: 0 })
191
192
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
193
const responseContent = await result.text()
194
195
t.assert.strictEqual(result.status, 200)
196
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
197
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
198
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
199
200
await fastify.close()
201
})
202
203
test('reply.view for mustache engine without data-parameter but with reply.locals and defaultContext', async t => {
204
t.plan(4)
205
const fastify = Fastify()
206
const mustache = require('mustache')
207
const localsData = { text: 'text from locals' }
208
const contextData = { text: 'text from context' }
209
210
fastify.register(require('../index'), {
211
engine: {
212
mustache
213
},
214
defaultContext: contextData
215
})
216
217
fastify.addHook('preHandler', function (_request, reply, done) {
218
reply.locals = localsData
219
done()
220
})
221
222
fastify.get('/', (_req, reply) => {
223
reply.view('./templates/index.html')
224
})
225
226
await fastify.listen({ port: 0 })
227
228
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
229
const responseContent = await result.text()
230
231
t.assert.strictEqual(result.status, 200)
232
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
233
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
234
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), localsData), responseContent)
235
236
await fastify.close()
237
})
238
239
test('reply.view for mustache engine with data-parameter and reply.locals and defaultContext', async t => {
240
t.plan(4)
241
const fastify = Fastify()
242
const mustache = require('mustache')
243
const localsData = { text: 'text from locals' }
244
const contextData = { text: 'text from context' }
245
const data = { text: 'text' }
246
247
fastify.register(require('../index'), {
248
engine: {
249
mustache
250
},
251
defaultContext: contextData
252
})
253
254
fastify.addHook('preHandler', function (_request, reply, done) {
255
reply.locals = localsData
256
done()
257
})
258
259
fastify.get('/', (_req, reply) => {
260
reply.view('./templates/index.html', data)
261
})
262
263
await fastify.listen({ port: 0 })
264
265
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
266
const responseContent = await result.text()
267
268
t.assert.strictEqual(result.status, 200)
269
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
270
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
271
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
272
273
await fastify.close()
274
})
275
276
test('reply.view with mustache engine with global partials', async t => {
277
t.plan(4)
278
const fastify = Fastify()
279
const mustache = require('mustache')
280
const data = { text: 'text' }
281
282
fastify.register(require('../index'), {
283
engine: {
284
mustache
285
},
286
options: {
287
partials: {
288
body: './templates/body.mustache'
289
}
290
}
291
})
292
293
fastify.get('/', (_req, reply) => {
294
reply.view('./templates/index.mustache', data)
295
})
296
297
await fastify.listen({ port: 0 })
298
299
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
300
const responseContent = await result.text()
301
302
t.assert.strictEqual(result.status, 200)
303
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
304
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
305
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.mustache', 'utf8'), data, { body: '<p>{{ text }}</p>' }), responseContent)
306
307
await fastify.close()
308
})
309
310
test('reply.view with mustache engine with global and local partials', async t => {
311
t.plan(4)
312
const fastify = Fastify()
313
const mustache = require('mustache')
314
const data = {}
315
316
fastify.register(require('../index'), {
317
engine: {
318
mustache
319
},
320
options: {
321
partials: {
322
partial1: './templates/partial-1.mustache'
323
}
324
}
325
})
326
327
fastify.get('/', (_req, reply) => {
328
reply.view('./templates/index-with-2-partials.mustache', data, { partials: { partial2: './templates/partial-2.mustache' } })
329
})
330
331
await fastify.listen({ port: 0 })
332
333
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
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
t.assert.strictEqual(
340
mustache.render(
341
fs.readFileSync('./templates/index-with-2-partials.mustache', 'utf8'),
342
data,
343
{
344
partial1: 'Partial 1 - b4d932b9-4baa-4c99-8d14-d45411b9361e\n', // defined globally
345
partial2: 'Partial 2 - fdab0fe2-6dab-4429-ae9f-dfcb791d1d3d\n' // defined locally
346
}
347
),
348
responseContent
349
)
350
351
await fastify.close()
352
})
353
354
test('reply.view with mustache engine with partials', async t => {
355
t.plan(4)
356
const fastify = Fastify()
357
const mustache = require('mustache')
358
const data = { text: 'text' }
359
360
fastify.register(require('../index'), {
361
engine: {
362
mustache
363
}
364
})
365
366
fastify.get('/', (_req, reply) => {
367
reply.view('./templates/index.mustache', data, { partials: { body: './templates/body.mustache' } })
368
})
369
370
await fastify.listen({ port: 0 })
371
372
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
373
const responseContent = await result.text()
374
375
t.assert.strictEqual(result.status, 200)
376
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
377
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
378
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.mustache', 'utf8'), data, { body: '<p>{{ text }}</p>' }), responseContent)
379
380
await fastify.close()
381
})
382
383
test('reply.view with mustache engine with partials in production mode should use cache', async t => {
384
t.plan(4)
385
const fastify = Fastify()
386
const mustache = require('mustache')
387
const data = { text: 'text' }
388
const POV = require('..')
389
390
fastify.decorate(POV.fastifyViewCache, {
391
get: () => {
392
return '<div>Cached Response</div>'
393
},
394
set: () => { }
395
})
396
397
fastify.register(POV, {
398
engine: {
399
mustache
400
},
401
production: true
402
})
403
404
fastify.get('/', (_req, reply) => {
405
reply.view('./templates/index.mustache', data, { partials: { body: './templates/body.mustache' } })
406
})
407
408
await fastify.listen({ port: 0 })
409
410
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
411
const responseContent = await result.text()
412
413
t.assert.strictEqual(result.status, 200)
414
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
415
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
416
t.assert.strictEqual('<div>Cached Response</div>', responseContent)
417
418
await fastify.close()
419
})
420
421
test('reply.view with mustache engine with partials in production mode should cache partials correctly', async t => {
422
t.plan(8)
423
const fastify = Fastify()
424
const mustache = require('mustache')
425
const data = { text: 'text' }
426
const POV = require('..')
427
428
fastify.register(POV, {
429
engine: {
430
mustache
431
},
432
production: true
433
})
434
435
fastify.get('/one', (_req, reply) => {
436
reply.view('./templates/index.mustache', data, { partials: { body: './templates/partial-1.mustache' } })
437
})
438
fastify.get('/two', (_req, reply) => {
439
reply.view('./templates/index.mustache', data, { partials: { body: './templates/partial-2.mustache' } })
440
})
441
442
await fastify.listen({ port: 0 })
443
444
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/one')
445
const responseContent = await result.text()
446
447
t.assert.strictEqual(result.status, 200)
448
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
449
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
450
451
t.assert.match(responseContent, /Partial 1 - b4d932b9-4baa-4c99-8d14-d45411b9361e/g)
452
453
const result2 = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/two')
454
const responseContent2 = await result2.text()
455
456
t.assert.strictEqual(result2.status, 200)
457
t.assert.strictEqual(result2.headers.get('content-length'), '' + responseContent2.length)
458
t.assert.strictEqual(result2.headers.get('content-type'), 'text/html; charset=utf-8')
459
t.assert.match(responseContent2, /Partial 2 - fdab0fe2-6dab-4429-ae9f-dfcb791d1d3d/g)
460
461
await fastify.close()
462
})
463
464
test('reply.view with mustache engine with partials and html-minifier-terser', async t => {
465
t.plan(4)
466
const fastify = Fastify()
467
const mustache = require('mustache')
468
const data = { text: 'text' }
469
470
fastify.register(require('../index'), {
471
engine: {
472
mustache
473
},
474
options: {
475
useHtmlMinifier: minifier,
476
htmlMinifierOptions: minifierOpts
477
}
478
})
479
480
fastify.get('/', (_req, reply) => {
481
reply.view('./templates/index.mustache', data, { partials: { body: './templates/body.mustache' } })
482
})
483
484
await fastify.listen({ port: 0 })
485
486
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
487
const responseContent = await result.text()
488
489
t.assert.strictEqual(result.status, 200)
490
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
491
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
492
t.assert.strictEqual(await minifier.minify(mustache.render(fs.readFileSync('./templates/index.mustache', 'utf8'), data, { body: '<p>{{ text }}</p>' }), minifierOpts), responseContent)
493
494
await fastify.close()
495
})
496
497
test('reply.view with mustache engine with partials and paths excluded from html-minifier-terser', async t => {
498
t.plan(4)
499
const fastify = Fastify()
500
const mustache = require('mustache')
501
const data = { text: 'text' }
502
503
fastify.register(require('../index'), {
504
engine: {
505
mustache
506
},
507
options: {
508
useHtmlMinifier: minifier,
509
htmlMinifierOptions: minifierOpts,
510
pathsToExcludeHtmlMinifier: ['/test']
511
}
512
})
513
514
fastify.get('/test', (_req, reply) => {
515
reply.view('./templates/index.mustache', data, { partials: { body: './templates/body.mustache' } })
516
})
517
518
await fastify.listen({ port: 0 })
519
520
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/test')
521
const responseContent = await result.text()
522
523
t.assert.strictEqual(result.status, 200)
524
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
525
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
526
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.mustache', 'utf8'), data, { body: '<p>{{ text }}</p>' }), responseContent)
527
528
await fastify.close()
529
})
530
531
test('reply.view with mustache engine, template folder specified', async t => {
532
t.plan(4)
533
const fastify = Fastify()
534
const mustache = require('mustache')
535
const data = { text: 'text' }
536
const templatesFolder = 'templates'
537
538
fastify.register(require('../index'), {
539
engine: {
540
mustache
541
},
542
templates: templatesFolder
543
})
544
545
fastify.get('/', (_req, reply) => {
546
reply.view('index.html', data)
547
})
548
549
await fastify.listen({ port: 0 })
550
551
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
552
const responseContent = await result.text()
553
554
t.assert.strictEqual(result.status, 200)
555
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
556
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
557
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
558
559
await fastify.close()
560
})
561
562
test('reply.view with mustache engine, template folder specified with partials', async t => {
563
t.plan(4)
564
const fastify = Fastify()
565
const mustache = require('mustache')
566
const data = { text: 'text' }
567
const templatesFolder = 'templates'
568
569
fastify.register(require('../index'), {
570
engine: {
571
mustache
572
},
573
templates: templatesFolder
574
})
575
576
fastify.get('/', (_req, reply) => {
577
reply.view('index.mustache', data, { partials: { body: 'body.mustache' } })
578
})
579
580
await fastify.listen({ port: 0 })
581
582
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
583
const responseContent = await result.text()
584
585
t.assert.strictEqual(result.status, 200)
586
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
587
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
588
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.mustache', 'utf8'), data, { body: '<p>{{ text }}</p>' }), responseContent)
589
590
await fastify.close()
591
})
592
593
test('reply.view with mustache engine, missing template file', async t => {
594
t.plan(3)
595
const fastify = Fastify()
596
const mustache = require('mustache')
597
const data = { text: 'text' }
598
599
fastify.register(require('../index'), {
600
engine: {
601
mustache
602
}
603
})
604
605
fastify.get('/', (_req, reply) => {
606
reply.view('../templates/missing.html', data)
607
})
608
609
await fastify.listen({ port: 0 })
610
611
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
612
const responseContent = await result.text()
613
614
t.assert.strictEqual(result.status, 500)
615
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
616
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
617
618
await fastify.close()
619
})
620
621
test('reply.view with mustache engine, with partials missing template file', async t => {
622
t.plan(3)
623
const fastify = Fastify()
624
const mustache = require('mustache')
625
const data = { text: 'text' }
626
627
fastify.register(require('../index'), {
628
engine: {
629
mustache
630
}
631
})
632
633
fastify.get('/', (_req, reply) => {
634
reply.view('./templates/missing.mustache', data, { partials: { body: './templates/body.mustache' } })
635
})
636
637
await fastify.listen({ port: 0 })
638
639
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
640
const responseContent = await result.text()
641
642
t.assert.strictEqual(result.status, 500)
643
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
644
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
645
646
await fastify.close()
647
})
648
649
test('reply.view with mustache engine, with partials missing partials file', async t => {
650
t.plan(3)
651
const fastify = Fastify()
652
const mustache = require('mustache')
653
const data = { text: 'text' }
654
655
fastify.register(require('../index'), {
656
engine: {
657
mustache
658
}
659
})
660
661
fastify.get('/', (_req, reply) => {
662
reply.view('./templates/index.mustache', data, { partials: { body: './templates/missing.mustache' } })
663
})
664
665
await fastify.listen({ port: 0 })
666
667
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
668
const responseContent = await result.text()
669
670
t.assert.strictEqual(result.status, 500)
671
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
672
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
673
674
await fastify.close()
675
})
676
677
test('reply.view with mustache engine, with partials and multiple missing partials file', async t => {
678
t.plan(3)
679
const fastify = Fastify()
680
const mustache = require('mustache')
681
const data = { text: 'text' }
682
683
fastify.register(require('../index'), {
684
engine: {
685
mustache
686
}
687
})
688
689
fastify.get('/', (_req, reply) => {
690
reply.view('./templates/index.mustache', data, { partials: { body: './templates/missing.mustache', footer: './templates/alsomissing.mustache' } })
691
})
692
693
await fastify.listen({ port: 0 })
694
695
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
696
const responseContent = await result.text()
697
698
t.assert.strictEqual(result.status, 500)
699
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
700
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
701
702
await fastify.close()
703
})
704
705
test('fastify.view with mustache engine, should throw page missing', (t, end) => {
706
t.plan(3)
707
const fastify = Fastify()
708
const mustache = require('mustache')
709
710
fastify.register(require('../index'), {
711
engine: {
712
mustache
713
}
714
})
715
716
fastify.ready(err => {
717
t.assert.ifError(err)
718
719
fastify.view(null, {}, err => {
720
t.assert.ok(err instanceof Error)
721
t.assert.strictEqual(err.message, 'Missing page')
722
fastify.close()
723
end()
724
})
725
})
726
})
727
test('reply.view for mustache and raw template', async t => {
728
t.plan(4)
729
const fastify = Fastify()
730
const mustache = require('mustache')
731
const data = { text: 'text' }
732
733
fastify.register(require('../index'), {
734
engine: {
735
mustache
736
},
737
defaultContext: data
738
})
739
740
fastify.get('/', (_req, reply) => {
741
reply.view({ raw: fs.readFileSync('./templates/index.html', 'utf8') })
742
})
743
744
await fastify.listen({ port: 0 })
745
746
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
747
const responseContent = await result.text()
748
749
t.assert.strictEqual(result.status, 200)
750
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
751
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
752
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
753
754
await fastify.close()
755
})
756
757
test('reply.view for mustache and function template', async t => {
758
t.plan(4)
759
const fastify = Fastify()
760
const mustache = require('mustache')
761
const data = { text: 'text' }
762
763
fastify.register(require('../index'), {
764
engine: {
765
mustache
766
},
767
defaultContext: data
768
})
769
770
fastify.get('/', (_req, reply) => {
771
reply.header('Content-Type', 'text/html').view((mustache.render.bind(mustache, fs.readFileSync('./templates/index.html', 'utf8'))))
772
})
773
774
await fastify.listen({ port: 0 })
775
776
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
777
const responseContent = await result.text()
778
779
t.assert.strictEqual(result.status, 200)
780
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
781
t.assert.strictEqual(result.headers.get('content-type'), 'text/html')
782
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), responseContent)
783
784
await fastify.close()
785
})
786
787