/
handlers.test.ts
167 lines (147 loc) · 5.61 KB
/
handlers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Runtime } from '@sentry/types';
import { Event, Request, User } from '../src';
import { parseRequest } from '../src/handlers';
describe('parseRequest', () => {
let mockReq: { [key: string]: any };
beforeEach(() => {
mockReq = {
body: 'foo',
cookies: { test: 'test' },
headers: {
host: 'mattrobenolt.com',
},
method: 'POST',
url: '/some/url?key=value',
originalUrl: '/some/originalUrl?key=value',
user: {
custom_property: 'foo',
email: 'tobias@mail.com',
id: 123,
username: 'tobias',
},
route: {
path: '/path',
stack: [
{
name: 'routeHandler',
},
],
},
};
});
describe('parseRequest.contexts runtime', () => {
test('runtime name must contain node', () => {
const parsedRequest: Event = parseRequest({}, mockReq);
expect((parsedRequest.contexts!.runtime as Runtime).name).toEqual('node');
});
test('runtime version must contain current node version', () => {
const parsedRequest: Event = parseRequest({}, mockReq);
expect((parsedRequest.contexts!.runtime as Runtime).version).toEqual(process.version);
});
test('runtime disbaled by options', () => {
const parsedRequest: Event = parseRequest({}, mockReq, {
version: false,
});
expect(parsedRequest).not.toHaveProperty('contexts.runtime');
});
});
describe('parseRequest.user properties', () => {
const DEFAULT_USER_KEYS = ['id', 'username', 'email'];
const CUSTOM_USER_KEYS = ['custom_property'];
test('parseRequest.user only contains the default properties from the user', () => {
const parsedRequest: Event = parseRequest({}, mockReq);
expect(Object.keys(parsedRequest.user as User)).toEqual(DEFAULT_USER_KEYS);
});
test('parseRequest.user only contains the custom properties specified in the options.user array', () => {
const parsedRequest: Event = parseRequest({}, mockReq, {
user: CUSTOM_USER_KEYS,
});
expect(Object.keys(parsedRequest.user as User)).toEqual(CUSTOM_USER_KEYS);
});
test('parseRequest.user doesnt blow up when someone passes non-object value', () => {
const parsedRequest: Event = parseRequest(
{},
{
...mockReq,
// @ts-ignore
user: 'wat',
},
);
expect(Object.keys(parsedRequest.user as User)).toEqual([]);
});
});
describe('parseRequest.ip property', () => {
test('can be extracted from req.ip', () => {
const parsedRequest: Event = parseRequest(
{},
{
...mockReq,
ip: '123',
},
{
ip: true,
},
);
expect(parsedRequest.user!.ip_address).toEqual('123');
});
test('can extract from req.connection.remoteAddress', () => {
const parsedRequest: Event = parseRequest(
{},
{
...mockReq,
connection: {
remoteAddress: '321',
},
},
{
ip: true,
},
);
expect(parsedRequest.user!.ip_address).toEqual('321');
});
});
describe('parseRequest.request properties', () => {
test('parseRequest.request only contains the default set of properties from the request', () => {
const DEFAULT_REQUEST_PROPERTIES = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];
const parsedRequest: Event = parseRequest({}, mockReq);
expect(Object.keys(parsedRequest.request as Request)).toEqual(DEFAULT_REQUEST_PROPERTIES);
});
test('parseRequest.request only contains the specified properties in the options.request array', () => {
const INCLUDED_PROPERTIES = ['data', 'headers', 'query_string', 'url'];
const parsedRequest: Event = parseRequest({}, mockReq, {
request: INCLUDED_PROPERTIES,
});
expect(Object.keys(parsedRequest.request as Request)).toEqual(INCLUDED_PROPERTIES);
});
test('parseRequest.request skips `body` property for GET and HEAD requests', () => {
expect(parseRequest({}, mockReq, {}).request).toHaveProperty('data');
expect(parseRequest({}, { ...mockReq, method: 'GET' }, {}).request).not.toHaveProperty('data');
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');
});
});
});