diff --git a/src/Http/WolverineWebApi/Bugs/FrameRearrangeEndpoint.cs b/src/Http/WolverineWebApi/Bugs/FrameRearrangeEndpoint.cs new file mode 100644 index 000000000..3efccf1db --- /dev/null +++ b/src/Http/WolverineWebApi/Bugs/FrameRearrangeEndpoint.cs @@ -0,0 +1,46 @@ +using JasperFx.CodeGeneration; +using Microsoft.AspNetCore.Mvc; +using Wolverine.Http; +using Wolverine.Http.Marten; +using Wolverine.Middleware; +using Wolverine.Runtime; +using WolverineWebApi.Marten; + +namespace WolverineWebApi.Bugs; + +public static class FrameRearrangeEndpoint +{ + [WolverineGet("/frame-rearrange/{id}")] + public static Invoice Get([Document(Required = true, MaybeSoftDeleted = false)] Invoice invoice) + { + return invoice; + } +} + +public static class FrameRearrangeMiddleware +{ + public static void Before(HttpContext context) + { + // Very simplified code, but similar to what I have in production + var userAgents = context.Request.Headers.UserAgent; + if (userAgents.Any(f => f?.Contains("block-me") ?? false)) + { + throw new Exception("Blocked client"); + } + } + + public class HttpPolicy : IHttpPolicy + { + public void Apply(IReadOnlyList chains, GenerationRules rules, IServiceContainer container) + { + // Only apply this policy to the FrameRearrangeEndpoint, but in production it's applied to all endpoints + foreach (var chain in chains.Where(f => f.Method.HandlerType.Name == nameof(FrameRearrangeEndpoint))) + { + var middlewarePolicy = new MiddlewarePolicy(); + middlewarePolicy.AddType(typeof(FrameRearrangeMiddleware)); + + middlewarePolicy.Apply([chain], rules, container); + } + } + } +} \ No newline at end of file diff --git a/src/Http/WolverineWebApi/Program.cs b/src/Http/WolverineWebApi/Program.cs index 905ef19a7..8e2bbea95 100644 --- a/src/Http/WolverineWebApi/Program.cs +++ b/src/Http/WolverineWebApi/Program.cs @@ -25,6 +25,7 @@ using Wolverine.Http.Transport; using Wolverine.Marten; using WolverineWebApi; +using WolverineWebApi.Bugs; using WolverineWebApi.Marten; using WolverineWebApi.Samples; using WolverineWebApi.Things; @@ -264,6 +265,7 @@ opts.AddPolicy(); + opts.AddPolicy(); #region sample_adding_custom_parameter_handling