From efd7fdc91a860940e99ecdb9b0822c29c523d50a Mon Sep 17 00:00:00 2001 From: Chris Canal Date: Mon, 30 Oct 2023 16:53:35 -0600 Subject: [PATCH] Add wildcard routing support. --- .../Wolverine.Http.Tests/route_wildcard.cs | 21 ++++++++++ src/Http/Wolverine.Http/HttpChain.Codegen.cs | 2 +- .../WolverineHandlers/GET_wildcard_name.cs | 39 +++++++++++++++++++ src/Http/WolverineWebApi/WildcardEndpoint.cs | 12 ++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/Http/Wolverine.Http.Tests/route_wildcard.cs create mode 100644 src/Http/WolverineWebApi/Internal/Generated/WolverineHandlers/GET_wildcard_name.cs create mode 100644 src/Http/WolverineWebApi/WildcardEndpoint.cs diff --git a/src/Http/Wolverine.Http.Tests/route_wildcard.cs b/src/Http/Wolverine.Http.Tests/route_wildcard.cs new file mode 100644 index 000000000..aa2d3cb23 --- /dev/null +++ b/src/Http/Wolverine.Http.Tests/route_wildcard.cs @@ -0,0 +1,21 @@ +using Shouldly; + +namespace Wolverine.Http.Tests; + +public class route_wildcard : IntegrationContext +{ + public route_wildcard(AppFixture fixture) : base(fixture) + { + } + + [Fact] + public async Task wildcard() + { + var body = await Scenario(x => + { + x.Get.Url("/wildcard/one/two/three"); + }); + + body.ReadAsText().ShouldBe("one/two/three"); + } +} \ No newline at end of file diff --git a/src/Http/Wolverine.Http/HttpChain.Codegen.cs b/src/Http/Wolverine.Http/HttpChain.Codegen.cs index 7c420c3d1..fa79335ec 100644 --- a/src/Http/Wolverine.Http/HttpChain.Codegen.cs +++ b/src/Http/Wolverine.Http/HttpChain.Codegen.cs @@ -109,7 +109,7 @@ internal IEnumerable DetermineFrames(GenerationRules rules) private string determineFileName() { - var parts = RoutePattern.RawText.Replace("{", "").Replace("}", "").Split('/').Select(x => x.Split(':').First()); + var parts = RoutePattern.RawText.Replace("{", "").Replace("*", "").Replace("}", "").Split('/').Select(x => x.Split(':').First()); return _httpMethods.Select(x => x.ToUpper()).Concat(parts).Join("_").Replace("-", "_").Replace("__", "_"); } diff --git a/src/Http/WolverineWebApi/Internal/Generated/WolverineHandlers/GET_wildcard_name.cs b/src/Http/WolverineWebApi/Internal/Generated/WolverineHandlers/GET_wildcard_name.cs new file mode 100644 index 000000000..089f2853d --- /dev/null +++ b/src/Http/WolverineWebApi/Internal/Generated/WolverineHandlers/GET_wildcard_name.cs @@ -0,0 +1,39 @@ +// +#pragma warning disable +using Microsoft.AspNetCore.Routing; +using System; +using System.Linq; +using Wolverine.Http; + +namespace Internal.Generated.WolverineHandlers +{ + // START: GET_wildcard_name + public class GET_wildcard_name : Wolverine.Http.HttpHandler + { + private readonly Wolverine.Http.WolverineHttpOptions _wolverineHttpOptions; + + public GET_wildcard_name(Wolverine.Http.WolverineHttpOptions wolverineHttpOptions) : base(wolverineHttpOptions) + { + _wolverineHttpOptions = wolverineHttpOptions; + } + + + + public override async System.Threading.Tasks.Task Handle(Microsoft.AspNetCore.Http.HttpContext httpContext) + { + var wildcardEndpoint = new WolverineWebApi.WildcardEndpoint(); + var name = (string)httpContext.GetRouteValue("name"); + + // The actual HTTP request handler execution + var result_of_Wildcard = wildcardEndpoint.Wildcard(name); + + await WriteString(httpContext, result_of_Wildcard); + } + + } + + // END: GET_wildcard_name + + +} + diff --git a/src/Http/WolverineWebApi/WildcardEndpoint.cs b/src/Http/WolverineWebApi/WildcardEndpoint.cs new file mode 100644 index 000000000..edd7c8272 --- /dev/null +++ b/src/Http/WolverineWebApi/WildcardEndpoint.cs @@ -0,0 +1,12 @@ +using Wolverine.Http; + +namespace WolverineWebApi; + +public class WildcardEndpoint +{ + [WolverineGet("/wildcard/{*name}")] + public string Wildcard(string name) + { + return name; + } +} \ No newline at end of file