1010using System . Linq ;
1111using System . Reflection ;
1212using System . Threading ;
13+ using System . Threading . Tasks ;
1314using Datadog . Trace . AppSec ;
15+ using Datadog . Trace . Configuration ;
1416using Datadog . Trace . Configuration . Telemetry ;
1517using Datadog . Trace . Security . Unit . Tests . Iast ;
1618using Datadog . Trace . Tagging ;
19+ using Datadog . Trace . TestHelpers . TestTracer ;
1720using FluentAssertions ;
1821using Moq ;
1922using Xunit ;
@@ -31,19 +34,19 @@ public class ApiSecurityTests
3134 [ InlineData ( true , true , true , SamplingPriorityValues . AutoKeep , false , null ) ]
3235 [ InlineData ( true , false , true , SamplingPriorityValues . AutoReject , true , "route2" ) ]
3336 [ InlineData ( true , false , false , SamplingPriorityValues . AutoKeep , false , "route3" ) ]
34- public void ApiSecurityTest ( bool enable , bool apmTracingEnabled , bool lastCall , int samplingPriority , bool expectedResult , string route )
37+ public async Task ApiSecurityTest ( bool enable , bool apmTracingEnabled , bool lastCall , int samplingPriority , bool expectedResult , string route )
3538 {
36- var apiSec = new ApiSecurity (
37- new SecuritySettings (
38- new CustomSettingsForTests (
39- new Dictionary < string , object >
40- {
41- { Configuration . ConfigurationKeys . AppSec . ApiSecurityEnabled , enable } ,
42- { Configuration . ConfigurationKeys . ApmTracingEnabled , apmTracingEnabled } ,
43- } ) ,
44- new NullConfigurationTelemetry ( ) ) ) ;
39+ var config = new Dictionary < string , object >
40+ {
41+ { Configuration . ConfigurationKeys . AppSec . ApiSecurityEnabled , enable } ,
42+ { Configuration . ConfigurationKeys . ApmTracingEnabled , apmTracingEnabled } ,
43+ } ;
44+
45+ await using var tracer = TracerHelper . Create ( TracerSettings . Create ( config ) ) ;
46+
47+ var apiSec = new ApiSecurity ( new SecuritySettings ( new CustomSettingsForTests ( config ) , new NullConfigurationTelemetry ( ) ) ) ;
4548 var dic = new Dictionary < string , object > ( ) ;
46- var tc = new TraceContext ( Mock . Of < IDatadogTracer > ( ) , new TraceTagCollection ( ) ) ;
49+ var tc = new TraceContext ( tracer , new TraceTagCollection ( ) ) ;
4750 tc . SetSamplingPriority ( samplingPriority ) ;
4851 var span = new Span ( new SpanContext ( SpanContext . None , tc , "Test" ) , DateTimeOffset . Now ) ;
4952 span . SetTag ( Tags . HttpRoute , route ) ;
@@ -66,10 +69,12 @@ public void ApiSecurityTest(bool enable, bool apmTracingEnabled, bool lastCall,
6669 }
6770
6871 [ Fact ]
69- public void ApiSecurityTestMaxRoutes ( )
72+ public async Task ApiSecurityTestMaxRoutes ( )
7073 {
7174 var maxRouteSize = 50 ;
72- var apiSec = new ApiSecurity ( new SecuritySettings ( new CustomSettingsForTests ( new Dictionary < string , object > { { Configuration . ConfigurationKeys . AppSec . ApiSecurityEnabled , true } } ) , new NullConfigurationTelemetry ( ) ) , maxRouteSize ) ;
75+ var config = new Dictionary < string , object > { { Configuration . ConfigurationKeys . AppSec . ApiSecurityEnabled , true } } ;
76+ await using var tracer = TracerHelper . Create ( TracerSettings . Create ( config ) ) ;
77+ var apiSec = new ApiSecurity ( new SecuritySettings ( new CustomSettingsForTests ( config ) , new NullConfigurationTelemetry ( ) ) , maxRouteSize ) ;
7378 var queue = new Queue < int > ( maxRouteSize ) ;
7479 for ( var i = 0 ; i < maxRouteSize + 1 ; i ++ )
7580 {
@@ -85,7 +90,7 @@ public void ApiSecurityTestMaxRoutes()
8590 var resHash = ApiSecurity . CombineHashes ( route , method , statusCode ) ;
8691 queue . Enqueue ( resHash ) ;
8792 var dic = new Dictionary < string , object > ( ) ;
88- var tc = new TraceContext ( Mock . Of < IDatadogTracer > ( ) , new TraceTagCollection ( ) ) ;
93+ var tc = new TraceContext ( tracer , new TraceTagCollection ( ) ) ;
8994 tc . SetSamplingPriority ( SamplingPriorityValues . AutoKeep ) ;
9095
9196 var span = new Span ( new SpanContext ( SpanContext . None , tc , "Test" ) , dt ) ;
@@ -104,15 +109,13 @@ public void ApiSecurityTestMaxRoutes()
104109 }
105110
106111 [ Fact ]
107- public void ApiSecurityTestMultiThread ( )
112+ public async Task ApiSecurityTestMultiThread ( )
108113 {
109- var apiSec = new ApiSecurity (
110- new SecuritySettings (
111- new CustomSettingsForTests (
112- new Dictionary < string , object > { { Configuration . ConfigurationKeys . AppSec . ApiSecurityEnabled , true } , { Configuration . ConfigurationKeys . AppSec . ApiSecuritySampleDelay , 120 } } ) ,
113- new NullConfigurationTelemetry ( ) ) ) ;
114+ var config = new Dictionary < string , object > { { Configuration . ConfigurationKeys . AppSec . ApiSecurityEnabled , true } , { Configuration . ConfigurationKeys . AppSec . ApiSecuritySampleDelay , 120 } } ;
115+ await using var tracer = TracerHelper . Create ( TracerSettings . Create ( config ) ) ;
116+ var apiSec = new ApiSecurity ( new SecuritySettings ( new CustomSettingsForTests ( config ) , new NullConfigurationTelemetry ( ) ) ) ;
114117 var dic = new Dictionary < string , object > { { "controller" , "test" } , { "action" , "test" } } ;
115- var tc = new TraceContext ( Mock . Of < IDatadogTracer > ( ) , new TraceTagCollection ( ) ) ;
118+ var tc = new TraceContext ( tracer , new TraceTagCollection ( ) ) ;
116119 tc . SetSamplingPriority ( SamplingPriorityValues . AutoKeep ) ;
117120 var dt = DateTime . UtcNow ;
118121
0 commit comments