From 39cf2ec5b46d5c0b910c2f7f5be5f38815e68469 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Thu, 1 Mar 2018 18:37:55 -0600 Subject: [PATCH 1/2] optional referrer --- index.js | 4 +++- test/test.response.js | 54 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 04b87f2..b19520c 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,6 @@ const register = (server, options) => { const getLogData = (request, statusCode) => { const logData = { - referrer: request.info.referrer, browser: useragent.parse(request.headers['user-agent']).toString(), userAgent: request.headers['user-agent'], ip: request.info.remoteAddress, @@ -26,6 +25,9 @@ const register = (server, options) => { query: Object.assign({}, request.query), statusCode, }; + if (request.info.referrer) { + logData.referrer = request.info.referrer; + } if (options.includeId) { logData.id = request.info.id; } diff --git a/test/test.response.js b/test/test.response.js index 044ffb8..295d692 100644 --- a/test/test.response.js +++ b/test/test.response.js @@ -26,7 +26,7 @@ test('logs errors responses', async (t) => { }); server.events.on('log', async (event, tags) => { t.deepEqual(tags, { 'detailed-response': true, 'user-error': true }, 'returns the right tags'); - t.deepEqual(Object.keys(event.data), ['referrer', 'browser', 'userAgent', 'ip', 'method', 'path', 'query', 'statusCode', 'error'], 'includes data about the request'); + t.deepEqual(Object.keys(event.data), ['browser', 'userAgent', 'ip', 'method', 'path', 'query', 'statusCode', 'error'], 'includes data about the request'); t.equal(event.data.error.message, 'bad bad bad'); t.equal(event.data.error.stack.startsWith('Error: bad bad bad'), true); t.deepEqual(event.data.error.output, { @@ -400,7 +400,7 @@ test('options.requests logs a response for every request', async (t) => { }); server.events.on('log', async (event, tags) => { t.deepEqual(tags, { 'detailed-response': true }, 'returns the right tags'); - t.deepEqual(Object.keys(event.data), ['referrer', 'browser', 'userAgent', 'ip', 'method', 'path', 'query', 'statusCode'], 'includes data about the request'); + t.deepEqual(Object.keys(event.data), ['browser', 'userAgent', 'ip', 'method', 'path', 'query', 'statusCode'], 'includes data about the request'); await server.stop(); t.end(); }); @@ -433,7 +433,7 @@ test('options.includeId will also include the request id', async (t) => { }); server.events.on('log', async (event, tags) => { t.deepEqual(tags, { 'detailed-response': true }, 'returns the right tags'); - t.deepEqual(Object.keys(event.data), ['referrer', 'browser', 'userAgent', 'ip', 'method', 'path', 'query', 'statusCode', 'id'], 'includes data about the request'); + t.deepEqual(Object.keys(event.data), ['browser', 'userAgent', 'ip', 'method', 'path', 'query', 'statusCode', 'id'], 'includes data about the request'); await server.stop(); t.end(); }); @@ -513,3 +513,51 @@ test('options.requiredTags just logs everything if set to empty', async (t) => { await server.stop(); t.end(); }); + +test('referrer is included if available in request', async (t) => { + const server = new Hapi.Server({ + debug: { + request: ['error'] + }, + port: 8080 + }); + await server.register([ + { plugin: require('../'), + options: { + requests: true + } + } + ]); + let withReferrer = false; + let without = false; + server.events.on('log', (event, tags) => { + if (event.data.referrer === 'a skeezy old man') { + withReferrer = true; + } else { + without = true; + } + }); + await server.start(); + server.events.emit('response', { + path: 'p1', + route: { settings: { plugins: {} } }, + response: true, + headers: {}, + info: { + referrer: 'a skeezy old man' + }, + statusCode: 200 + }); + server.events.emit('response', { + path: 'p1', + route: { settings: { plugins: {} } }, + response: true, + headers: {}, + info: {}, + }); + await wait(2000); + t.ok(withReferrer); + t.ok(without); + await server.stop(); + t.end(); +}); From f91f579fccec2401676308e3aac95b6305178809 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Thu, 1 Mar 2018 18:43:11 -0600 Subject: [PATCH 2/2] test --- test/test.response.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.response.js b/test/test.response.js index 295d692..5584985 100644 --- a/test/test.response.js +++ b/test/test.response.js @@ -531,7 +531,7 @@ test('referrer is included if available in request', async (t) => { let withReferrer = false; let without = false; server.events.on('log', (event, tags) => { - if (event.data.referrer === 'a skeezy old man') { + if (event.data.referrer === 'a mysterious hermit') { withReferrer = true; } else { without = true; @@ -544,7 +544,7 @@ test('referrer is included if available in request', async (t) => { response: true, headers: {}, info: { - referrer: 'a skeezy old man' + referrer: 'a mysterious hermit' }, statusCode: 200 });