Skip to content

Commit

Permalink
Merge pull request #47 from firstandthird/passH
Browse files Browse the repository at this point in the history
pass h to preResponse
  • Loading branch information
orthagonal authored Nov 8, 2018
2 parents 2d73c27 + 55c7540 commit 2c14814
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ module.exports = function(viewConfig) {

result = await pprops(data);
if (viewConfig.preResponse) {
viewConfig.preResponse(request, viewConfig.options, result);
// if preresponse returns a value, use that as the response value:
const response = viewConfig.preResponse(request, viewConfig.options, result, h);
if (response) {
return response;
}
}
} catch (err) {
if (typeof viewConfig.onError === 'function') {
Expand Down
56 changes: 50 additions & 6 deletions test/test.views.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ lab.test('preprocess', async() => {
await server.stop();
});


lab.test('preprocess with redirect', async() => {
const server = new Hapi.Server({
debug: { log: 'hapi-views', request: '*' },
Expand Down Expand Up @@ -307,7 +306,8 @@ lab.test('preResponse', async() => {
return resolve({ test1: true });
});
});

let passedData;
let passedH;
await server.register([
require('vision'),
{
Expand All @@ -317,9 +317,13 @@ lab.test('preResponse', async() => {
'/yaml': {
view: 'yaml',
data: {
yaml1: "yaml()",
yaml1: 'yaml()',
},
preResponse: (request, options, h) => { processRan = true; }
preResponse: (request, options, data, h) => {
passedData = data;
passedH = h;
processRan = true;
}
}
}
}
Expand All @@ -331,6 +335,8 @@ lab.test('preResponse', async() => {
});
await server.start();
const response = await server.inject({ url: '/yaml' });
expect(passedData.yaml1.test1).to.equal(true);
expect(typeof passedH.response).to.equal('function');
expect(response.statusCode).to.equal(200);
expect(processRan).to.equal(true);
await server.stop();
Expand Down Expand Up @@ -434,7 +440,7 @@ lab.test('returns stale data on cache error', async () => {
// tests
const response = await server.inject({ url: '/yaml' });
expect(response.statusCode).to.equal(200);

const context = response.request.response.source.context;
expect(context).to.equal({ yaml1: { something: 'sure' } });

Expand All @@ -447,7 +453,7 @@ lab.test('returns stale data on cache error', async () => {
expect(context2).to.equal({ yaml1: { something: 'sure' } }); // stale

await wait(3);

const resp3 = await server.inject({ url: '/yaml' });
expect(resp3.statusCode).to.equal(200);

Expand All @@ -457,3 +463,41 @@ lab.test('returns stale data on cache error', async () => {

await server.stop();
});

lab.test('preResponse can take over a response ', async() => {
let processRan = false;
const server = new Hapi.Server({
debug: { log: 'hapi-views' },
port: 9991
});
server.method('yaml', (request, yamlFile) => {
return new Promise((resolve) => {
return resolve({ test1: true });
});
});
await server.register([
require('vision'),
{
plugin: require('../'),
options: {
routes: {
'/yaml': {
view: 'yaml',
data: {
yaml1: 'yaml()',
},
preResponse: (request, options, data, h) => h.response().redirect('/newLocation')
}
}
}
}
]);
server.views({
engines: { html: require('handlebars') },
path: `${__dirname}/views`
});
await server.start();
const response = await server.inject({ url: '/yaml' });
expect(response.statusCode).to.equal(302);
await server.stop();
});

0 comments on commit 2c14814

Please sign in to comment.