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