File tree Expand file tree Collapse file tree 3 files changed +77
-8
lines changed
Expand file tree Collapse file tree 3 files changed +77
-8
lines changed Original file line number Diff line number Diff line change @@ -12,28 +12,30 @@ public class Rule
1212 public IList < Func < HttpRequest , bool > > Predictions { get ; set ; }
1313 public Action < HttpResponse > Action { get ; set ; }
1414 public RuleSet RuleSet { get ; set ; }
15+ public int ? MaxMatchCount { get ; private set ; }
16+ private int MatchCount { get ; set ; }
1517
1618 public Rule ( RuleSet ruleSet )
1719 {
1820 RuleSet = ruleSet ;
1921 Predictions = new List < Func < HttpRequest , bool > > ( ) ;
2022 }
2123
22- public bool IsValid ( )
24+ public bool IsActive ( )
2325 {
24- return Action != null ;
26+ if ( Action == null )
27+ {
28+ return false ;
29+ }
30+
31+ return ! MaxMatchCount . HasValue || MatchCount < MaxMatchCount ;
2532 }
2633
2734 /// <summary>
2835 /// Complete current Rule and add a new rule on parent RuleSet
2936 /// </summary>
30- /// <exception cref="InvalidOperationException">throws if current rule is not completed</exception>
3137 public Rule AddRule ( )
3238 {
33- if ( ! IsValid ( ) )
34- {
35- throw new InvalidOperationException ( "Previous rule not completed" ) ;
36- }
3739 return RuleSet . AddRule ( ) ;
3840 }
3941
@@ -128,5 +130,22 @@ public Rule SetOkResponse(string output)
128130 } ;
129131 return this ;
130132 }
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+ }
131150 }
132151}
Original file line number Diff line number Diff line change @@ -22,9 +22,15 @@ public virtual void Configure(IApplicationBuilder app, RuleSet ruleSet)
2222 var matchFound = false ;
2323 foreach ( var rule in ruleSet . Rules )
2424 {
25+ if ( ! rule . IsActive ( ) )
26+ {
27+ continue ;
28+ }
29+
2530 var match = rule . Predictions . All ( prediction => prediction ( context . Request ) ) ;
2631 if ( match )
2732 {
33+ rule . SetMatchCount ( ) ;
2834 await Task . Run ( ( ) =>
2935 {
3036 rule . Action ( context . Response ) ;
Original file line number Diff line number Diff line change @@ -297,5 +297,49 @@ public async Task WhenHaveDelegatingHandler_ShouldApplyToClient()
297297 // Assert
298298 AssertOkResponse ( actual ) ;
299299 }
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+ }
300344 }
301- }
345+ }
You can’t perform that action at this time.
0 commit comments