Skip to content

Commit

Permalink
nested test module added and code fixed for nested classes. (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkelMato authored May 25, 2024
1 parent 8456dd0 commit 35f0a0c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 9 deletions.
16 changes: 16 additions & 0 deletions samples/CarterSample/Features/Home/NestedHomeModule.cs
Original file line number Diff line number Diff line change
@@ -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");
});
}
}
}
2 changes: 1 addition & 1 deletion src/Carter/CarterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private static IEnumerable<Type> GetNewModules(CarterConfigurator carterConfigur
!t.IsAbstract &&
typeof(ICarterModule).IsAssignableFrom(t) &&
t != typeof(ICarterModule) &&
t.IsPublic
(t.IsPublic || t.IsNestedPublic)
));

carterConfigurator.ModuleTypes.AddRange(modules);
Expand Down
60 changes: 53 additions & 7 deletions test/Carter.Tests/RouteExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public RouteExtensionsTests(ITestOutputHelper outputHelper)
x.AddRouting();
x.AddCarter(configurator: c =>
{
c.WithModule<TestModule>();
c.WithValidator<TestModelValidator>();
}
);
{
c.WithModule<TestModule>();
c.WithValidator<TestModelValidator>();
});
})
.Configure(x =>
{
Expand Down Expand Up @@ -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<IDependency, Dependency>();
x.AddRouting();
x.AddCarter(configurator: c => {
c.WithValidator<TestModelValidator>();
}
);
})
.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
{
Expand Down
18 changes: 18 additions & 0 deletions test/Carter.Tests/TestModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,22 @@ public void AddRoutes(IEndpointRouteBuilder app)
app.MapPut<TestModel>("/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"); });
}
}
}

}
2 changes: 1 addition & 1 deletion test/Carter.Tests/TypeExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICarterModule>();
}

Expand Down

0 comments on commit 35f0a0c

Please sign in to comment.