Skip to content

Commit 9fdb5e6

Browse files
committed
FailingMiddleware for Ordering API
1 parent bbc1481 commit 9fdb5e6

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Ordering.API.Infrastructure.Middlewares
8+
{
9+
public class FailingMiddleware
10+
{
11+
private readonly RequestDelegate _next;
12+
private bool _mustFail;
13+
private readonly FailingOptions _options;
14+
public FailingMiddleware(RequestDelegate next, FailingOptions options)
15+
{
16+
_next = next;
17+
_options = options;
18+
_mustFail = false;
19+
}
20+
public async Task Invoke(HttpContext context)
21+
{
22+
var path = context.Request.Path;
23+
if (path.Equals(_options.ConfigPath, StringComparison.OrdinalIgnoreCase))
24+
{
25+
await ProcessConfigRequest(context);
26+
return;
27+
}
28+
29+
30+
if (_mustFail)
31+
{
32+
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
33+
context.Response.ContentType = "text/plain";
34+
await context.Response.WriteAsync("Failed due to FailingMiddleware enabled.");
35+
}
36+
else
37+
{
38+
await _next.Invoke(context);
39+
}
40+
}
41+
42+
private async Task ProcessConfigRequest(HttpContext context)
43+
{
44+
int i = 0;
45+
var enable = context.Request.Query.Keys.Any(k => k == "enable");
46+
var disable = context.Request.Query.Keys.Any(k => k == "disable");
47+
48+
if (enable && disable)
49+
{
50+
throw new ArgumentException("Must use enable or disable querystring values, but not both");
51+
}
52+
53+
if (disable)
54+
{
55+
_mustFail = false;
56+
await SendOkResponse(context, "FailingMiddleware disabled. Further requests will be processed.");
57+
return;
58+
}
59+
if (enable)
60+
{
61+
_mustFail = true;
62+
await SendOkResponse(context, "FailingMiddleware enabled. Further requests will return HTTP 500");
63+
return;
64+
}
65+
66+
// If reach here, that means that no valid parameter has been passed. Just output status
67+
await SendOkResponse(context, string.Format("FailingMiddleware is {0}", _mustFail ? "enabled" : "disabled"));
68+
return;
69+
}
70+
71+
private async Task SendOkResponse(HttpContext context, string message)
72+
{
73+
context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
74+
context.Response.ContentType = "text/plain";
75+
await context.Response.WriteAsync(message);
76+
}
77+
78+
}
79+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Ordering.API.Infrastructure.Middlewares
8+
{
9+
public static class FailingMiddlewareAppBuilderExtensions
10+
{
11+
public static IApplicationBuilder UseFailingMiddleware(this IApplicationBuilder builder)
12+
{
13+
return UseFailingMiddleware(builder, null);
14+
}
15+
public static IApplicationBuilder UseFailingMiddleware(this IApplicationBuilder builder, Action<FailingOptions> action)
16+
{
17+
var options = new FailingOptions();
18+
action?.Invoke(options);
19+
builder.UseMiddleware<FailingMiddleware>(options);
20+
return builder;
21+
}
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Ordering.API.Infrastructure.Middlewares
7+
{
8+
public class FailingOptions
9+
{
10+
public string ConfigPath = "/Failing";
11+
}
12+
}

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using AspNetCore.Http;
44
using Autofac;
55
using Autofac.Extensions.DependencyInjection;
6+
using global::Ordering.API.Infrastructure.Middlewares;
67
using Infrastructure;
78
using Infrastructure.Auth;
89
using Infrastructure.AutofacModules;
@@ -109,6 +110,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
109110

110111
app.UseCors("CorsPolicy");
111112

113+
app.UseFailingMiddleware();
114+
112115
ConfigureAuth(app);
113116

114117
app.UseMvcWithDefaultRoute();

0 commit comments

Comments
 (0)