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