Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/test/dot.test.js
107 views
1
'use strict'
2
3
const { test } = require('node:test')
4
const Fastify = require('fastify')
5
const { existsSync, rmdirSync, readFileSync } = require('node:fs')
6
const { join } = require('node:path')
7
const pino = require('pino')
8
const split = require('split2')
9
const compileOptions = {
10
path: 'templates',
11
destination: 'out',
12
log: false
13
}
14
15
require('./helper').dotHtmlMinifierTests(compileOptions, true)
16
require('./helper').dotHtmlMinifierTests(compileOptions, false)
17
18
test('reply.view with dot engine .dot file', async t => {
19
t.plan(4)
20
const fastify = Fastify()
21
const data = { text: 'text' }
22
const engine = require('dot')
23
engine.log = false
24
25
fastify.register(require('../index'), {
26
engine: {
27
dot: engine
28
},
29
root: 'templates'
30
})
31
32
fastify.get('/', (_req, reply) => {
33
reply.view('testdot', data)
34
})
35
36
await fastify.listen({ port: 0 })
37
38
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
39
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(responseContent, engine.process({ path: 'templates', destination: 'out' }).testdot(data))
46
47
await fastify.close()
48
})
49
50
test('reply.view with dot engine .dot file should create non-existent destination', async t => {
51
t.plan(1)
52
const fastify = Fastify()
53
const engine = require('dot')
54
engine.log = false
55
56
fastify.register(require('../index'), {
57
engine: {
58
dot: engine
59
},
60
options: {
61
destination: 'non-existent'
62
}
63
})
64
65
t.after(() => rmdirSync('non-existent'))
66
67
fastify.get('/', (_req, reply) => {
68
reply.view('testdot')
69
})
70
71
await fastify.listen({ port: 0 })
72
73
t.assert.ok(existsSync('non-existent'))
74
75
await fastify.close()
76
})
77
78
test('reply.view with dot engine .dot file should log WARN if template not found', async t => {
79
t.plan(1)
80
const splitStream = split(JSON.parse)
81
splitStream.on('data', (line) => {
82
t.assert.strictEqual(line.msg, `WARN: no template found in ${join(__dirname, '..')}`)
83
})
84
const logger = pino({ level: 'warn' }, splitStream)
85
const fastify = Fastify({
86
loggerInstance: logger
87
})
88
const engine = require('dot')
89
engine.log = false
90
91
t.after(() => rmdirSync('empty'))
92
93
fastify.register(require('../index'), {
94
engine: {
95
dot: engine
96
},
97
options: {
98
destination: 'empty'
99
}
100
})
101
102
await fastify.listen({ port: 0 })
103
await fastify.close()
104
})
105
106
test('reply.view with dot engine .jst file', async t => {
107
t.plan(4)
108
const fastify = Fastify()
109
const data = { text: 'text' }
110
const engine = require('dot')
111
engine.log = false
112
113
fastify.register(require('../index'), {
114
engine: {
115
dot: engine
116
},
117
root: 'templates'
118
})
119
120
fastify.get('/', (_req, reply) => {
121
reply.view('testjst', data)
122
})
123
124
await fastify.listen({ port: 0 })
125
126
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
127
128
const responseContent = await result.text()
129
130
t.assert.strictEqual(result.status, 200)
131
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
132
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
133
engine.process(compileOptions)
134
t.assert.strictEqual(responseContent, require('../out/testjst')(data))
135
136
await fastify.close()
137
})
138
139
test('reply.view with dot engine without data-parameter but defaultContext', async t => {
140
t.plan(4)
141
const fastify = Fastify()
142
const data = { text: 'text' }
143
144
const engine = require('dot')
145
engine.log = false
146
147
fastify.register(require('../index'), {
148
engine: {
149
dot: engine
150
},
151
defaultContext: data,
152
root: 'templates'
153
})
154
155
fastify.get('/', (_req, reply) => {
156
reply.view('testdot')
157
})
158
159
await fastify.listen({ port: 0 })
160
161
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
162
163
const responseContent = await result.text()
164
165
t.assert.strictEqual(result.status, 200)
166
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
167
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
168
t.assert.strictEqual(responseContent, engine.process(compileOptions).testdot(data))
169
170
await fastify.close()
171
})
172
173
test('reply.view with dot engine without data-parameter but without defaultContext', async t => {
174
t.plan(4)
175
const fastify = Fastify()
176
177
const engine = require('dot')
178
engine.log = false
179
180
fastify.register(require('../index'), {
181
engine: {
182
dot: engine
183
},
184
root: 'templates'
185
})
186
187
fastify.get('/', (_req, reply) => {
188
reply.view('testdot')
189
})
190
191
await fastify.listen({ port: 0 })
192
193
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
194
195
const responseContent = await result.text()
196
197
t.assert.strictEqual(result.status, 200)
198
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
199
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
200
engine.process(compileOptions)
201
t.assert.strictEqual(responseContent, engine.process(compileOptions).testdot())
202
203
await fastify.close()
204
})
205
206
test('reply.view with dot engine with data-parameter and defaultContext', async t => {
207
t.plan(4)
208
const fastify = Fastify()
209
const data = { text: 'text' }
210
211
const engine = require('dot')
212
engine.log = false
213
214
fastify.register(require('../index'), {
215
engine: {
216
dot: engine
217
},
218
defaultContext: data,
219
root: 'templates'
220
})
221
222
fastify.get('/', (_req, reply) => {
223
reply.view('testdot', {})
224
})
225
226
await fastify.listen({ port: 0 })
227
228
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
229
230
const responseContent = await result.text()
231
232
t.assert.strictEqual(result.status, 200)
233
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
234
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
235
t.assert.strictEqual(responseContent, engine.process(compileOptions).testdot(data))
236
237
await fastify.close()
238
})
239
240
test('reply.view for dot engine without data-parameter and defaultContext but with reply.locals', async t => {
241
t.plan(4)
242
const fastify = Fastify()
243
const localsData = { text: 'text from locals' }
244
245
const engine = require('dot')
246
engine.log = false
247
248
fastify.register(require('../index'), {
249
engine: {
250
dot: engine
251
},
252
root: 'templates'
253
})
254
255
fastify.addHook('preHandler', function (_request, reply, done) {
256
reply.locals = localsData
257
done()
258
})
259
260
fastify.get('/', (_req, reply) => {
261
reply.view('testdot', {})
262
})
263
264
await fastify.listen({ port: 0 })
265
266
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
267
268
const responseContent = await result.text()
269
270
t.assert.strictEqual(result.status, 200)
271
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
272
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
273
t.assert.strictEqual(responseContent, engine.process(compileOptions).testdot(localsData))
274
275
await fastify.close()
276
})
277
278
test('reply.view for dot engine without defaultContext but with reply.locals and data-parameter', async t => {
279
t.plan(4)
280
const fastify = Fastify()
281
const localsData = { text: 'text from locals' }
282
const data = { text: 'text' }
283
284
const engine = require('dot')
285
engine.log = false
286
287
fastify.register(require('../index'), {
288
engine: {
289
dot: engine
290
},
291
root: 'templates'
292
})
293
294
fastify.addHook('preHandler', function (_request, reply, done) {
295
reply.locals = localsData
296
done()
297
})
298
299
fastify.get('/', (_req, reply) => {
300
reply.view('testdot', data)
301
})
302
303
await fastify.listen({ port: 0 })
304
305
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
306
307
const responseContent = await result.text()
308
309
t.assert.strictEqual(result.status, 200)
310
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
311
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
312
t.assert.strictEqual(responseContent, engine.process(compileOptions).testdot(data))
313
314
await fastify.close()
315
})
316
317
test('reply.view for dot engine without data-parameter but with reply.locals and defaultContext', async t => {
318
t.plan(4)
319
const fastify = Fastify()
320
const localsData = { text: 'text from locals' }
321
const defaultContext = { text: 'text' }
322
323
const engine = require('dot')
324
engine.log = false
325
326
fastify.register(require('../index'), {
327
engine: {
328
dot: engine
329
},
330
defaultContext,
331
root: 'templates'
332
})
333
334
fastify.addHook('preHandler', function (_request, reply, done) {
335
reply.locals = localsData
336
done()
337
})
338
339
fastify.get('/', (_req, reply) => {
340
reply.view('testdot')
341
})
342
343
await fastify.listen({ port: 0 })
344
345
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
346
347
const responseContent = await result.text()
348
349
t.assert.strictEqual(result.status, 200)
350
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
351
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
352
t.assert.strictEqual(responseContent, engine.process(compileOptions).testdot(localsData))
353
354
await fastify.close()
355
})
356
357
test('reply.view for dot engine with data-parameter and reply.locals and defaultContext', async t => {
358
t.plan(4)
359
const fastify = Fastify()
360
const localsData = { text: 'text from locals' }
361
const defaultContext = { text: 'text from context' }
362
const data = { text: 'text' }
363
364
const engine = require('dot')
365
366
fastify.register(require('../index'), {
367
engine: {
368
dot: engine
369
},
370
defaultContext,
371
root: 'templates'
372
})
373
374
fastify.addHook('preHandler', function (_request, reply, done) {
375
reply.locals = localsData
376
done()
377
})
378
379
fastify.get('/', (_req, reply) => {
380
reply.view('testdot', data)
381
})
382
383
await fastify.listen({ port: 0 })
384
385
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
386
387
const responseContent = await result.text()
388
389
t.assert.strictEqual(result.status, 200)
390
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
391
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
392
t.assert.strictEqual(responseContent, engine.process(compileOptions).testdot(data))
393
394
await fastify.close()
395
})
396
397
test('fastify.view with dot engine, should throw page missing', (t, end) => {
398
t.plan(3)
399
const fastify = Fastify()
400
const engine = require('dot')
401
engine.log = false
402
403
fastify.register(require('../index'), {
404
engine: {
405
dot: engine
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 dot engine with layout option', async t => {
422
t.plan(4)
423
const fastify = Fastify()
424
const engine = require('dot')
425
const data = { text: 'text' }
426
427
fastify.register(require('../index'), {
428
engine: {
429
dot: engine
430
},
431
root: 'templates',
432
layout: 'layout'
433
})
434
435
fastify.get('/', (_req, reply) => {
436
reply.view('testdot', data)
437
})
438
439
await fastify.listen({ port: 0 })
440
441
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
442
443
const responseContent = await result.text()
444
445
t.assert.strictEqual(result.status, 200)
446
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
447
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
448
t.assert.strictEqual('header: textfoo text1 <p>foo</p>footer', responseContent)
449
450
await fastify.close()
451
})
452
453
test('reply.view with dot engine with layout option on render', async t => {
454
t.plan(4)
455
const fastify = Fastify()
456
const engine = require('dot')
457
const data = { text: 'text' }
458
459
fastify.register(require('../index'), {
460
engine: {
461
dot: engine
462
},
463
root: 'templates'
464
})
465
466
fastify.get('/', (_req, reply) => {
467
reply.view('testdot', data, { layout: 'layout' })
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
t.assert.strictEqual('header: textfoo text1 <p>foo</p>footer', responseContent)
480
481
await fastify.close()
482
})
483
484
test('reply.view with dot engine with layout option on render', async t => {
485
t.plan(4)
486
const fastify = Fastify()
487
const engine = require('dot')
488
const data = { text: 'text' }
489
490
fastify.register(require('../index'), {
491
engine: {
492
dot: engine
493
},
494
root: 'templates'
495
})
496
497
fastify.get('/', (_req, reply) => {
498
reply.view('testdot', data, { layout: 'layout' })
499
})
500
501
await fastify.listen({ port: 0 })
502
503
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
504
505
const responseContent = await result.text()
506
507
t.assert.strictEqual(result.status, 200)
508
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
509
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
510
t.assert.strictEqual('header: textfoo text1 <p>foo</p>footer', responseContent)
511
512
await fastify.close()
513
})
514
515
test('reply.view should return 500 if layout is missing on render', async t => {
516
t.plan(1)
517
const fastify = Fastify()
518
const engine = require('dot')
519
const data = { text: 'text' }
520
521
fastify.register(require('../index'), {
522
engine: {
523
dot: engine
524
},
525
root: 'templates'
526
})
527
528
fastify.get('/', (_req, reply) => {
529
reply.view('testdot', data, { layout: 'non-existing-layout' })
530
})
531
532
await fastify.listen({ port: 0 })
533
534
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
535
536
t.assert.strictEqual(result.status, 500)
537
538
await fastify.close()
539
})
540
541
test('reply.view with dot engine and raw template', async t => {
542
t.plan(4)
543
const fastify = Fastify()
544
const data = { text: 'text' }
545
const engine = require('dot')
546
engine.log = false
547
548
fastify.register(require('../index'), {
549
engine: {
550
dot: engine
551
}
552
})
553
554
fastify.get('/', (_req, reply) => {
555
reply.view({ raw: readFileSync('./templates/testdot.dot'), imports: { testdef: readFileSync('./templates/testdef.def') } }, data)
556
})
557
558
await fastify.listen({ port: 0 })
559
560
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
561
562
const responseContent = await result.text()
563
564
t.assert.strictEqual(result.status, 200)
565
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
566
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
567
t.assert.strictEqual(responseContent, engine.process({ path: 'templates', destination: 'out' }).testdot(data))
568
569
await fastify.close()
570
})
571
572
test('reply.view with dot engine and function template', async t => {
573
t.plan(4)
574
const fastify = Fastify()
575
const data = { text: 'text' }
576
const engine = require('dot')
577
engine.log = false
578
579
fastify.register(require('../index'), {
580
engine: {
581
dot: engine
582
}
583
})
584
585
fastify.get('/', (_req, reply) => {
586
reply.header('Content-Type', 'text/html').view(engine.process({ path: 'templates', destination: 'out' }).testdot, data)
587
})
588
589
await fastify.listen({ port: 0 })
590
591
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
592
593
const responseContent = await result.text()
594
595
t.assert.strictEqual(result.status, 200)
596
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
597
t.assert.strictEqual(result.headers.get('content-type'), 'text/html')
598
t.assert.strictEqual(responseContent, engine.process({ path: 'templates', destination: 'out' }).testdot(data))
599
600
await fastify.close()
601
})
602
603