Skip to content

Commit 5d61695

Browse files
Fixes throttling handling
Adds Access-Control-Allow-Origin on error headers
1 parent 0e03dc0 commit 5d61695

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

msgraph-chaos-proxy/ChaosEngine.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public ChaosEngine(ChaosProxyConfiguration config) {
8080
_random = new Random();
8181
_throttledRequests = new Dictionary<string, DateTime>();
8282
if (_config.AllowedErrors.Any()) {
83-
foreach(string k in _methodStatusCode.Keys) {
83+
foreach (string k in _methodStatusCode.Keys) {
8484
_methodStatusCode[k] = _methodStatusCode[k].Where(e => _config.AllowedErrors.Any(a => (int)e == a)).ToArray();
8585
}
8686
}
@@ -159,7 +159,7 @@ private FailMode ShouldFail(Request r) {
159159
}
160160
else {
161161
// clean up expired throttled request and ensure that this request is passed through.
162-
_throttledRequests.Remove(r.Url);
162+
_throttledRequests.Remove(key);
163163
return FailMode.PassThru;
164164
}
165165
}
@@ -255,9 +255,6 @@ private static void ProcessMockResponse(SessionEventArgs e, ResponseComponents r
255255
responseComponents.Headers.Add(new HttpHeader(key, matchingResponse.ResponseHeaders[key]));
256256
}
257257
}
258-
if (e.HttpClient.Request.Headers.FirstOrDefault((HttpHeader h) => h.Name.Equals("Origin", StringComparison.OrdinalIgnoreCase)) is not null) {
259-
responseComponents.Headers.Add(new HttpHeader("Access-Control-Allow-Origin", "*"));
260-
}
261258

262259
if (matchingResponse.ResponseBody is not null) {
263260
var bodyString = JsonSerializer.Serialize(matchingResponse.ResponseBody) as string;
@@ -274,6 +271,10 @@ private static void ProcessMockResponse(SessionEventArgs e, ResponseComponents r
274271
responseComponents.Body = bodyString;
275272
}
276273
else {
274+
if (e.HttpClient.Request.Headers.FirstOrDefault((HttpHeader h) => h.Name.Equals("Origin", StringComparison.OrdinalIgnoreCase)) is not null) {
275+
responseComponents.Headers.Add(new HttpHeader("Access-Control-Allow-Origin", "*"));
276+
}
277+
277278
var bodyBytes = File.ReadAllBytes(filePath);
278279
e.GenericResponse(bodyBytes, responseComponents.ErrorStatus, responseComponents.Headers);
279280
responseComponents.ResponseIsComplete = true;
@@ -318,6 +319,10 @@ private void UpdateProxyResponse(SessionEventArgs e, ResponseComponents response
318319
responseComponents.Headers.Add(new HttpHeader("Retry-After", retryAfterInSeconds.ToString()));
319320
}
320321

322+
if (e.HttpClient.Request.Headers.FirstOrDefault((HttpHeader h) => h.Name.Equals("Origin", StringComparison.OrdinalIgnoreCase)) is not null) {
323+
responseComponents.Headers.Add(new HttpHeader("Access-Control-Allow-Origin", "*"));
324+
}
325+
321326
if ((int)responseComponents.ErrorStatus >= 400 && string.IsNullOrEmpty(responseComponents.Body)) {
322327
responseComponents.Body = JsonSerializer.Serialize(new ErrorResponseBody(
323328
new ErrorResponseError {

msgraph-chaos-proxy/Properties/launchSettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"Default": {
2929
"commandName": "Project",
30+
"commandLineArgs": "--no-mocks",
3031
"hotReloadEnabled": true
3132
}
3233
}

0 commit comments

Comments
 (0)