@@ -4,6 +4,7 @@ const { assert } = require('chai');
4
4
const {
5
5
removeTokensFromQuery,
6
6
removeTokensFromUrl,
7
+ pick,
7
8
getRequestDataToLog,
8
9
} = require ( '../lib/utils' ) ;
9
10
@@ -52,8 +53,44 @@ describe('utils', () => {
52
53
53
54
} ) ;
54
55
56
+ describe ( 'pick' , ( ) => {
57
+
58
+ it ( 'should only return object with specified keys' , ( ) => {
59
+ const obj = {
60
+ good : 'should remain' ,
61
+ bad : 'should be gone' ,
62
+ } ;
63
+ const picked = pick ( obj , [ 'good' ] ) ;
64
+ assert . deepEqual ( picked , { good : 'should remain' } ) ;
65
+ } ) ;
66
+
67
+ } ) ;
68
+
55
69
describe ( 'getRequestDataToLog' , ( ) => {
56
70
71
+ it ( 'should filter out headers' , ( ) => {
72
+ const req = {
73
+ url : '/path?key=val' ,
74
+ method : 'GET' ,
75
+ connection : {
76
+ remoteAddress : '::ffff:172.18.0.12' ,
77
+ remotePort : 40234 ,
78
+ } ,
79
+ headers : {
80
+ 'user-agent' : 'Mozilla/5.0' ,
81
+ referer : 'https://twitter.com' ,
82
+ 'cache-control' : 'no-cache' ,
83
+ 'x-something-bogus' : false ,
84
+ }
85
+ } ;
86
+ const data = getRequestDataToLog ( req ) ;
87
+ assert . deepEqual ( data . headers , {
88
+ 'user-agent' : 'Mozilla/5.0' ,
89
+ referer : 'https://twitter.com' ,
90
+ 'cache-control' : 'no-cache' ,
91
+ } ) ;
92
+ } ) ;
93
+
57
94
it ( 'should grab the data we want to track' , ( ) => {
58
95
const req = {
59
96
url : '/path?key=val' ,
@@ -75,19 +112,35 @@ describe('utils', () => {
75
112
assert . deepEqual ( data , {
76
113
url : '/path?key=val' ,
77
114
method : 'GET' ,
78
- params : {
79
- key : 'val' ,
80
- } ,
81
115
connection : {
82
116
remoteAddress : '::ffff:172.18.0.12' ,
83
117
remotePort : 40234 ,
84
118
} ,
85
119
headers : {
86
120
'user-agent' : 'Mozilla/5.0' ,
87
121
} ,
122
+ params : { } ,
88
123
} ) ;
89
124
} ) ;
90
125
126
+ it ( 'should filter out params that are not specified' , ( ) => {
127
+ const req = {
128
+ url : '/path?keep=1&remove=0' ,
129
+ method : 'GET' ,
130
+ query : {
131
+ keep : 1 ,
132
+ remove : 0 ,
133
+ } ,
134
+ connection : {
135
+ remoteAddress : '::ffff:172.18.0.12' ,
136
+ remotePort : 40234 ,
137
+ } ,
138
+ headers : { } ,
139
+ } ;
140
+ const data = getRequestDataToLog ( req , [ 'keep' ] ) ;
141
+ assert . deepEqual ( data . params , { keep : 1 } ) ;
142
+ } ) ;
143
+
91
144
it ( 'should handle requests with unparsed query objects' , ( ) => {
92
145
const req = {
93
146
url : '/path?key=val' ,
@@ -100,7 +153,7 @@ describe('utils', () => {
100
153
cookie : 'ok' ,
101
154
} ,
102
155
} ;
103
- const data = getRequestDataToLog ( req ) ;
156
+ const data = getRequestDataToLog ( req , [ 'key' ] ) ;
104
157
assert . equal ( data . params . key , 'val' ) ;
105
158
} ) ;
106
159
0 commit comments