Skip to content

Commit ca98245

Browse files
ThatTobMatekamilogorek
authored andcommitted
test: Add tests for parseRequest user and request objects
1 parent 07cf478 commit ca98245

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

packages/node/test/handlers.test.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { parseRequest } from '../src/handlers';
2+
import { Event } from '../src';
3+
4+
describe('parseRequest', () => {
5+
const mockReq = {
6+
method: 'GET',
7+
url: '/some/path?key=value',
8+
headers: {
9+
host: 'mattrobenolt.com',
10+
},
11+
cookies: { test: 'test' },
12+
body: '',
13+
user: {
14+
id: 123,
15+
username: 'tobias',
16+
email: 'tobias@mail.com',
17+
custom_property: 'foo',
18+
},
19+
};
20+
21+
describe('parseRequest.user properties', () => {
22+
const DEFAULT_USER_KEYS = ['id', 'username', 'email'];
23+
const CUSTOM_USER_KEYS = ['custom_property'];
24+
25+
test('parseRequest.user only contains the default properties from the user', done => {
26+
const fakeEvent: Event = {};
27+
const parsedRequest: Event = parseRequest(fakeEvent, mockReq);
28+
const userKeys = Object.keys(parsedRequest.user);
29+
30+
expect(userKeys).toEqual(DEFAULT_USER_KEYS);
31+
expect(userKeys).not.toEqual(expect.arrayContaining(CUSTOM_USER_KEYS));
32+
done();
33+
});
34+
35+
test('parseRequest.user only contains the custom properties specified in the options.user array', done => {
36+
const options = {
37+
user: CUSTOM_USER_KEYS,
38+
};
39+
const fakeEvent: Event = {};
40+
const parsedRequest: Event = parseRequest(fakeEvent, mockReq, options);
41+
const userKeys = Object.keys(parsedRequest.user);
42+
43+
expect(userKeys).toEqual(CUSTOM_USER_KEYS);
44+
expect(userKeys).not.toEqual(expect.arrayContaining(DEFAULT_USER_KEYS));
45+
done();
46+
});
47+
});
48+
49+
describe('parseRequest.request properties', () => {
50+
test('parseRequest.request only contains the default set of properties from the request', done => {
51+
const DEFAULT_REQUEST_PROPERTIES = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];
52+
const fakeEvent: Event = {};
53+
const parsedRequest: Event = parseRequest(fakeEvent, mockReq, {});
54+
expect(Object.keys(parsedRequest.request)).toEqual(DEFAULT_REQUEST_PROPERTIES);
55+
done();
56+
});
57+
58+
test('parseRequest.request only contains the specified properties in the options.request array', done => {
59+
const EXCLUDED_PROPERTIES = ['cookies', 'method'];
60+
const INCLUDED_PROPERTIES = ['data', 'headers', 'query_string', 'url'];
61+
const options = {
62+
request: INCLUDED_PROPERTIES,
63+
};
64+
const fakeEvent: Event = {};
65+
const parsedRequest: Event = parseRequest(fakeEvent, mockReq, options);
66+
const requestKeys = Object.keys(parsedRequest.request);
67+
68+
expect(requestKeys).toEqual(INCLUDED_PROPERTIES);
69+
expect(requestKeys).not.toEqual(expect.arrayContaining(EXCLUDED_PROPERTIES));
70+
done();
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)