From 4bac8398c5c37e3f912591dea7b196a495dd021f Mon Sep 17 00:00:00 2001 From: Onkel Mato <46560885+OnkelMato@users.noreply.github.com> Date: Sun, 12 May 2024 15:22:39 +0000 Subject: [PATCH] nested test module added and code fixed for nested classes. --- .../Features/Home/NestedHomeModule.cs | 16 +++++ src/Carter/CarterExtensions.cs | 2 +- test/Carter.Tests/RouteExtensionsTests.cs | 60 ++++++++++++++++--- test/Carter.Tests/TestModule.cs | 18 ++++++ test/Carter.Tests/TypeExtensionTests.cs | 2 +- 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 samples/CarterSample/Features/Home/NestedHomeModule.cs diff --git a/samples/CarterSample/Features/Home/NestedHomeModule.cs b/samples/CarterSample/Features/Home/NestedHomeModule.cs new file mode 100644 index 0000000..74963ec --- /dev/null +++ b/samples/CarterSample/Features/Home/NestedHomeModule.cs @@ -0,0 +1,16 @@ +namespace CarterSample.Features.Home; + +public static class NestedHomeModule +{ + public class HomeModule : ICarterModule + { + public void AddRoutes(IEndpointRouteBuilder app) + { + app.MapGet("/nestedHome", (HttpResponse res) => + { + res.StatusCode = 409; + return Results.Text("There's no place like 127.0.0.1"); + }); + } + } +} diff --git a/src/Carter/CarterExtensions.cs b/src/Carter/CarterExtensions.cs index 586b232..2a0b29f 100644 --- a/src/Carter/CarterExtensions.cs +++ b/src/Carter/CarterExtensions.cs @@ -253,7 +253,7 @@ private static IEnumerable GetNewModules(CarterConfigurator carterConfigur !t.IsAbstract && typeof(ICarterModule).IsAssignableFrom(t) && t != typeof(ICarterModule) && - t.IsPublic + (t.IsPublic || t.IsNestedPublic) )); carterConfigurator.ModuleTypes.AddRange(modules); diff --git a/test/Carter.Tests/RouteExtensionsTests.cs b/test/Carter.Tests/RouteExtensionsTests.cs index 17c85a2..c9c783b 100644 --- a/test/Carter.Tests/RouteExtensionsTests.cs +++ b/test/Carter.Tests/RouteExtensionsTests.cs @@ -36,11 +36,10 @@ public RouteExtensionsTests(ITestOutputHelper outputHelper) x.AddRouting(); x.AddCarter(configurator: c => - { - c.WithModule(); - c.WithValidator(); - } - ); + { + c.WithModule(); + c.WithValidator(); + }); }) .Configure(x => { @@ -82,14 +81,61 @@ public async Task Should_hit_route_if_validation_successful(string httpMethod) { var res = await this.httpClient.SendAsync(new HttpRequestMessage(new HttpMethod(httpMethod), "/endpointfilter") { - Content = new StringContent(JsonConvert.SerializeObject(new TestModel{MyStringProperty = "hi", MyIntProperty = 123}), Encoding.UTF8, "application/json") + Content = new StringContent(JsonConvert.SerializeObject(new TestModel { MyStringProperty = "hi", MyIntProperty = 123 }), Encoding.UTF8, "application/json") }); var body = await res.Content.ReadAsStringAsync(); - + Assert.Equal(httpMethod, body); } } +public class NestedRouteExtensionsTests +{ + private readonly TestServer server; + + private readonly HttpClient httpClient; + + public NestedRouteExtensionsTests(ITestOutputHelper outputHelper) + { + this.server = new TestServer( + new WebHostBuilder() + .ConfigureServices(x => + { + x.AddLogging(b => + { + XUnitLoggerExtensions.AddXUnit((ILoggingBuilder)b, outputHelper, x => x.IncludeScopes = true); + b.SetMinimumLevel(LogLevel.Debug); + }); + + x.AddSingleton(); + + x.AddRouting(); + x.AddCarter(configurator: c => { + c.WithValidator(); + } + ); + }) + .Configure(x => + { + x.UseRouting(); + x.UseEndpoints(builder => builder.MapCarter()); + }) + ); + this.httpClient = this.server.CreateClient(); + } + + [Theory] + [InlineData("GET")] + public async Task Should_have_nested_class_registered(string httpMethod) + { + var res = await this.httpClient.SendAsync(new HttpRequestMessage(new HttpMethod(httpMethod), "/nested") + { + Content = new StringContent(JsonConvert.SerializeObject(new TestModel()), Encoding.UTF8, "application/json") + }); + Assert.Equal(HttpStatusCode.OK, res.StatusCode); + } + +} internal interface IDependency { diff --git a/test/Carter.Tests/TestModule.cs b/test/Carter.Tests/TestModule.cs index 5c7c044..1f998ce 100644 --- a/test/Carter.Tests/TestModule.cs +++ b/test/Carter.Tests/TestModule.cs @@ -106,4 +106,22 @@ public void AddRoutes(IEndpointRouteBuilder app) app.MapPut("/endpointfilter", (IDependency dependency, TestModel testModel) => "PUT"); } } + public static class NestedTestModule + { + public class TestModule : ICarterModule + { + private Guid instanceId; + + public TestModule() + { + this.instanceId = Guid.NewGuid(); + } + + public void AddRoutes(IEndpointRouteBuilder app) + { + app.MapGet("/nested", async (HttpContext ctx) => { await ctx.Response.WriteAsync("Hello Nested"); }); + } + } + } + } diff --git a/test/Carter.Tests/TypeExtensionTests.cs b/test/Carter.Tests/TypeExtensionTests.cs index 860ec05..6ea6a0c 100644 --- a/test/Carter.Tests/TypeExtensionTests.cs +++ b/test/Carter.Tests/TypeExtensionTests.cs @@ -10,7 +10,7 @@ public class TypeExtensionTests [Fact] public void MustDeriveFrom_TypesDerivingFrom_WontThrow() { - var types = new[] { typeof(TestModule), typeof(StreamModule) }.ToArray(); + var types = new[] { typeof(TestModule), typeof(StreamModule), typeof(NestedTestModule.TestModule) }.ToArray(); types.MustDeriveFrom(); }