Skip to content

Commit 339307d

Browse files
Adds support for nth request in mocks. Closes #330 (#339)
1 parent 472e0ad commit 339307d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

m365-developer-proxy-plugins/MockResponses/MockResponse.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class MockResponse {
1010
public string Url { get; set; } = string.Empty;
1111
[JsonPropertyName("method")]
1212
public string Method { get; set; } = "GET";
13+
[JsonPropertyName("nth")]
14+
public int? Nth { get; set; }
1315
[JsonPropertyName("responseCode")]
1416
public int? ResponseCode { get; set; } = 200;
1517
[JsonPropertyName("responseBody")]

m365-developer-proxy-plugins/MockResponses/MockResponsePlugin.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)