File tree 3 files changed +77
-8
lines changed
3 files changed +77
-8
lines changed Original file line number Diff line number Diff line change @@ -12,28 +12,30 @@ public class Rule
12
12
public IList < Func < HttpRequest , bool > > Predictions { get ; set ; }
13
13
public Action < HttpResponse > Action { get ; set ; }
14
14
public RuleSet RuleSet { get ; set ; }
15
+ public int ? MaxMatchCount { get ; private set ; }
16
+ private int MatchCount { get ; set ; }
15
17
16
18
public Rule ( RuleSet ruleSet )
17
19
{
18
20
RuleSet = ruleSet ;
19
21
Predictions = new List < Func < HttpRequest , bool > > ( ) ;
20
22
}
21
23
22
- public bool IsValid ( )
24
+ public bool IsActive ( )
23
25
{
24
- return Action != null ;
26
+ if ( Action == null )
27
+ {
28
+ return false ;
29
+ }
30
+
31
+ return ! MaxMatchCount . HasValue || MatchCount < MaxMatchCount ;
25
32
}
26
33
27
34
/// <summary>
28
35
/// Complete current Rule and add a new rule on parent RuleSet
29
36
/// </summary>
30
- /// <exception cref="InvalidOperationException">throws if current rule is not completed</exception>
31
37
public Rule AddRule ( )
32
38
{
33
- if ( ! IsValid ( ) )
34
- {
35
- throw new InvalidOperationException ( "Previous rule not completed" ) ;
36
- }
37
39
return RuleSet . AddRule ( ) ;
38
40
}
39
41
@@ -128,5 +130,22 @@ public Rule SetOkResponse(string output)
128
130
} ;
129
131
return this ;
130
132
}
133
+
134
+ /// <summary>
135
+ /// Set how much request could apply to this rule
136
+ /// </summary>
137
+ public Rule SetMaxMatchCount ( int ? maxMatchCount )
138
+ {
139
+ MaxMatchCount = maxMatchCount ;
140
+ return this ;
141
+ }
142
+
143
+ /// <summary>
144
+ /// Increase the count this rule applies
145
+ /// </summary>
146
+ internal void SetMatchCount ( )
147
+ {
148
+ MatchCount ++ ;
149
+ }
131
150
}
132
151
}
Original file line number Diff line number Diff line change @@ -22,9 +22,15 @@ public virtual void Configure(IApplicationBuilder app, RuleSet ruleSet)
22
22
var matchFound = false ;
23
23
foreach ( var rule in ruleSet . Rules )
24
24
{
25
+ if ( ! rule . IsActive ( ) )
26
+ {
27
+ continue ;
28
+ }
29
+
25
30
var match = rule . Predictions . All ( prediction => prediction ( context . Request ) ) ;
26
31
if ( match )
27
32
{
33
+ rule . SetMatchCount ( ) ;
28
34
await Task . Run ( ( ) =>
29
35
{
30
36
rule . Action ( context . Response ) ;
Original file line number Diff line number Diff line change @@ -297,5 +297,49 @@ public async Task WhenHaveDelegatingHandler_ShouldApplyToClient()
297
297
// Assert
298
298
AssertOkResponse ( actual ) ;
299
299
}
300
+
301
+ [ Test ]
302
+ public async Task WhenMaxCountReached_ShouldFail ( )
303
+ {
304
+ // Arrange
305
+ _server . CurrentRuleSet
306
+ . AddRule ( )
307
+ . SetMaxMatchCount ( 1 )
308
+ . SetOkResponse ( _okResponse ) ;
309
+
310
+ var client = _server . CreateClient ( ) ;
311
+
312
+ // Act
313
+ var first = await client . GetAsync ( "/" ) ;
314
+ var second = await client . GetAsync ( "/" ) ;
315
+
316
+ // Assert
317
+ AssertOkResponse ( first ) ;
318
+ AssertNoPredictionMatched ( second ) ;
319
+ }
320
+
321
+ [ Test ]
322
+ public async Task WhenFirstMatchReachedMaxCount_ShouldApplySecond ( )
323
+ {
324
+ // Arrange
325
+ var firstResponse = "first rule matched" ;
326
+ var secondResponse = "second rule matched" ;
327
+ _server . CurrentRuleSet
328
+ . AddRule ( )
329
+ . SetMaxMatchCount ( 1 )
330
+ . SetOkResponse ( firstResponse )
331
+ . AddRule ( )
332
+ . SetOkResponse ( secondResponse ) ;
333
+
334
+ var client = _server . CreateClient ( ) ;
335
+
336
+ // Act
337
+ var first = await client . GetAsync ( "/" ) ;
338
+ var second = await client . GetAsync ( "/" ) ;
339
+
340
+ // Assert
341
+ AssertOkResponse ( first , firstResponse ) ;
342
+ AssertOkResponse ( second , secondResponse ) ;
343
+ }
300
344
}
301
- }
345
+ }
You can’t perform that action at this time.
0 commit comments