@@ -164,16 +164,43 @@ func TestParseResponse(t *testing.T) {
164164 assert .Equal (t , uint64 (20 ), message .Seq ())
165165}
166166
167+ func parseHTTPReqMessage (t * testing.T , raw string ) protocol.ParsedMessage {
168+ t .Helper ()
169+
170+ parser := protocol.HTTPStreamParser {}
171+ result := parser .ParseRequest (raw , protocol .Request , 10 , 20 )
172+ assert .Equal (t , protocol .Success , result .ParseState )
173+ assert .Len (t , result .ParsedMessages , 1 )
174+
175+ return result .ParsedMessages [0 ]
176+ }
177+
178+ func parseHTTPRespMessage (t * testing.T , raw string ) protocol.ParsedMessage {
179+ t .Helper ()
180+
181+ streamBuffer := buffer .New (1000 )
182+ streamBuffer .Add (10 , []byte (raw ), 10000 )
183+
184+ parser := protocol.HTTPStreamParser {}
185+ result := parser .ParseResponse (raw , protocol .Response , 10 , 20 , streamBuffer )
186+ assert .Equal (t , protocol .Success , result .ParseState )
187+ assert .Len (t , result .ParsedMessages , 1 )
188+
189+ return result .ParsedMessages [0 ]
190+ }
191+
167192func TestHttpFilter_Filter (t * testing.T ) {
168193 type fields struct {
169194 TargetPath string
170195 TargetPathReg * regexp.Regexp
171196 TargetPathPrefix string
172197 TargetHostName string
173198 TargetMethods []string
199+ TargetBodyReg * regexp.Regexp
174200 }
175201 type args struct {
176- parsedReq protocol.ParsedMessage
202+ parsedReq protocol.ParsedMessage
203+ parsedResp protocol.ParsedMessage
177204 }
178205 tests := []struct {
179206 name string
@@ -197,6 +224,7 @@ func TestHttpFilter_Filter(t *testing.T) {
197224 parsedReq : & protocol.ParsedHttpRequest {
198225 Path : "/foo/bar" ,
199226 },
227+ parsedResp : nil ,
200228 },
201229 want : true ,
202230 },
@@ -209,6 +237,7 @@ func TestHttpFilter_Filter(t *testing.T) {
209237 parsedReq : & protocol.ParsedHttpRequest {
210238 Path : "/foo/bar/baz" ,
211239 },
240+ parsedResp : nil ,
212241 },
213242 want : false ,
214243 },
@@ -221,6 +250,7 @@ func TestHttpFilter_Filter(t *testing.T) {
221250 parsedReq : & protocol.ParsedHttpRequest {
222251 Path : "/foo/bar/baz" ,
223252 },
253+ parsedResp : nil ,
224254 },
225255 want : true ,
226256 },
@@ -233,6 +263,7 @@ func TestHttpFilter_Filter(t *testing.T) {
233263 parsedReq : & protocol.ParsedHttpRequest {
234264 Path : "/test" ,
235265 },
266+ parsedResp : nil ,
236267 },
237268 want : false ,
238269 },
@@ -245,6 +276,7 @@ func TestHttpFilter_Filter(t *testing.T) {
245276 parsedReq : & protocol.ParsedHttpRequest {
246277 Path : "/foo/bar/100/baz" ,
247278 },
279+ parsedResp : nil ,
248280 },
249281 want : true ,
250282 },
@@ -257,6 +289,7 @@ func TestHttpFilter_Filter(t *testing.T) {
257289 parsedReq : & protocol.ParsedHttpRequest {
258290 Path : "/test" ,
259291 },
292+ parsedResp : nil ,
260293 },
261294 want : false ,
262295 },
@@ -269,6 +302,7 @@ func TestHttpFilter_Filter(t *testing.T) {
269302 Host : "test.com" ,
270303 Method : "POST" ,
271304 },
305+ parsedResp : nil ,
272306 },
273307 want : true ,
274308 },
@@ -281,6 +315,7 @@ func TestHttpFilter_Filter(t *testing.T) {
281315 parsedReq : & protocol.ParsedHttpRequest {
282316 Method : "GET" ,
283317 },
318+ parsedResp : nil ,
284319 },
285320 want : true ,
286321 },
@@ -293,6 +328,7 @@ func TestHttpFilter_Filter(t *testing.T) {
293328 parsedReq : & protocol.ParsedHttpRequest {
294329 Method : "POST" ,
295330 },
331+ parsedResp : nil ,
296332 },
297333 want : false ,
298334 },
@@ -305,6 +341,7 @@ func TestHttpFilter_Filter(t *testing.T) {
305341 parsedReq : & protocol.ParsedHttpRequest {
306342 Host : "foo.bar" ,
307343 },
344+ parsedResp : nil ,
308345 },
309346 want : true ,
310347 },
@@ -317,6 +354,52 @@ func TestHttpFilter_Filter(t *testing.T) {
317354 parsedReq : & protocol.ParsedHttpRequest {
318355 Host : "foo.baz" ,
319356 },
357+ parsedResp : nil ,
358+ },
359+ want : false ,
360+ },
361+ {
362+ name : "filter_by_request_body_regex" ,
363+ fields : fields {
364+ TargetBodyReg : regexp .MustCompile (`reqId=123` ),
365+ },
366+ args : args {
367+ parsedReq : parseHTTPReqMessage (t ,
368+ "POST /foo HTTP/1.1\r \n Host: foo.bar\r \n Content-Length: 21\r \n \r \n payload=reqId=123&x=1" ,
369+ ),
370+ parsedResp : parseHTTPRespMessage (t ,
371+ "HTTP/1.1 200 OK\r \n Content-Length: 2\r \n \r \n ok" ,
372+ ),
373+ },
374+ want : true ,
375+ },
376+ {
377+ name : "filter_by_response_body_regex" ,
378+ fields : fields {
379+ TargetBodyReg : regexp .MustCompile (`status=ok` ),
380+ },
381+ args : args {
382+ parsedReq : parseHTTPReqMessage (t ,
383+ "POST /foo HTTP/1.1\r \n Host: foo.bar\r \n Content-Length: 6\r \n \r \n hello!" ,
384+ ),
385+ parsedResp : parseHTTPRespMessage (t ,
386+ "HTTP/1.1 200 OK\r \n Content-Length: 17\r \n \r \n result=status=ok!" ,
387+ ),
388+ },
389+ want : true ,
390+ },
391+ {
392+ name : "not_filter_by_body_regex" ,
393+ fields : fields {
394+ TargetBodyReg : regexp .MustCompile (`reqId=123` ),
395+ },
396+ args : args {
397+ parsedReq : parseHTTPReqMessage (t ,
398+ "POST /foo HTTP/1.1\r \n Host: foo.bar\r \n Content-Length: 7\r \n \r \n nope123" ,
399+ ),
400+ parsedResp : parseHTTPRespMessage (t ,
401+ "HTTP/1.1 200 OK\r \n Content-Length: 7\r \n \r \n stillno" ,
402+ ),
320403 },
321404 want : false ,
322405 },
@@ -329,8 +412,18 @@ func TestHttpFilter_Filter(t *testing.T) {
329412 TargetPathPrefix : tt .fields .TargetPathPrefix ,
330413 TargetHostName : tt .fields .TargetHostName ,
331414 TargetMethods : tt .fields .TargetMethods ,
415+ TargetBodyReg : tt .fields .TargetBodyReg ,
332416 }
333- assert .Equalf (t , tt .want , filter .Filter (tt .args .parsedReq , nil ), "Filter(%v, %v)" , tt .args .parsedReq , nil )
417+ assert .Equalf (t , tt .want , filter .Filter (tt .args .parsedReq , tt . args . parsedResp ), "Filter(%v, %v)" , tt .args .parsedReq , tt . args . parsedResp )
334418 })
335419 }
336420}
421+
422+ func TestHttpFilter_BodyRegexRequiresResponseFiltering (t * testing.T ) {
423+ filter := protocol.HttpFilter {
424+ TargetBodyReg : regexp .MustCompile (`reqId=123` ),
425+ }
426+
427+ assert .True (t , filter .FilterByRequest ())
428+ assert .True (t , filter .FilterByResponse ())
429+ }
0 commit comments