'use strict'; const Fs = require('fs'); const Path = require('path'); const Code = require('@hapi/code'); const Handlebars = require('handlebars'); const Hapi = require('@hapi/hapi'); const Lab = require('@hapi/lab'); const Mustache = require('mustache'); const Pug = require('pug'); const Vision = require('..'); const Manager = require('../lib/manager'); const Utils = require('../lib/utils'); const internals = {}; const { it, describe } = exports.lab = Lab.script(); const expect = Code.expect; describe('Manager', () => { it('renders handlebars template', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { html: { module: require('handlebars'), path: __dirname + '/templates/valid' } } }); server.route({ method: 'GET', path: '/handlebars', handler: { view: { template: 'test.html', context: { message: 'Hello World!' } } } }); const res = await server.inject('/handlebars'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(200); }); it('shallow copies global context', () => { const options = { engines: { html: { module: require('handlebars'), path: __dirname + '/templates/valid' } }, context: { a: 1 } }; const server = Hapi.server(); const manager = new Manager(server, options); expect(manager._context).to.equal(options.context); }); it('sets content type', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { html: { module: require('handlebars'), path: __dirname + '/templates/valid', contentType: 'something/else' } } }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'test', context: { message: 'Hello World!' } } } }); const res = await server.inject('/'); expect(res.headers['content-type']).to.equal('something/else'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(200); }); it('errors on invalid template path', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/invalid' }); // Rendering errors are not available to extensions. server.route({ method: 'GET', path: '/', handler: { view: { template: 'test', context: { message: 'Hello, World!' } } } }); const res = await server.inject('/'); expect(res.statusCode).to.equal(500); }); it('returns a compiled Handlebars template reply', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/valid' }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'test', context: { message: 'Hello, World!' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.result).to.have.string('Hello, World!'); expect(res.statusCode).to.equal(200); }); it('errors absolute path given and allowAbsolutePath is false (by default)', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/valid' }); // Compilation errors sould be available for extensions. let error = null; server.ext('onPreResponse', (request, h) => { const response = request.response; if (response.isBoom) { error = response; } return h.continue; }); server.route({ method: 'GET', path: '/', handler: { view: { template: __dirname + '/templates/valid/test', context: { message: 'Hello, World!' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(500); expect(error).to.be.an.error(); }); it('errors if path given includes ../ and allowInsecureAccess is false (by default)', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/valid' }); // Compilation errors sould be available for extensions. let error = null; server.ext('onPreResponse', (request, h) => { const response = request.response; if (response.isBoom) { error = response; } return h.continue; }); server.route({ method: 'GET', path: '/', handler: { view: { template: '../test', context: { message: 'Hello, World!' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(500); expect(error).to.be.an.error(); }); it('allows if path given includes ../ and allowInsecureAccess is true', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, allowInsecureAccess: true, path: __dirname + '/templates/valid/helpers' }); server.route({ method: 'GET', path: '/', handler: { view: { template: '../test', context: { message: 'Hello, World!' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.result).to.have.string('Hello, World!'); expect(res.statusCode).to.equal(200); }); it('errors if template does not exist', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/valid' }); // Compilation errors sould be available for extensions. let error = null; server.ext('onPreResponse', (request, h) => { const response = request.response; if (response.isBoom) { error = response; } return h.continue; }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'testNope', context: { message: 'Hello, World!' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(500); expect(error).to.be.an.error(); }); it('errors if template is a directory', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/invalid' }); // Compilation errors sould be available for extensions. let error = null; server.ext('onPreResponse', (request, h) => { const response = request.response; if (response.isBoom) { error = response; } return h.continue; }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'dir' } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(500); expect(error).to.be.an.error(); }); it('errors if engine.compile throws', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/valid' }); // Compilation errors sould be available for extensions. let error = null; server.ext('onPreResponse', (request, h) => { const response = request.response; if (response.isBoom) { error = response; } return h.continue; }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'badmustache', context: { message: 'Hello, World!' }, options: { path: __dirname + '/templates/valid/invalid' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(500); expect(error).to.be.an.error(); }); it('should not fail if rendered template returns undefined', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { html: { module: { compile: function (template, compileOptions) { return function (context, renderOptions) { return undefined; }; } }, path: __dirname + '/templates/valid' } } }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'test.html' } } }); const res = await server.inject('/'); expect(res.statusCode).to.be.within(200, 204); }); it('allows the context to be modified by extensions', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { html: require('handlebars') }, path: __dirname + '/templates/valid' }); server.ext('onPreResponse', (request, h) => { const response = request.response; response.source.context.message = 'goodbye'; return h.continue; }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'test.html', context: { message: 'hello' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.result).not.to.contain('hello'); expect(res.result).to.contain('goodbye'); expect(res.statusCode).to.equal(200); }); describe('with engine initialization', () => { it('modifies the engine options', async () => { let compileOptions; let runtimeOptions; const server = Hapi.server(); const manager = new Manager(server, { path: __dirname + '/templates', engines: { html: { compile: function (string, options1) { compileOptions = options1; return function (context, options2) { runtimeOptions = options2; return string; }; }, prepare: function (options, next) { options.compileOptions = { stage: 'compile' }; options.runtimeOptions = { stage: 'render' }; return next(); } } } }); await manager.render('valid/test'); expect(compileOptions).to.include({ stage: 'compile' }); expect(runtimeOptions).to.include({ stage: 'render' }); }); it('errors if initialization fails', async () => { const server = Hapi.server(); const manager = new Manager(server, { path: __dirname + '/templates', engines: { html: { compile: function (string, options1) { return function (context, options2) { return string; }; }, prepare: function (options, next) { return next(new Error('Initialization failed')); } } } }); await expect(manager.render('valid/test')).to.reject('Initialization failed'); }); it('errors if initialization throws', async () => { const server = Hapi.server(); const manager = new Manager(server, { path: __dirname + '/templates', engines: { html: { compile: function (string, options1) { return function (context, options2) { return string; }; }, prepare: function (options, next) { throw new Error('Initialization error'); } } } }); await expect(manager.render('valid/test')).to.reject('Initialization error'); }); it('throws on invalid options', () => { expect(() => { const server = Hapi.server(); new Manager(server, { path: __dirname + '/templates', engines: { html: { compile: function (string, options1) { return function (context, options2) { return string; }; }, prepare: function (options, next) { throw new Error('Initialization error'); } } }, badValue: 'badValue' }); }) .to.throw(/"badValue" is not allowed/); }); it('only initializes once before rendering', async () => { let initialized = 0; const server = Hapi.server(); const manager = new Manager(server, { path: __dirname + '/templates', engines: { html: { compile: function (string, options1) { return function (context, options2) { return string; }; }, prepare: function (options, next) { ++initialized; return next(); } } } }); expect(initialized).to.equal(0); const rendered1 = await manager.render('valid/test'); expect(rendered1).to.exist(); expect(initialized).to.equal(1); const rendered2 = await manager.render('valid/test'); expect(rendered2).to.exist(); expect(initialized).to.equal(1); }); it('initializes multiple engines independently', async () => { let htmlOptions; let jadeOptions; const server = Hapi.server(); const manager = new Manager(server, { path: __dirname + '/templates', engines: { html: { compile: function (string, options1) { htmlOptions = options1; return function (context, options2) { return string; }; }, prepare: function (options, next) { options.compileOptions = { engine: 'handlebars' }; return next(); } }, pug: { compile: function (string, options1) { jadeOptions = options1; return function (context, options2) { return string; }; }, prepare: function (options, next) { options.compileOptions = { engine: 'pug' }; return next(); } } } }); const rendered1 = await manager.render('valid/test.html'); expect(rendered1).to.exist(); expect(htmlOptions).to.include({ engine: 'handlebars' }); expect(jadeOptions).to.not.exist(); const rendered2 = await manager.render('valid/test.pug'); expect(rendered2).to.exist(); expect(jadeOptions).to.include({ engine: 'pug' }); }); }); it('should not error on layoutKeyword conflict', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates/valid' }); // Rendering errors are not available to extensions. server.route({ method: 'GET', path: '/', handler: { view: { template: 'test', context: { message: 'Hello, World!', content: 'fail' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(200); expect(res.payload).to.contain('Hello, World!'); }); describe('with layout', () => { it('returns response', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { 'html': require('handlebars') }, path: __dirname + '/templates', layout: true }); server.route({ method: 'GET', path: '/', handler: { view: { template: 'valid/test', context: { title: 'test', message: 'Hapi' } } } }); const res = await server.inject('/'); expect(res.result).to.exist(); expect(res.statusCode).to.equal(200); expect(res.result.replace(/\r/g, '')).to.equal('\n\n
\nThis is all UPPERCASE and this is how we like it!
\n'); }); it('loads ESM named export helpers and render them', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests path: __dirname + '/templates/valid', helpersPath: __dirname + '/templates/valid/helpers' }); const rendered = await tempView.render('testHelpersESMNamedExport', { something: 'uppercase' }); expect(rendered).to.equal('This is all UPPERCASE and this is how we like it!
\n'); }); it('loads helpers and render them', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests path: __dirname + '/templates/valid', helpersPath: __dirname + '/templates/valid/helpers' }); const rendered = await tempView.render('testHelpers', { something: 'uppercase' }); expect(rendered).to.equal('This is all UPPERCASE and this is how we like it!
'); }); it('loads helpers and render them when helpersPath ends with a slash', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests path: __dirname + '/templates/valid', helpersPath: __dirname + '/templates/valid/helpers/' }); const rendered = await tempView.render('testHelpers', { something: 'uppercase' }); expect(rendered).to.equal('This is all UPPERCASE and this is how we like it!
'); }); it('loads helpers using relative paths', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests relativeTo: './test/templates', path: './valid', helpersPath: './valid/helpers' }); const rendered = await tempView.render('testHelpers', { something: 'uppercase' }); expect(rendered).to.equal('This is all UPPERCASE and this is how we like it!
'); }); it('loads helpers from multiple paths without a base', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests path: './test/templates/valid', helpersPath: ['./test/templates/valid/helpers/tools', './test/templates/valid/helpers'] }); const rendered = await tempView.render('testHelpers', { something: 'uppercase' }); expect(rendered).to.equal('This is all UPPERCASE and this is how we like it!
'); }); it('loads helpers from multiple paths with a base', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests relativeTo: './test/templates', path: './valid', helpersPath: ['./valid/helpers/tools', './valid/helpers'] }); const rendered = await tempView.render('testHelpers', { something: 'uppercase' }); expect(rendered).to.equal('This is all UPPERCASE and this is how we like it!
'); }); it('loads helpers using relative paths (without dots)', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests relativeTo: 'test/templates', path: 'valid', helpersPath: 'valid/helpers' }); const rendered = await tempView.render('testHelpers', { something: 'uppercase' }); expect(rendered).to.equal('This is all UPPERCASE and this is how we like it!
'); }); it('registers helpers programmatically', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { module: Handlebars.create() }, txt: { module: Handlebars.create() } }, relativeTo: 'test/templates', path: 'valid' }); tempView.registerHelper('long', (string) => string + string.substr(-1).repeat(2)); tempView.registerHelper('uppercase', (string) => string.toUpperCase()); const rendered1 = await tempView.render('testHelpers.html', { something: 'uppercase' }); expect(rendered1).to.equal('This is all UPPERCASE and this is howww we like it!
'); const rendered2 = await tempView.render('testHelpers.txt', { something: 'uppercase' }); expect(rendered2).to.equal('This is all UPPERCASE and this is howww we like it!'); }); it('does not register helpers on engines that don\'t have helper support', async () => { const server = Hapi.server(); const tempView = new Manager(server, { engines: { html: { compile: function (template) { Mustache.parse(template); return function (context) { return Mustache.render(template, context); }; } } }, relativeTo: 'test/templates', path: 'valid' }); tempView.registerHelper('long', (string) => string + string.substr(-1).repeat(2)); tempView.registerHelper('uppercase', (string) => string.toUpperCase()); const rendered = await tempView.render('testHelpers', { something: 'uppercase' }); expect(rendered).to.equal('This is all and this is we like it!
'); }); it('logs an error when helpers fail to load', async () => { const server = Hapi.server(); const log = server.events.once('log'); new Manager(server, { engines: { html: { module: Handlebars.create() } }, relativeTo: 'test/templates', path: 'valid', helpersPath: 'invalid/helpers' }); const [event] = await log; expect(event.tags).to.equal(['vision', 'helper', 'load', 'error']); }); it('reuses cached compilation', async () => { let gen = 0; const server = Hapi.server(); const views = new Manager(server, { path: __dirname + '/templates', engines: { html: { compileMode: 'async', module: { compile: function (string, options, callback) { ++gen; const compiled = Handlebars.compile(string, options); const renderer = function (context, opt, next) { return next(null, compiled(context, opt)); }; return callback(null, renderer); } } } }, isCached: true // isCached defaults to `true` }); const original = await views.render('valid/test', { title: 'test', message: 'Hapi' }); expect(original).to.exist(); expect(original).to.contain('Hapi'); const cached = await views.render('valid/test', { title: 'test', message: 'Hapi' }); expect(cached).to.exist(); expect(cached).to.contain('Hapi'); expect(gen).to.equal(1); }); it('disables caching', async () => { let gen = 0; const server = Hapi.server(); const views = new Manager(server, { path: __dirname + '/templates', engines: { html: { compileMode: 'async', module: { compile: function (string, options, callback) { ++gen; const compiled = Handlebars.compile(string, options); const renderer = function (context, opt, next) { return next(null, compiled(context, opt)); }; return callback(null, renderer); } } } }, isCached: false }); const original = await views.render('valid/test', { title: 'test', message: 'Hapi' }); expect(original).to.exist(); expect(original).to.contain('Hapi'); const notCached = await views.render('valid/test', { title: 'test', message: 'Joi' }); expect(notCached).to.exist(); expect(notCached).to.contain('Joi'); expect(gen).to.equal(2); }); it('recompiles partials/helpers when caching is disabled', async () => { const partialsPath = __dirname + '/templates/valid/partials'; const helpersPath = __dirname + '/templates/valid/helpers'; const originalPartialVal = ''; const originalNestedPartialVal = ''; const originalHelperVal = '\'use strict\';\n\nexports = module.exports = function (context) {\n\n return context.toUpperCase();\n};\n'; const mutateToUppercaseVal = 'i want to be uppercase'; const resetMutatedValues = () => { return new Promise((resolve, reject) => { Fs.writeFileSync(partialsPath + '/navToMutate.html', originalPartialVal); Fs.writeFileSync(partialsPath + '/nested/navToMutate.html', originalNestedPartialVal); Fs.writeFileSync(helpersPath + '/uppercaseToMutate.js', originalHelperVal); return resolve(); }); }; // Set everything correct to start in case a failed test leaves // these in a mutated state await resetMutatedValues(); const server = Hapi.server(); const views = new Manager(server, { path: __dirname + '/templates/valid', partialsPath, helpersPath, engines: { html: Handlebars }, isCached: false }); const originalPartialAndHelperValues = ' Nav:|\nThis is all I WANT TO BE UPPERCASE and this is how we like it!
'; const original = await views.render('testHelpersAndPartials', { mutateToUppercase: mutateToUppercaseVal }); expect(original).to.exist(); expect(original).to.equal(originalPartialAndHelperValues); // Mutate the partials here Fs.writeFileSync(partialsPath + '/navToMutate.html', ''); Fs.writeFileSync(partialsPath + '/nested/navToMutate.html', ''); // Mutate the helpers here Fs.writeFileSync(helpersPath + '/uppercaseToMutate.js', '\'use strict\';\n\nexports = module.exports = function (context) {\n\n return context.toUpperCase().split(\'\').sort().join(\'\').trim();\n};\n'); const mutatedPartialsAndHelper = await views.render('testHelpersAndPartials', { mutateToUppercase: mutateToUppercaseVal }); expect(mutatedPartialsAndHelper).to.exist(); expect(mutatedPartialsAndHelper).to.equal(' Nav:|\nThis is all AABCEEEINOPPRSTTUW and this is how we like it!
'); // Revert the mutations await resetMutatedValues(); // Verify they've reverted correctly const changedBack = await views.render('testHelpersAndPartials', { mutateToUppercase: mutateToUppercaseVal }); expect(changedBack).to.equal(originalPartialAndHelperValues); }); it('does not recompile partials/helpers when caching is enabled', async () => { const partialsPath = __dirname + '/templates/valid/partials'; const helpersPath = __dirname + '/templates/valid/helpers'; const originalPartialVal = ''; const originalNestedPartialVal = ''; const originalHelperVal = '\'use strict\';\n\nexports = module.exports = function (context) {\n\n return context.toUpperCase();\n};\n'; const mutateToUppercaseVal = 'i want to be uppercase'; const resetMutatedValues = () => { return new Promise((resolve, reject) => { Fs.writeFileSync(partialsPath + '/navToMutate.html', originalPartialVal); Fs.writeFileSync(partialsPath + '/nested/navToMutate.html', originalNestedPartialVal); Fs.writeFileSync(helpersPath + '/uppercaseToMutate.js', originalHelperVal); return resolve(); }); }; // Set everything correct to start in case a failed test leaves // these in a mutated state await resetMutatedValues(); const server = Hapi.server(); const views = new Manager(server, { path: __dirname + '/templates/valid', partialsPath, helpersPath, engines: { html: Handlebars }, isCached: true }); const originalPartialAndHelperValues = ' Nav:|\nThis is all I WANT TO BE UPPERCASE and this is how we like it!
'; const original = await views.render('testHelpersAndPartials', { mutateToUppercase: mutateToUppercaseVal }); expect(original).to.exist(); expect(original).to.equal(originalPartialAndHelperValues); // Mutate the partials here Fs.writeFileSync(partialsPath + '/navToMutate.html', ''); Fs.writeFileSync(partialsPath + '/nested/navToMutate.html', ''); // Mutate the helpers here Fs.writeFileSync(helpersPath + '/uppercaseToMutate.js', '\'use strict\';\n\nexports = module.exports = function (context) {\n\n return context.toUpperCase().split(\'\').sort().join(\'\').trim();\n};\n'); const mutatedPartialsAndHelper = await views.render('testHelpersAndPartials', { mutateToUppercase: mutateToUppercaseVal }); expect(mutatedPartialsAndHelper).to.exist(); expect(mutatedPartialsAndHelper).to.equal(originalPartialAndHelperValues); // Revert the mutations await resetMutatedValues(); // Verify they've reverted correctly const changedBack = await views.render('testHelpersAndPartials', { mutateToUppercase: mutateToUppercaseVal }); expect(changedBack).to.equal(originalPartialAndHelperValues); }); }); describe('clearCache()', () => { it('clears cache given a path', async () => { let gen = 0; let gen2 = 0; const server = Hapi.server(); const viewsManager = new Manager(server, { path: __dirname + '/templates', engines: { html: { compileMode: 'async', module: { compile: function (string, options, callback) { ++gen; const compiled = Handlebars.compile(string, options); const renderer = function (context, opt, next) { return next(null, compiled(context, opt)); }; return callback(null, renderer); } } }, pug: Pug }, isCached: true }); const viewsManager2 = new Manager(server, { path: __dirname + '/templates', engines: { html: { compileMode: 'async', module: { compile: function (string, options, callback) { ++gen2; const compiled = Handlebars.compile(string, options); const renderer = function (context, opt, next) { return next(null, compiled(context, opt)); }; return callback(null, renderer); } } } }, defaultExtension: 'html', isCached: true }); const original = await viewsManager.render('valid/test.html', { title: 'test', message: 'Hapi' }); expect(original).to.exist(); expect(original).to.contain('Hapi'); const cached = await viewsManager.render('valid/test.html', { title: 'test', message: 'Joi' }); expect(cached).to.exist(); expect(cached).to.contain('Joi'); expect(gen).to.equal(1); const engine = viewsManager.getEngine('html'); viewsManager.clearCache('valid/test', engine); const notCached = await viewsManager.render('valid/test.html', { title: 'test', message: 'Inert' }); expect(notCached).to.exist(); expect(notCached).to.contain('Inert'); expect(gen).to.equal(2); // Err checks expect(() => { viewsManager.clearCache(); }) .to.throw(/template is required/); expect(() => { viewsManager.clearCache('valid/test'); }) .to.throw(/Must pass the engine, have a single engine on the manager, have an extension on the template name, or set defaultExtension on manager options/); const original2 = await viewsManager2.render('valid/test', { title: 'test', message: 'Hapi' }); expect(original2).to.exist(); expect(original2).to.contain('Hapi'); const cached2 = await viewsManager2.render('valid/test', { title: 'test', message: 'Joi' }); expect(cached2).to.exist(); expect(cached2).to.contain('Joi'); expect(gen2).to.equal(1); viewsManager2.clearCache('valid/test'); const notCached2 = await viewsManager2.render('valid/test', { title: 'test', message: 'Inert' }); expect(notCached2).to.exist(); expect(notCached2).to.contain('Inert'); expect(gen2).to.equal(2); // Allow user-supplied suffix viewsManager2.clearCache('valid/test.html'); const notCached3 = await viewsManager2.render('valid/test', { title: 'test', message: 'Vision' }); expect(notCached3).to.exist(); expect(notCached3).to.contain('Vision'); expect(gen2).to.equal(3); // Warnings const log = server.events.once('log'); viewsManager2.clearCache('some/bogus/path'); const [event] = await log; expect(event.tags).to.equal(['vision', 'cache', 'clear', 'error']); }); }); describe('_response()', () => { it('sets Content-Type', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { html: require('handlebars') }, path: __dirname + '/templates/valid' }); const handler = function (request, h) { return h.view('test.html', { message: 'hi' }); }; server.route({ method: 'GET', path: '/', handler }); const res = await server.inject('/'); expect(res.headers['content-type']).to.contain('text/html'); }); it('does not override Content-Type', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { html: require('handlebars') }, path: __dirname + '/templates/valid' }); const handler = function (request, h) { return h.view('test.html', { message: 'hi' }).type('text/plain'); }; server.route({ method: 'GET', path: '/', handler }); const res = await server.inject('/'); expect(res.headers['content-type']).to.contain('text/plain'); }); it('errors on invalid template', async () => { const server = Hapi.server({ debug: false }); await server.register(Vision); server.views({ engines: { html: require('handlebars') }, path: __dirname + '/templates/invalid' }); const handler = function (request, h) { return h.view('test.html', { message: 'hi' }); }; server.route({ method: 'GET', path: '/', handler }); const res = await server.inject('/'); expect(res.statusCode).to.equal(500); }); it('passes the response object to the global context function', async () => { const server = Hapi.server(); await server.register(Vision); server.views({ engines: { html: Handlebars }, path: __dirname + '/templates/valid', context: function (request) { return { message: request ? request.route.path : 'default message', query: { test: 'global' } }; } }); const handler = function (request, h) { return h.view('testContext'); }; server.route({ method: 'GET', path: '/', handler }); const res = await server.inject('/'); expect(res.payload).to.contain('