Skip to content

Commit

Permalink
Merge pull request #34 from firstandthird/logFullPath
Browse files Browse the repository at this point in the history
log full path
  • Loading branch information
orthagonal authored Feb 28, 2018
2 parents 08f773f + f90cc04 commit b485e04
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ module.exports = async (server, pluginOptions) => {
// let the url parser format the correct redirect Location:
const fullRedirectLocation = redirectToUrl.format();

let from = request.path;
if (Object.keys(request.query).length !== 0) {
from = `${from}?${querystring.stringify(request.query)}`;
}
// log the route info:
server.log(['hapi-redirect', 'redirect', 'info'], {
remoteAddress: `${request.info.remoteAddress}:${request.info.remotePort}`,
Expand All @@ -108,7 +112,7 @@ module.exports = async (server, pluginOptions) => {
referrer: request.info.referrer,
routePath: route,
to: fullRedirectLocation,
from: request.path
from
});

// now emit the event and do the redirect:
Expand Down
30 changes: 29 additions & 1 deletion test/redirects.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const Code = require('code'); // assertion library
const Code = require('code'); // assertion library
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const Hapi = require('hapi');
Expand Down Expand Up @@ -534,4 +534,32 @@ lab.experiment('hapi-redirect', () => {
});
Code.expect(result.statusCode).to.equal(404);
});

lab.test(' logs output', async() => {
await server.register({
plugin: redirectModule,
options: {
redirects: {
'/': '/it/works?test=1',
},
}
});
await server.start();
let called = false;
server.events.on('log', (input, tags) => {
Code.expect(input.data.to).to.equal('/it/works?moniker=hugo&test=1');
Code.expect(input.data.from).to.equal('/?moniker=hugo');
Code.expect(input.data.referrer).to.equal('a wandering clown');
called = true;
});
await server.inject({
method: 'get',
headers: {
referrer: 'a wandering clown'
},
url: '/?moniker=hugo'
});
await new Promise(resolve => setTimeout(resolve, 500));
Code.expect(called).to.equal(true);
});
});

0 comments on commit b485e04

Please sign in to comment.