Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional referrer #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
54 changes: 51 additions & 3 deletions test/test.response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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 mysterious hermit') {
withReferrer = true;
} else {
without = true;
}
});
await server.start();
server.events.emit('response', {
path: 'p1',
route: { settings: { plugins: {} } },
response: true,
headers: {},
info: {
referrer: 'a mysterious hermit'
},
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();
});