Created
June 23, 2015 07:48
-
-
Save damianh/70d1db7ddfa7d3b74d7a to your computer and use it in GitHub Desktop.
Middleware that trolls front end devs. Use in conjunction with LimitsMiddleware and FakeReverseProxyHost to emulate real-world scenarios. Mwhahahahah!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Chaos | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Microsoft.Owin; | |
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>; | |
using MidFunc = System.Func< | |
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>, | |
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>>; | |
public static class ChaosMiddleware | |
{ | |
private static readonly List<AppFunc> s_monkeys; | |
static ChaosMiddleware() | |
{ | |
s_monkeys = new List<AppFunc> | |
{ | |
// Bunch of status codes | |
env => WriteStatus(env, 400, "Bad Request"), | |
env => WriteStatus(env, 403, "Forbidden"), | |
env => WriteStatus(env, 404, "Not found"), | |
env => WriteStatus(env, 408, "Request Timeout"), | |
env => WriteStatus(env, 429, "Too many requests"), | |
env => WriteStatus(env, 500, "Internal Server Error"), | |
env => WriteStatus(env, 502, "Bad Gateway"), | |
env => WriteStatus(env, 503, "Service Unavailable"), | |
env => WriteStatus(env, 504, "Gateway Timeout"), | |
//TODO | |
// - Malformed Content-MD5 request & response | |
// - drop properties from Json | |
// - corrupt encoding | |
// - change header name casing | |
}; | |
} | |
public static MidFunc Create(int errorRatePercent, string startWithSegment) | |
{ | |
var segment = new PathString(startWithSegment); | |
return next => | |
{ | |
var random = new Random(); | |
return env => | |
{ | |
var context = new OwinContext(env); | |
if (!context.Request.Path.StartsWithSegments(segment) || errorRatePercent == 0) | |
{ | |
return next(env); | |
} | |
var i = random.Next(1, 100); | |
if(i >= errorRatePercent) | |
{ | |
return next(env); | |
} | |
i = random.Next(0, s_monkeys.Count - 1); | |
return s_monkeys[i](env); | |
}; | |
}; | |
} | |
private static async Task WriteStatus(IDictionary<string, object> env, int statusCode, string reasonPhrase) | |
{ | |
var context = new OwinContext(env); | |
lock(Program.ConsoleWriterLock) | |
{ | |
var foreground = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.White; | |
Console.WriteLine(@"CHAOS MONKEY STRIKES \@(o・___・o)@/ {0} {1}", statusCode, context.Request.Uri.PathAndQuery); | |
Console.ForegroundColor = foreground; | |
} | |
context.Response.StatusCode = statusCode; | |
context.Response.ReasonPhrase = reasonPhrase; | |
context.Response.ContentType = "text/plain"; | |
await context.Response.WriteAsync(reasonPhrase); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your monkey could be better; may I propose @(◔ ◡ ◔)@