FailingMiddleware for Ordering API
This commit is contained in:
parent
bbc1481979
commit
9fdb5e6c96
@ -0,0 +1,79 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Infrastructure.Middlewares
|
||||
{
|
||||
public class FailingMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private bool _mustFail;
|
||||
private readonly FailingOptions _options;
|
||||
public FailingMiddleware(RequestDelegate next, FailingOptions options)
|
||||
{
|
||||
_next = next;
|
||||
_options = options;
|
||||
_mustFail = false;
|
||||
}
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
var path = context.Request.Path;
|
||||
if (path.Equals(_options.ConfigPath, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await ProcessConfigRequest(context);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (_mustFail)
|
||||
{
|
||||
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
|
||||
context.Response.ContentType = "text/plain";
|
||||
await context.Response.WriteAsync("Failed due to FailingMiddleware enabled.");
|
||||
}
|
||||
else
|
||||
{
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessConfigRequest(HttpContext context)
|
||||
{
|
||||
int i = 0;
|
||||
var enable = context.Request.Query.Keys.Any(k => k == "enable");
|
||||
var disable = context.Request.Query.Keys.Any(k => k == "disable");
|
||||
|
||||
if (enable && disable)
|
||||
{
|
||||
throw new ArgumentException("Must use enable or disable querystring values, but not both");
|
||||
}
|
||||
|
||||
if (disable)
|
||||
{
|
||||
_mustFail = false;
|
||||
await SendOkResponse(context, "FailingMiddleware disabled. Further requests will be processed.");
|
||||
return;
|
||||
}
|
||||
if (enable)
|
||||
{
|
||||
_mustFail = true;
|
||||
await SendOkResponse(context, "FailingMiddleware enabled. Further requests will return HTTP 500");
|
||||
return;
|
||||
}
|
||||
|
||||
// If reach here, that means that no valid parameter has been passed. Just output status
|
||||
await SendOkResponse(context, string.Format("FailingMiddleware is {0}", _mustFail ? "enabled" : "disabled"));
|
||||
return;
|
||||
}
|
||||
|
||||
private async Task SendOkResponse(HttpContext context, string message)
|
||||
{
|
||||
context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
|
||||
context.Response.ContentType = "text/plain";
|
||||
await context.Response.WriteAsync(message);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Infrastructure.Middlewares
|
||||
{
|
||||
public static class FailingMiddlewareAppBuilderExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseFailingMiddleware(this IApplicationBuilder builder)
|
||||
{
|
||||
return UseFailingMiddleware(builder, null);
|
||||
}
|
||||
public static IApplicationBuilder UseFailingMiddleware(this IApplicationBuilder builder, Action<FailingOptions> action)
|
||||
{
|
||||
var options = new FailingOptions();
|
||||
action?.Invoke(options);
|
||||
builder.UseMiddleware<FailingMiddleware>(options);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Infrastructure.Middlewares
|
||||
{
|
||||
public class FailingOptions
|
||||
{
|
||||
public string ConfigPath = "/Failing";
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
using AspNetCore.Http;
|
||||
using Autofac;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using global::Ordering.API.Infrastructure.Middlewares;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Auth;
|
||||
using Infrastructure.AutofacModules;
|
||||
@ -109,6 +110,8 @@
|
||||
|
||||
app.UseCors("CorsPolicy");
|
||||
|
||||
app.UseFailingMiddleware();
|
||||
|
||||
ConfigureAuth(app);
|
||||
|
||||
app.UseMvcWithDefaultRoute();
|
||||
|
Loading…
x
Reference in New Issue
Block a user