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