-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathGenericRandomErrorPlugin.cs
More file actions
339 lines (287 loc) · 12.2 KB
/
GenericRandomErrorPlugin.cs
File metadata and controls
339 lines (287 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using DevProxy.Abstractions.Plugins;
using DevProxy.Abstractions.Proxy;
using DevProxy.Abstractions.Utils;
using DevProxy.Plugins.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.CommandLine.Parsing;
using System.CommandLine;
using System.Globalization;
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
namespace DevProxy.Plugins.Behavior;
enum GenericRandomErrorFailMode
{
Throttled,
Random,
PassThru
}
public sealed class GenericRandomErrorConfiguration
{
public IEnumerable<GenericErrorResponse> Errors { get; set; } = [];
public string ErrorsFile { get; set; } = "errors.json";
public int Rate { get; set; } = 50;
public int RetryAfterInSeconds { get; set; } = 5;
}
public sealed class GenericRandomErrorPlugin(
ILogger<GenericRandomErrorPlugin> logger,
ISet<UrlToWatch> urlsToWatch,
IProxyConfiguration proxyConfiguration,
IConfigurationSection pluginConfigurationSection) :
BasePlugin<GenericRandomErrorConfiguration>(
logger,
urlsToWatch,
proxyConfiguration,
pluginConfigurationSection)
{
private const string _rateOptionName = "--failure-rate";
private readonly Random _random = new();
private GenericErrorResponsesLoader? _loader;
public override string Name => nameof(GenericRandomErrorPlugin);
public override async Task InitializeAsync(InitArgs e)
{
ArgumentNullException.ThrowIfNull(e);
await base.InitializeAsync(e);
Configuration.ErrorsFile = ProxyUtils.GetFullPath(Configuration.ErrorsFile, ProxyConfiguration.ConfigFile);
_loader = ActivatorUtilities.CreateInstance<GenericErrorResponsesLoader>(e.ServiceProvider, Configuration);
_loader.InitFileWatcher();
ValidateErrors();
}
public override Option[] GetOptions()
{
var _rateOption = new Option<int?>(_rateOptionName, ["-f"])
{
Description = "The percentage of chance that a request will fail",
HelpName = "failure rate"
};
// Add validation for rate option
_rateOption.Validators.Add(result =>
{
var rate = result.GetValueOrDefault<int?>();
if (rate is not null && (rate < 0 || rate > 100))
{
result.AddError($"Rate must be between 0 and 100. Received: {rate}");
}
});
return [_rateOption];
}
public override void OptionsLoaded(OptionsLoadedArgs e)
{
ArgumentNullException.ThrowIfNull(e);
base.OptionsLoaded(e);
var parseResult = e.ParseResult;
var rate = parseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
if (rate is not null)
{
Configuration.Rate = rate.Value;
}
}
public override Task BeforeRequestAsync(ProxyRequestArgs e)
{
Logger.LogTrace("{Method} called", nameof(BeforeRequestAsync));
ArgumentNullException.ThrowIfNull(e);
if (!e.HasRequestUrlMatch(UrlsToWatch))
{
Logger.LogRequest("URL not matched", MessageType.Skipped, new(e.Session));
return Task.CompletedTask;
}
if (e.ResponseState.HasBeenSet)
{
Logger.LogRequest("Response already set", MessageType.Skipped, new(e.Session));
return Task.CompletedTask;
}
var failMode = ShouldFail();
if (failMode == GenericRandomErrorFailMode.PassThru && Configuration.Rate != 100)
{
Logger.LogRequest("Pass through", MessageType.Skipped, new(e.Session));
return Task.CompletedTask;
}
FailResponse(e);
Logger.LogTrace("Left {Name}", nameof(BeforeRequestAsync));
return Task.CompletedTask;
}
// uses config to determine if a request should be failed
private GenericRandomErrorFailMode ShouldFail() => _random.Next(1, 100) <= Configuration.Rate ? GenericRandomErrorFailMode.Random : GenericRandomErrorFailMode.PassThru;
private void FailResponse(ProxyRequestArgs e)
{
var matchingResponse = GetMatchingErrorResponse(e.Session.HttpClient.Request);
if (matchingResponse is not null &&
matchingResponse.Responses is not null)
{
// pick a random error response for the current request
var error = matchingResponse.Responses.ElementAt(_random.Next(0, matchingResponse.Responses.Count()));
UpdateProxyResponse(e, error);
}
else
{
Logger.LogRequest("No matching error response found", MessageType.Skipped, new(e.Session));
}
}
private ThrottlingInfo ShouldThrottle(Request request, string throttlingKey)
{
var throttleKeyForRequest = BuildThrottleKey(request);
return new(throttleKeyForRequest == throttlingKey ? Configuration.RetryAfterInSeconds : 0, "Retry-After");
}
private GenericErrorResponse? GetMatchingErrorResponse(Request request)
{
if (Configuration.Errors is null ||
!Configuration.Errors.Any())
{
return null;
}
var errorResponse = Configuration.Errors.FirstOrDefault(errorResponse =>
{
if (errorResponse.Request is null)
{
return false;
}
if (errorResponse.Responses is null)
{
return false;
}
if (errorResponse.Request.Method != request.Method)
{
return false;
}
if (errorResponse.Request.Url == request.Url &&
HasMatchingBody(errorResponse, request))
{
return true;
}
// check if the URL contains a wildcard
// if it doesn't, it's not a match for the current request for sure
if (!errorResponse.Request.Url.Contains('*', StringComparison.OrdinalIgnoreCase))
{
return false;
}
// turn mock URL with wildcard into a regex and match against the request URL
var errorResponseUrlRegex = Regex.Escape(errorResponse.Request.Url).Replace("\\*", ".*", StringComparison.OrdinalIgnoreCase);
return Regex.IsMatch(request.Url, $"^{errorResponseUrlRegex}$") &&
HasMatchingBody(errorResponse, request);
});
return errorResponse;
}
private void UpdateProxyResponse(ProxyRequestArgs e, GenericErrorResponseResponse error)
{
var session = e.Session;
var request = session.HttpClient.Request;
var headers = new List<GenericErrorResponseHeader>();
if (error.Headers is not null)
{
headers.AddRange(error.Headers);
}
if (error.StatusCode == (int)HttpStatusCode.TooManyRequests &&
error.Headers is not null &&
error.Headers.FirstOrDefault(h => h.Name is "Retry-After" or "retry-after")?.Value == "@dynamic")
{
var retryAfterDate = DateTime.Now.AddSeconds(Configuration.RetryAfterInSeconds);
if (!e.GlobalData.TryGetValue(RetryAfterPlugin.ThrottledRequestsKey, out var value))
{
value = new List<ThrottlerInfo>();
e.GlobalData.Add(RetryAfterPlugin.ThrottledRequestsKey, value);
}
var throttledRequests = value as List<ThrottlerInfo>;
throttledRequests?.Add(new(BuildThrottleKey(request), ShouldThrottle, retryAfterDate));
// replace the header with the @dynamic value with the actual value
var h = headers.First(h => h.Name is "Retry-After" or "retry-after");
_ = headers.Remove(h);
headers.Add(new("Retry-After", Configuration.RetryAfterInSeconds.ToString(CultureInfo.InvariantCulture)));
}
var statusCode = (HttpStatusCode)(error.StatusCode ?? 400);
var body = error.Body is null ? string.Empty : JsonSerializer.Serialize(error.Body, ProxyUtils.JsonSerializerOptions);
// we get a JSON string so need to start with the opening quote
if (body.StartsWith("\"@"))
{
// we've got a mock body starting with @-token which means we're sending
// a response from a file on disk
// if we can read the file, we can immediately send the response and
// skip the rest of the logic in this method
// remove the surrounding quotes and the @-token
var filePath = Path.Combine(Path.GetDirectoryName(Configuration.ErrorsFile) ?? "", ProxyUtils.ReplacePathTokens(body.Trim('"').Substring(1)));
if (!File.Exists(filePath))
{
Logger.LogError("File {FilePath} not found. Serving file path in the mock response", (string?)filePath);
session.GenericResponse(body, statusCode, headers.Select(h => new HttpHeader(h.Name, h.Value)));
}
else
{
var bodyBytes = File.ReadAllBytes(filePath);
session.GenericResponse(bodyBytes, statusCode, headers.Select(h => new HttpHeader(h.Name, h.Value)));
}
}
else
{
session.GenericResponse(body, statusCode, headers.Select(h => new HttpHeader(h.Name, h.Value)));
}
e.ResponseState.HasBeenSet = true;
Logger.LogRequest($"{error.StatusCode} {statusCode}", MessageType.Chaos, new(e.Session));
}
private void ValidateErrors()
{
Logger.LogDebug("Validating error responses");
if (Configuration.Errors is null ||
!Configuration.Errors.Any())
{
Logger.LogDebug("No error responses defined");
return;
}
var unmatchedErrorUrls = new List<string>();
foreach (var error in Configuration.Errors)
{
if (error.Request is null)
{
Logger.LogDebug("Error response is missing a request");
continue;
}
if (string.IsNullOrEmpty(error.Request.Url))
{
Logger.LogDebug("Error response is missing a URL");
continue;
}
if (!ProxyUtils.MatchesUrlToWatch(UrlsToWatch, error.Request.Url, true))
{
unmatchedErrorUrls.Add(error.Request.Url);
}
}
if (unmatchedErrorUrls.Count == 0)
{
return;
}
var suggestedWildcards = ProxyUtils.GetWildcardPatterns(unmatchedErrorUrls.AsReadOnly());
Logger.LogWarning(
"The following URLs in {ErrorsFile} don't match any URL to watch: {UnmatchedMocks}. Add the following URLs to URLs to watch: {UrlsToWatch}",
Configuration.ErrorsFile,
string.Join(", ", unmatchedErrorUrls),
string.Join(", ", suggestedWildcards)
);
}
private static bool HasMatchingBody(GenericErrorResponse errorResponse, Request request)
{
if (request.Method == "GET")
{
// GET requests don't have a body so we can't match on it
return true;
}
if (errorResponse.Request?.BodyFragment is null)
{
// no body fragment to match on
return true;
}
if (!request.HasBody || string.IsNullOrEmpty(request.BodyString))
{
// error response defines a body fragment but the request has no body
// so it can't match
return false;
}
return request.BodyString.Contains(errorResponse.Request.BodyFragment, StringComparison.OrdinalIgnoreCase);
}
// throttle requests per host
private static string BuildThrottleKey(Request r) => r.RequestUri.Host;
}