@@ -7,55 +7,54 @@ class ApiSecurityRequestSamplerTest extends DDSpecification {
7
7
8
8
void ' happy path with single request' () {
9
9
given :
10
- def ctx = Mock ( AppSecRequestContext )
10
+ def ctx = Spy (createContext( ' route1 ' , ' GET ' , 200 ) )
11
11
def sampler = new ApiSecurityRequestSampler ()
12
12
13
13
when :
14
14
sampler. preSampleRequest(ctx)
15
15
16
16
then :
17
- _ * ctx. getRoute() >> ' route1 '
18
- _ * ctx. getMethod() >> ' GET '
19
- _ * ctx. getResponseStatus() >> 200
17
+ 1 * ctx. getRoute()
18
+ 1 * ctx. getMethod()
19
+ 1 * ctx. getResponseStatus()
20
20
1 * ctx. setKeepOpenForApiSecurityPostProcessing(true )
21
+ 1 * ctx. setApiSecurityEndpointHash(_)
21
22
0 * _
22
23
23
24
when :
24
25
def sampleDecision = sampler. sampleRequest(ctx)
25
26
26
27
then :
27
28
sampleDecision
28
- _ * ctx. getRoute() >> ' route1'
29
- _ * ctx. getMethod() >> ' GET'
30
- _ * ctx. getResponseStatus() >> 200
31
29
_ * ctx. isKeepOpenForApiSecurityPostProcessing() >> true
30
+ 1 * ctx. getApiSecurityEndpointHash()
32
31
0 * _
33
32
}
34
33
35
34
void ' second request is not sampled for the same endpoint' () {
35
+ Long hash
36
36
given :
37
- AppSecRequestContext ctx1 = Mock ( AppSecRequestContext )
38
- AppSecRequestContext ctx2 = Mock ( AppSecRequestContext )
37
+ AppSecRequestContext ctx1 = Spy (createContext( ' route1 ' , ' GET ' , 200 ) )
38
+ AppSecRequestContext ctx2 = Spy (createContext( ' route1 ' , ' GET ' , 200 ) )
39
39
def sampler = new ApiSecurityRequestSampler ()
40
40
41
41
when :
42
42
sampler. preSampleRequest(ctx1)
43
43
def sampleDecision = sampler. sampleRequest(ctx1)
44
+ sampler. counter. release()
44
45
45
46
then :
46
47
sampleDecision
47
- _ * ctx1. getRoute() >> ' route1'
48
- _ * ctx1. getMethod() >> ' GET'
49
- _ * ctx1. getResponseStatus() >> 200
50
48
_ * _
51
49
52
50
when :
53
51
sampler. preSampleRequest(ctx2)
54
52
55
53
then :
56
- _ * ctx2. getRoute() >> ' route1'
57
- _ * ctx2. getMethod() >> ' GET'
58
- _ * ctx2. getResponseStatus() >> 200
54
+ 1 * ctx2. getRoute()
55
+ 1 * ctx2. getMethod()
56
+ 1 * ctx2. getResponseStatus()
57
+ 1 * ctx2. setApiSecurityEndpointHash(_)
59
58
0 * ctx2. setKeepOpenForApiSecurityPostProcessing(_)
60
59
0 * _
61
60
@@ -64,10 +63,98 @@ class ApiSecurityRequestSamplerTest extends DDSpecification {
64
63
65
64
then :
66
65
! sampleDecision
67
- _ * ctx2. getRoute() >> ' route1'
68
- _ * ctx2. getMethod() >> ' GET'
69
- _ * ctx2. getResponseStatus() >> 200
66
+ 1 * ctx2. getApiSecurityEndpointHash()
70
67
0 * _
71
68
}
72
69
70
+ void ' preSampleRequest with maximum concurrent contexts' () {
71
+ given :
72
+ final ctx1 = Spy (createContext(' route2' , ' GET' , 200 ))
73
+ final ctx2 = Spy (createContext(' route3' , ' GET' , 200 ))
74
+ final sampler = new ApiSecurityRequestSampler ()
75
+ assert sampler. MAX_POST_PROCESSING_TASKS > 0
76
+
77
+ when : ' exhaust the maximum number of concurrent contexts'
78
+ for (int i = 0 ; i < sampler. MAX_POST_PROCESSING_TASKS ; i++ ) {
79
+ sampler. preSampleRequest(createContext(' route1' , ' GET' , 200 + i))
80
+ }
81
+
82
+ and : ' try to sample one more'
83
+ sampler. preSampleRequest(ctx1)
84
+
85
+ then :
86
+ 1 * ctx1. getRoute()
87
+ 1 * ctx1. getMethod()
88
+ 1 * ctx1. getResponseStatus()
89
+ 1 * ctx1. setApiSecurityEndpointHash(_)
90
+ 0 * _
91
+
92
+ when : ' release one context'
93
+ sampler. counter. release()
94
+
95
+ and : ' next can be sampled'
96
+ sampler. preSampleRequest(ctx2)
97
+
98
+ then :
99
+ 1 * ctx2. getRoute()
100
+ 1 * ctx2. getMethod()
101
+ 1 * ctx2. getResponseStatus()
102
+ 1 * ctx2. setApiSecurityEndpointHash(_)
103
+ 1 * ctx2. setKeepOpenForApiSecurityPostProcessing(true )
104
+ 0 * _
105
+ }
106
+
107
+ void ' preSampleRequest with null route' () {
108
+ given :
109
+ def ctx = Spy (createContext(null , ' GET' , 200 ))
110
+ def sampler = new ApiSecurityRequestSampler ()
111
+
112
+ when :
113
+ def sampleDecision = sampler. preSampleRequest(ctx)
114
+
115
+ then :
116
+ ! sampleDecision
117
+ 1 * ctx. getRoute()
118
+ 0 * _
119
+ }
120
+
121
+ void ' preSampleRequest with null method' () {
122
+ given :
123
+ def ctx = Spy (createContext(' route1' , null , 200 ))
124
+ def sampler = new ApiSecurityRequestSampler ()
125
+
126
+ when :
127
+ def sampleDecision = sampler. preSampleRequest(ctx)
128
+
129
+ then :
130
+ ! sampleDecision
131
+ 1 * ctx. getRoute()
132
+ 1 * ctx. getMethod()
133
+ 0 * _
134
+ }
135
+
136
+ void ' preSampleRequest with 0 status code' () {
137
+ given :
138
+ def ctx = Spy (createContext(' route1' , ' GET' , 0 ))
139
+ def sampler = new ApiSecurityRequestSampler ()
140
+
141
+ when :
142
+ def sampleDecision = sampler. preSampleRequest(ctx)
143
+
144
+ then :
145
+ ! sampleDecision
146
+ 1 * ctx. getRoute()
147
+ 1 * ctx. getMethod()
148
+ 1 * ctx. getResponseStatus()
149
+ 0 * _
150
+ }
151
+
152
+ private AppSecRequestContext createContext (final String route , final String method , int statusCode ) {
153
+ final AppSecRequestContext ctx = new AppSecRequestContext ()
154
+ ctx. setRoute(route)
155
+ ctx. setMethod(method)
156
+ ctx. setResponseStatus(statusCode)
157
+ ctx
158
+ }
159
+
73
160
}
0 commit comments