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