Skip to content
Merged
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
14 changes: 11 additions & 3 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ function extractTransaction(req: { [key: string]: any }, type: boolean | Transac
try {
// Express.js shape
const request = req as {
url: string;
originalUrl: string;
method: string;
route: {
path: string;
Expand All @@ -91,18 +93,24 @@ function extractTransaction(req: { [key: string]: any }, type: boolean | Transac
};
};

let routePath;
try {
routePath = url.parse(request.originalUrl || request.url).pathname;
} catch (_oO) {
routePath = request.route.path;
}

switch (type) {
case 'path': {
return request.route.path;
return routePath;
}
case 'handler': {
return request.route.stack[0].name;
}
case 'methodPath':
default: {
const method = request.method.toUpperCase();
const path = request.route.path;
return `${method}|${path}`;
return `${method}|${routePath}`;
}
}
} catch (_oO) {
Expand Down
73 changes: 58 additions & 15 deletions packages/node/test/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@ import { Event, Request, User } from '../src';
import { parseRequest } from '../src/handlers';

describe('parseRequest', () => {
const mockReq = {
body: 'foo',
cookies: { test: 'test' },
headers: {
host: 'mattrobenolt.com',
},
method: 'POST',
url: '/some/path?key=value',
user: {
custom_property: 'foo',
email: '[email protected]',
id: 123,
username: 'tobias',
},
};
let mockReq: { [key: string]: any };

beforeEach(() => {
mockReq = {
body: 'foo',
cookies: { test: 'test' },
headers: {
host: 'mattrobenolt.com',
},
method: 'POST',
originalUrl: '/some/originalUrl?key=value',
route: {
path: '/path',
stack: [
{
name: 'routeHandler',
},
],
},
url: '/some/url?key=value',
user: {
custom_property: 'foo',
email: '[email protected]',
id: 123,
username: 'tobias',
},
};
});

describe('parseRequest.contexts runtime', () => {
test('runtime name must contain node', () => {
Expand Down Expand Up @@ -121,4 +134,34 @@ describe('parseRequest', () => {
expect(parseRequest({}, { ...mockReq, method: 'HEAD' }, {}).request).not.toHaveProperty('data');
});
});

describe('parseRequest.transaction property', () => {
test('extracts method and full route path by default from `originalUrl`', () => {
const parsedRequest: Event = parseRequest({}, mockReq);
expect(parsedRequest.transaction).toEqual('POST|/some/originalUrl');
});

test('extracts method and full route path by default from `url` if `originalUrl` is not present', () => {
delete mockReq.originalUrl;
const parsedRequest: Event = parseRequest({}, mockReq);
expect(parsedRequest.transaction).toEqual('POST|/some/url');
});

test('fallback to method and `route.path` if previous attempts failed', () => {
delete mockReq.originalUrl;
delete mockReq.url;
const parsedRequest: Event = parseRequest({}, mockReq);
expect(parsedRequest.transaction).toEqual('POST|/path');
});

test('can extract path only instead if configured', () => {
const parsedRequest: Event = parseRequest({}, mockReq, { transaction: 'path' });
expect(parsedRequest.transaction).toEqual('/some/originalUrl');
});

test('can extract handler name instead if configured', () => {
const parsedRequest: Event = parseRequest({}, mockReq, { transaction: 'handler' });
expect(parsedRequest.transaction).toEqual('routeHandler');
});
});
});