@@ -30,6 +30,9 @@ public class MockResponsePlugin : BaseProxyPlugin {
3030 private readonly Option < string ? > _mocksFile ;
3131 public override string Name => nameof ( MockResponsePlugin ) ;
3232 private IProxyConfiguration ? _proxyConfiguration ;
33+ // tracks the number of times a mock has been applied
34+ // used in combination with mocks that have an Nth property
35+ private Dictionary < string , int > _appliedMocks = new ( ) ;
3336
3437 public MockResponsePlugin ( ) {
3538 _noMocks = new Option < bool ? > ( "--no-mocks" , "Disable loading mock requests" ) ;
@@ -107,7 +110,7 @@ _configuration.Responses is null ||
107110
108111 var mockResponse = _configuration . Responses . FirstOrDefault ( mockResponse => {
109112 if ( mockResponse . Method != request . Method ) return false ;
110- if ( mockResponse . Url == request . Url ) {
113+ if ( mockResponse . Url == request . Url && IsNthRequest ( mockResponse ) ) {
111114 return true ;
112115 }
113116
@@ -119,11 +122,36 @@ _configuration.Responses is null ||
119122
120123 //turn mock URL with wildcard into a regex and match against the request URL
121124 var mockResponseUrlRegex = Regex . Escape ( mockResponse . Url ) . Replace ( "\\ *" , ".*" ) ;
122- return Regex . IsMatch ( request . Url , $ "^{ mockResponseUrlRegex } $") ;
125+ return Regex . IsMatch ( request . Url , $ "^{ mockResponseUrlRegex } $") && IsNthRequest ( mockResponse ) ;
123126 } ) ;
127+
128+ if ( mockResponse is not null )
129+ {
130+ if ( ! _appliedMocks . ContainsKey ( mockResponse . Url ) )
131+ {
132+ _appliedMocks . Add ( mockResponse . Url , 0 ) ;
133+ }
134+ _appliedMocks [ mockResponse . Url ] ++ ;
135+ }
136+
124137 return mockResponse ;
125138 }
126139
140+ private bool IsNthRequest ( MockResponse mockResponse )
141+ {
142+ if ( mockResponse . Nth is null )
143+ {
144+ // mock doesn't define an Nth property so it always qualifies
145+ return true ;
146+ }
147+
148+ var nth = 0 ;
149+ _appliedMocks . TryGetValue ( mockResponse . Url , out nth ) ;
150+ nth ++ ;
151+
152+ return mockResponse . Nth == nth ;
153+ }
154+
127155 private void ProcessMockResponse ( SessionEventArgs e , MockResponse matchingResponse ) {
128156 string ? body = null ;
129157 string requestId = Guid . NewGuid ( ) . ToString ( ) ;
0 commit comments