From b509a54c008569aa5820a37ad8356c3a21918724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20N=C3=A4geli?= Date: Mon, 16 Dec 2024 15:49:03 +0100 Subject: [PATCH] Allow to add sections to layouts --- Modules/Layouting/Provider/LayoutBuilder.cs | 14 +++++++++ .../Modules/Layouting/SectionTests.cs | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 Testing/Acceptance/Modules/Layouting/SectionTests.cs diff --git a/Modules/Layouting/Provider/LayoutBuilder.cs b/Modules/Layouting/Provider/LayoutBuilder.cs index 076b1366..aacc598e 100644 --- a/Modules/Layouting/Provider/LayoutBuilder.cs +++ b/Modules/Layouting/Provider/LayoutBuilder.cs @@ -89,6 +89,20 @@ public LayoutBuilder Add(IHandlerBuilder handler) return this; } + /// + /// Creates a new layout and registers it at the given path. + /// + /// The path of the section to be added + /// The newly created section + public LayoutBuilder AddSection(string section) + { + var child = Layout.Create(); + + Add(section, child); + + return child; + } + public LayoutBuilder Add(IConcernBuilder concern) { _Concerns.Add(concern); diff --git a/Testing/Acceptance/Modules/Layouting/SectionTests.cs b/Testing/Acceptance/Modules/Layouting/SectionTests.cs new file mode 100644 index 00000000..d1bb7c4a --- /dev/null +++ b/Testing/Acceptance/Modules/Layouting/SectionTests.cs @@ -0,0 +1,31 @@ +using System.Net; +using GenHTTP.Modules.IO; +using GenHTTP.Modules.Layouting; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace GenHTTP.Testing.Acceptance.Modules.Layouting; + +[TestClass] +public class SectionTests +{ + + [TestMethod] + [MultiEngineTest] + public async Task TestSection(TestEngine engine) + { + var app = Layout.Create(); + + var section = app.AddSection("section"); + + section.Index(Content.From(Resource.FromString("Hello World"))); + + await using var host = await TestHost.RunAsync(app, engine: engine); + + using var response = await host.GetResponseAsync("/section/"); + + await response.AssertStatusAsync(HttpStatusCode.OK); + + Assert.AreEqual("Hello World", await response.GetContentAsync()); + } + +}