Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/test/eta.test.js
107 views
1
'use strict'
2
3
const { test, beforeEach } = require('node:test')
4
const Fastify = require('fastify')
5
const fs = require('node:fs')
6
const path = require('node:path')
7
8
const pointOfView = require('../index')
9
const { Eta } = require('eta')
10
let eta = new Eta()
11
12
require('./helper').etaHtmlMinifierTests(true)
13
require('./helper').etaHtmlMinifierTests(false)
14
15
beforeEach(async () => {
16
// this is mandatory since some test call eta.configure(customOptions)
17
eta = new Eta()
18
})
19
20
test('reply.view with eta engine and custom templates folder', async t => {
21
t.plan(4)
22
const fastify = Fastify()
23
24
const data = { text: 'text' }
25
26
fastify.register(pointOfView, {
27
engine: {
28
eta
29
},
30
templates: 'templates'
31
})
32
33
fastify.get('/', (_req, reply) => {
34
reply.view('index.eta', data)
35
})
36
37
await fastify.listen({ port: 0 })
38
39
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
40
const responseContent = await result.text()
41
42
t.assert.strictEqual(result.status, 200)
43
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
44
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
45
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
46
47
await fastify.close()
48
})
49
50
test('reply.view with eta engine with layout option', async t => {
51
t.plan(4)
52
const fastify = Fastify()
53
54
const data = { text: 'text' }
55
56
fastify.register(pointOfView, {
57
engine: {
58
eta
59
},
60
root: path.join(__dirname, '../templates'),
61
layout: 'layout-eta.html'
62
})
63
64
fastify.get('/', (_req, reply) => {
65
reply.view('index-for-layout.eta', data)
66
})
67
68
await fastify.listen({ port: 0 })
69
70
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
71
const responseContent = await result.text()
72
73
t.assert.strictEqual(result.status, 200)
74
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
75
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
76
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
77
78
await fastify.close()
79
})
80
81
test('reply.view with eta engine with layout option on render', async t => {
82
t.plan(4)
83
const fastify = Fastify()
84
85
const data = { text: 'text' }
86
87
fastify.register(pointOfView, {
88
engine: {
89
eta
90
},
91
root: path.join(__dirname, '../templates')
92
})
93
94
fastify.get('/', (_req, reply) => {
95
reply.view('index-for-layout.eta', data, { layout: 'layout-eta.html' })
96
})
97
98
await fastify.listen({ port: 0 })
99
100
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
101
const responseContent = await result.text()
102
103
t.assert.strictEqual(result.status, 200)
104
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
105
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
106
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
107
108
await fastify.close()
109
})
110
111
test('reply.view should return 500 if layout is missing on render', async t => {
112
t.plan(1)
113
const fastify = Fastify()
114
115
const data = { text: 'text' }
116
117
fastify.register(pointOfView, {
118
engine: {
119
eta
120
},
121
root: path.join(__dirname, '../templates')
122
})
123
124
fastify.get('/', (_req, reply) => {
125
reply.view('index-for-layout.eta', data, { layout: 'non-existing-layout-eta.html' })
126
})
127
128
await fastify.listen({ port: 0 })
129
130
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
131
132
t.assert.strictEqual(result.status, 500)
133
134
await fastify.close()
135
})
136
137
test('reply.view with eta engine and custom ext', async t => {
138
t.plan(4)
139
const fastify = Fastify()
140
141
const data = { text: 'text' }
142
143
fastify.register(pointOfView, {
144
engine: {
145
eta
146
},
147
templates: 'templates',
148
viewExt: 'eta'
149
})
150
151
fastify.get('/', (_req, reply) => {
152
reply.view('index', data)
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(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
164
165
await fastify.close()
166
})
167
168
test('reply.view for eta without data-parameter but defaultContext', async t => {
169
t.plan(4)
170
const fastify = Fastify()
171
172
const data = { text: 'text' }
173
174
fastify.register(pointOfView, {
175
engine: {
176
eta
177
},
178
defaultContext: data,
179
templates: 'templates'
180
})
181
182
fastify.get('/', (_req, reply) => {
183
reply.view('index.eta')
184
})
185
186
await fastify.listen({ port: 0 })
187
188
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
189
const responseContent = await result.text()
190
191
t.assert.strictEqual(result.status, 200)
192
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
193
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
194
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
195
196
await fastify.close()
197
})
198
199
test('reply.view for eta without data-parameter but defaultContext', async t => {
200
t.plan(4)
201
const fastify = Fastify()
202
203
const data = { text: 'text' }
204
205
fastify.register(pointOfView, {
206
engine: {
207
eta
208
},
209
defaultContext: data,
210
templates: 'templates'
211
})
212
213
fastify.get('/', (_req, reply) => {
214
reply.view('index.eta')
215
})
216
217
await fastify.listen({ port: 0 })
218
219
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
220
const responseContent = await result.text()
221
222
t.assert.strictEqual(result.status, 200)
223
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
224
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
225
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
226
227
await fastify.close()
228
})
229
230
test('reply.view for eta without data-parameter and without defaultContext', async t => {
231
t.plan(4)
232
const fastify = Fastify()
233
234
fastify.register(pointOfView, {
235
engine: {
236
eta
237
},
238
templates: 'templates'
239
})
240
241
fastify.get('/', (_req, reply) => {
242
reply.view('index-bare.html')
243
})
244
245
await fastify.listen({ port: 0 })
246
247
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
248
249
const responseContent = await result.text()
250
251
t.assert.strictEqual(result.status, 200)
252
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
253
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
254
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index-bare.html', 'utf8')), responseContent)
255
256
await fastify.close()
257
})
258
259
test('reply.view for eta engine without data-parameter and defaultContext but with reply.locals', async t => {
260
t.plan(4)
261
const fastify = Fastify()
262
263
const localsData = { text: 'text from locals' }
264
265
fastify.register(pointOfView, {
266
engine: {
267
eta
268
}
269
})
270
271
fastify.addHook('preHandler', function (_request, reply, done) {
272
reply.locals = localsData
273
done()
274
})
275
276
fastify.get('/', (_req, reply) => {
277
reply.view('./templates/index-bare.html')
278
})
279
280
await fastify.listen({ port: 0 })
281
282
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
283
284
const responseContent = await result.text()
285
286
t.assert.strictEqual(result.status, 200)
287
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
288
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
289
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index-bare.html', 'utf8'), localsData), responseContent)
290
291
await fastify.close()
292
})
293
294
test('reply.view for eta engine without defaultContext but with reply.locals and data-parameter', async t => {
295
t.plan(4)
296
const fastify = Fastify()
297
298
const localsData = { text: 'text from locals' }
299
const data = { text: 'text' }
300
301
fastify.register(pointOfView, {
302
engine: {
303
eta
304
}
305
})
306
307
fastify.addHook('preHandler', function (_request, reply, done) {
308
reply.locals = localsData
309
done()
310
})
311
312
fastify.get('/', (_req, reply) => {
313
reply.view('./templates/index-bare.html', data)
314
})
315
316
await fastify.listen({ port: 0 })
317
318
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
319
320
const responseContent = await result.text()
321
322
t.assert.strictEqual(result.status, 200)
323
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
324
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
325
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index-bare.html', 'utf8'), data), responseContent)
326
327
await fastify.close()
328
})
329
330
test('reply.view for eta engine without data-parameter but with reply.locals and defaultContext', async t => {
331
t.plan(4)
332
const fastify = Fastify()
333
334
const localsData = { text: 'text from locals' }
335
const contextData = { text: 'text from context' }
336
337
fastify.register(pointOfView, {
338
engine: {
339
eta
340
},
341
defaultContext: contextData
342
})
343
344
fastify.addHook('preHandler', function (_request, reply, done) {
345
reply.locals = localsData
346
done()
347
})
348
349
fastify.get('/', (_req, reply) => {
350
reply.view('./templates/index-bare.html')
351
})
352
353
await fastify.listen({ port: 0 })
354
355
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
356
357
const responseContent = await result.text()
358
359
t.assert.strictEqual(result.status, 200)
360
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
361
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
362
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index-bare.html', 'utf8'), localsData), responseContent)
363
364
await fastify.close()
365
})
366
367
test('reply.view for eta engine with data-parameter and reply.locals and defaultContext', async t => {
368
t.plan(4)
369
const fastify = Fastify()
370
371
const localsData = { text: 'text from locals' }
372
const contextData = { text: 'text from context' }
373
const data = { text: 'text' }
374
375
fastify.register(pointOfView, {
376
engine: {
377
eta
378
},
379
defaultContext: contextData
380
})
381
382
fastify.addHook('preHandler', function (_request, reply, done) {
383
reply.locals = localsData
384
done()
385
})
386
387
fastify.get('/', (_req, reply) => {
388
reply.view('./templates/index-bare.html', data)
389
})
390
391
await fastify.listen({ port: 0 })
392
393
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
394
395
const responseContent = await result.text()
396
397
t.assert.strictEqual(result.status, 200)
398
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
399
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
400
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index-bare.html', 'utf8'), data), responseContent)
401
402
await fastify.close()
403
})
404
405
test('reply.view with eta engine and full path templates folder', async t => {
406
t.plan(4)
407
const fastify = Fastify()
408
409
const data = { text: 'text' }
410
411
fastify.register(pointOfView, {
412
engine: {
413
eta
414
},
415
templates: path.join(__dirname, '../templates')
416
})
417
418
fastify.get('/', (_req, reply) => {
419
reply.view('index.eta', data)
420
})
421
422
await fastify.listen({ port: 0 })
423
424
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
425
426
const responseContent = await result.text()
427
428
t.assert.strictEqual(result.status, 200)
429
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
430
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
431
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
432
433
await fastify.close()
434
})
435
436
test('reply.view with eta engine', async t => {
437
t.plan(4)
438
const fastify = Fastify()
439
440
const data = { text: 'text' }
441
442
fastify.register(pointOfView, {
443
engine: {
444
eta
445
}
446
})
447
448
fastify.get('/', (_req, reply) => {
449
reply.view('templates/index.eta', data)
450
})
451
452
await fastify.listen({ port: 0 })
453
454
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
455
456
const responseContent = await result.text()
457
458
t.assert.strictEqual(result.status, 200)
459
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
460
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
461
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
462
463
await fastify.close()
464
})
465
466
test('reply.view with eta engine and defaultContext', async t => {
467
t.plan(4)
468
const fastify = Fastify()
469
470
const data = { text: 'text' }
471
472
fastify.register(pointOfView, {
473
engine: {
474
eta
475
},
476
defaultContext: data
477
})
478
479
fastify.get('/', (_req, reply) => {
480
reply.view('templates/index.eta', {})
481
})
482
483
await fastify.listen({ port: 0 })
484
485
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
486
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(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
493
494
await fastify.close()
495
})
496
497
test('reply.view with eta engine and includeViewExtension property as true', async t => {
498
t.plan(4)
499
const fastify = Fastify()
500
501
const data = { text: 'text' }
502
503
fastify.register(pointOfView, {
504
engine: {
505
eta
506
},
507
includeViewExtension: true
508
})
509
510
fastify.get('/', (_req, reply) => {
511
reply.view('templates/index', data)
512
})
513
514
await fastify.listen({ port: 0 })
515
516
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
517
518
const responseContent = await result.text()
519
520
t.assert.strictEqual(result.status, 200)
521
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
522
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
523
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
524
525
await fastify.close()
526
})
527
528
test('reply.view with eta engine, template folder specified, include files (eta and html) used in template, includeViewExtension property as true', async t => {
529
t.plan(5)
530
const fastify = Fastify()
531
532
const templatesFolder = path.join(__dirname, '../templates')
533
const options = {
534
views: templatesFolder // must be put to make tests (with include files) working ...
535
}
536
const data = { text: 'text' }
537
538
fastify.register(pointOfView, {
539
engine: {
540
eta
541
},
542
includeViewExtension: true,
543
templates: templatesFolder,
544
options
545
})
546
547
fastify.get('/', (_req, reply) => {
548
reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type
549
})
550
551
await fastify.listen({ port: 0 })
552
553
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
554
555
const responseContent = await result.text()
556
557
t.assert.strictEqual(result.status, 200)
558
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
559
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
560
561
const content = eta.render('/index-linking-other-pages.eta', data, options)
562
t.assert.strictEqual(content.length, responseContent.length)
563
t.assert.strictEqual(content, responseContent)
564
565
await fastify.close()
566
})
567
568
test('reply.view with eta engine, templates with folder specified, include files and attributes; home', async t => {
569
t.plan(4)
570
const fastify = Fastify()
571
572
const templatesFolder = path.join(__dirname, '../templates')
573
const options = {
574
views: templatesFolder
575
}
576
const data = { text: 'Hello from eta Templates' }
577
578
fastify.register(pointOfView, {
579
engine: {
580
eta
581
},
582
includeViewExtension: true,
583
templates: templatesFolder,
584
options
585
})
586
587
fastify.get('/', (_req, reply) => {
588
reply.type('text/html; charset=utf-8').view('index', data)
589
})
590
591
await fastify.listen({ port: 0 })
592
593
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
594
595
const responseContent = await result.text()
596
597
t.assert.strictEqual(result.status, 200)
598
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
599
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
600
601
const content = eta.render('/index.eta', data, options)
602
t.assert.strictEqual(content.length, responseContent.length)
603
604
await fastify.close()
605
})
606
607
test('reply.view with eta engine, templates with folder specified, include files and attributes; page with no data', async t => {
608
t.plan(4)
609
const fastify = Fastify()
610
611
const templatesFolder = path.join(__dirname, '../templates')
612
const options = {
613
views: templatesFolder
614
}
615
616
fastify.register(pointOfView, {
617
engine: {
618
eta
619
},
620
includeViewExtension: true,
621
templates: templatesFolder,
622
options
623
})
624
625
fastify.get('/no-data-test', (_req, reply) => {
626
reply.type('text/html; charset=utf-8').view('index-with-no-data')
627
})
628
629
await fastify.listen({ port: 0 })
630
631
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/no-data-test')
632
633
const responseContent = await result.text()
634
635
t.assert.strictEqual(result.status, 200)
636
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
637
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
638
639
const content = eta.render('/index-with-no-data.eta', null, options)
640
t.assert.strictEqual(content.length, responseContent.length)
641
642
await fastify.close()
643
})
644
645
test('reply.view with eta engine, templates with folder specified, include files and attributes; page with includes', async t => {
646
t.plan(4)
647
const fastify = Fastify()
648
649
const templatesFolder = path.join(__dirname, '../templates')
650
const options = {
651
views: templatesFolder
652
}
653
654
const data = { text: 'Hello from eta Templates' }
655
656
fastify.register(pointOfView, {
657
engine: {
658
eta
659
},
660
includeViewExtension: true,
661
templates: templatesFolder,
662
options
663
})
664
665
fastify.get('/include-test', (_req, reply) => {
666
reply.type('text/html; charset=utf-8').view('index-with-includes', data)
667
})
668
669
await fastify.listen({ port: 0 })
670
671
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/include-test')
672
673
const responseContent = await result.text()
674
675
t.assert.strictEqual(result.status, 200)
676
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
677
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
678
679
const content = eta.render('/index-with-includes.eta', data, options)
680
t.assert.strictEqual(content.length, responseContent.length)
681
682
await fastify.close()
683
})
684
685
test('reply.view with eta engine, templates with folder specified, include files and attributes; page with one include missing', async t => {
686
t.plan(4)
687
const fastify = Fastify()
688
689
const templatesFolder = path.join(__dirname, '../templates')
690
const options = {
691
views: templatesFolder
692
}
693
const data = { text: 'Hello from eta Templates' }
694
695
fastify.register(pointOfView, {
696
engine: {
697
eta
698
},
699
includeViewExtension: true,
700
templates: templatesFolder,
701
options
702
})
703
704
fastify.get('/include-one-include-missing-test', (_req, reply) => {
705
reply.type('text/html; charset=utf-8').view('index-with-includes-one-missing', data)
706
})
707
708
await fastify.listen({ port: 0 })
709
710
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/include-one-include-missing-test')
711
712
const responseContent = await result.text()
713
714
t.assert.strictEqual(result.status, 500)
715
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
716
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
717
718
await t.assert.rejects(eta.renderAsync('/index-with-includes-one-missing.eta', data, options))
719
720
await fastify.close()
721
})
722
723
test('fastify.view with eta engine and callback in production mode', (t, end) => {
724
t.plan(6)
725
const fastify = Fastify()
726
727
const data = { text: 'text' }
728
729
fastify.register(pointOfView, {
730
engine: {
731
eta
732
},
733
production: true
734
})
735
736
fastify.ready(err => {
737
t.assert.ifError(err)
738
739
fastify.view('templates/index.eta', data, (err, compiled) => {
740
t.assert.ifError(err)
741
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), compiled)
742
743
fastify.ready(err => {
744
t.assert.ifError(err)
745
746
fastify.view('templates/index.eta', data, (err, compiled) => {
747
t.assert.ifError(err)
748
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), compiled)
749
fastify.close()
750
end()
751
})
752
})
753
})
754
})
755
})
756
757
test('fastify.view with eta engine in production mode should use cache', async t => {
758
t.plan(1)
759
760
const fastify = Fastify()
761
const cache = {
762
cache: {},
763
get (k) {
764
if (this.cache[k] !== undefined) {
765
t.assert.ok(true)
766
}
767
return this.cache[k]
768
},
769
define (k, v) {
770
this.cache[k] = v
771
}
772
}
773
774
fastify.register(pointOfView, {
775
production: true,
776
engine: {
777
eta
778
},
779
options: {
780
templatesSync: cache
781
}
782
})
783
784
await fastify.ready()
785
786
await fastify.view('templates/index.eta', { text: 'test' })
787
await fastify.view('templates/index.eta', { text: 'test' }) // This should trigger the cache
788
await fastify.close()
789
})
790
791
test('fastify.view with eta engine and custom cache', async t => {
792
t.plan(6)
793
const fastify = Fastify()
794
795
const tplPath = 'templates/index.eta'
796
const tplAbsPath = path.resolve(tplPath)
797
const data = { text: 'text' }
798
799
// Custom cache
800
const pseudoCache = {
801
cache: {},
802
get: function (k) {
803
t.assert.ok('the cache is set')
804
return this.cache[k]
805
},
806
define: function (k, v) {
807
this.cache[k] = v
808
}
809
}
810
811
const etaOptions = {
812
cache: true,
813
templatesSync: pseudoCache,
814
views: path.join(__dirname, '../templates')
815
}
816
817
eta.configure(etaOptions)
818
819
fastify.register(pointOfView, {
820
engine: {
821
eta
822
},
823
options: etaOptions
824
})
825
826
// pre-cache
827
eta.loadTemplate(path.join(__dirname, tplPath), eta.readFile(tplPath))
828
const tplF = eta.templatesSync.get(path.join(__dirname, tplPath))
829
830
fastify.get('/', (_req, reply) => {
831
try {
832
const res = reply.view(tplPath, data)
833
t.assert.strictEqual(eta.templatesSync, pseudoCache,
834
'Cache instance should be equal to the pre-defined one')
835
t.assert.notStrictEqual(eta.templatesSync.get(tplAbsPath), undefined,
836
'Template should be pre-cached')
837
return res
838
} catch (e) {
839
t.assert.ifError(e)
840
}
841
})
842
843
await fastify.listen()
844
845
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
846
847
const responseContent = await result.text()
848
849
t.assert.strictEqual(result.status, 200, 'Response should be 200')
850
851
const str = eta.render(tplF, data)
852
t.assert.strictEqual(str, responseContent, 'Route should return the same result as cached template function')
853
854
await fastify.close()
855
})
856
857
test('fastify.view with eta engine, should throw page missing', (t, end) => {
858
t.plan(3)
859
const fastify = Fastify()
860
861
fastify.register(require('../index'), {
862
engine: {
863
eta
864
}
865
})
866
867
fastify.ready(err => {
868
t.assert.ifError(err)
869
870
fastify.view(null, {}, err => {
871
t.assert.ok(err instanceof Error)
872
t.assert.strictEqual(err.message, 'Missing page')
873
fastify.close()
874
end()
875
})
876
})
877
})
878
879
test('fastify.view with eta engine and async in production mode', (t, end) => {
880
t.plan(3)
881
const fastify = Fastify()
882
883
const data = { text: 'text' }
884
885
fastify.register(pointOfView, {
886
engine: {
887
eta
888
},
889
production: true,
890
options: {
891
async: true
892
}
893
})
894
895
fastify.ready(err => {
896
t.assert.ifError(err)
897
898
fastify.view('templates/index.eta', data).then((compiled) => {
899
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), compiled)
900
fastify.view('templates/index.eta', null)
901
.then(() => { t.fail('should not be here') })
902
.catch((err) => {
903
t.assert.ok(err instanceof Error)
904
fastify.close()
905
end()
906
})
907
})
908
})
909
})
910
911
test('reply.view with eta engine and raw template', async t => {
912
t.plan(4)
913
const fastify = Fastify()
914
915
const data = { text: 'text' }
916
917
fastify.register(pointOfView, {
918
engine: {
919
eta
920
}
921
})
922
923
fastify.get('/', (_req, reply) => {
924
reply.view({ raw: fs.readFileSync('./templates/index.eta', 'utf8') }, data)
925
})
926
927
await fastify.listen({ port: 0 })
928
929
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
930
931
const responseContent = await result.text()
932
933
t.assert.strictEqual(result.status, 200)
934
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
935
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
936
937
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
938
939
await fastify.close()
940
})
941
942
test('reply.view with eta engine and function template', async t => {
943
t.plan(4)
944
const fastify = Fastify()
945
946
const data = { text: 'text' }
947
948
fastify.register(pointOfView, {
949
engine: {
950
eta
951
},
952
templates: 'templates'
953
})
954
955
fastify.get('/', (_req, reply) => {
956
reply.view(eta.compile(fs.readFileSync('./templates/index.eta', 'utf8')), data)
957
})
958
959
await fastify.listen({ port: 0 })
960
961
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
962
963
const responseContent = await result.text()
964
965
t.assert.strictEqual(result.status, 200)
966
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
967
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
968
969
t.assert.strictEqual(eta.renderString(fs.readFileSync('./templates/index.eta', 'utf8'), data), responseContent)
970
971
await fastify.close()
972
})
973
974
test('reply.view should return 500 if function return sync error', async t => {
975
t.plan(1)
976
const fastify = Fastify()
977
978
const data = { text: 'text' }
979
980
fastify.register(pointOfView, {
981
engine: {
982
eta
983
},
984
root: path.join(__dirname, '../templates')
985
})
986
987
fastify.get('/', (_req, reply) => {
988
reply.view(() => { throw new Error('kaboom') }, data)
989
})
990
991
await fastify.listen({ port: 0 })
992
993
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
994
995
t.assert.strictEqual(result.status, 500)
996
997
await fastify.close()
998
})
999
1000
test('reply.view should return 500 if function return async error', async t => {
1001
t.plan(1)
1002
const fastify = Fastify()
1003
1004
const data = { text: 'text' }
1005
1006
fastify.register(pointOfView, {
1007
engine: {
1008
eta
1009
},
1010
root: path.join(__dirname, '../templates')
1011
})
1012
1013
fastify.get('/', (_req, reply) => {
1014
reply.view(() => Promise.reject(new Error('kaboom')), data)
1015
})
1016
1017
await fastify.listen({ port: 0 })
1018
1019
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
1020
1021
t.assert.strictEqual(result.status, 500)
1022
1023
await fastify.close()
1024
})
1025
1026