From 02f8c4655590bb1281c175e253741350554bd5e8 Mon Sep 17 00:00:00 2001 From: krishanthaudayakumara Date: Fri, 29 Aug 2025 15:49:15 +0530 Subject: [PATCH 1/4] feat: migrate ModelBindingFixture to TestWebApplicationFactory - Replace obsolete TestServerBuilder with TestWebApplicationFactory - Implement constructor injection pattern with IClassFixture - Utilize factory's built-in MockClientHandler for HTTP mocking - Modernize test structure to follow current ASP.NET Core testing practices - Maintain existing test coverage while improving test isolation --- .../Fixtures/Binding/ModelBindingFixture.cs | 50 +------ .../Binding/TestModelBindingProgram.cs | 37 +++++ .../Fixtures/Pages/TestPagesProgram.cs | 6 +- .../TestWebApplicationFactory.cs | 137 +++++++++++++++++- 4 files changed, 184 insertions(+), 46 deletions(-) create mode 100644 tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs index 91cb431..c8246bd 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs @@ -1,52 +1,25 @@ using System.Net; using AwesomeAssertions; -using Microsoft.AspNetCore.TestHost; -using Sitecore.AspNetCore.SDK.AutoFixture.Mocks; -using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions; using Sitecore.AspNetCore.SDK.LayoutService.Client.Response; -using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; using Sitecore.AspNetCore.SDK.TestData; using Xunit; namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Binding; -public class ModelBindingFixture : IDisposable +public class ModelBindingFixture(TestWebApplicationFactory factory) : IClassFixture> { - private readonly TestServer _server; - private readonly MockHttpMessageHandler _mockClientHandler; - private readonly Uri _layoutServiceUri = new("http://layout.service"); - - public ModelBindingFixture() - { - TestServerBuilder testHostBuilder = new(); - _mockClientHandler = new MockHttpMessageHandler(); - testHostBuilder - .ConfigureServices(builder => - { - builder - .AddSitecoreLayoutService() - .AddHttpHandler("mock", _ => new HttpClient(_mockClientHandler) { BaseAddress = _layoutServiceUri }) - .AsDefaultHandler(); - builder.AddSitecoreRenderingEngine(); - }) - .Configure(app => - { - app.UseSitecoreRenderingEngine(); - }); - - _server = testHostBuilder.BuildServer(new Uri("http://localhost")); - } + private readonly TestWebApplicationFactory _factory = factory; [Fact] public async Task SitecoreRouteModelBinding_ReturnsCorrectData() { - _mockClientHandler.Responses.Push(new HttpResponseMessage + _factory.MockClientHandler.Responses.Push(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder)) }); - HttpClient client = _server.CreateClient(); + HttpClient client = _factory.CreateClient(); string response = await client.GetStringAsync("WithBoundSitecoreRoute"); // assert that the SitecoreRouteProperty attribute binding worked @@ -62,13 +35,13 @@ public async Task SitecoreRouteModelBinding_ReturnsCorrectData() [Fact] public async Task SitecoreContextModelBinding_ReturnsCorrectData() { - _mockClientHandler.Responses.Push(new HttpResponseMessage + _factory.MockClientHandler.Responses.Push(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder)) }); - HttpClient client = _server.CreateClient(); + HttpClient client = _factory.CreateClient(); string response = await client.GetStringAsync("WithBoundSitecoreContext"); // assert that the SitecoreContextProperty attribute binding worked @@ -82,23 +55,16 @@ public async Task SitecoreContextModelBinding_ReturnsCorrectData() [Fact] public async Task SitecoreResponseModelBinding_ReturnsCorrectData() { - _mockClientHandler.Responses.Push(new HttpResponseMessage + _factory.MockClientHandler.Responses.Push(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder)) }); - HttpClient client = _server.CreateClient(); + HttpClient client = _factory.CreateClient(); string response = await client.GetStringAsync("WithBoundSitecoreResponse"); // assert that the SitecoreLayoutResponse attribute binding worked response.Should().Contain(TestConstants.DatabaseName); } - - public void Dispose() - { - _server.Dispose(); - _mockClientHandler.Dispose(); - GC.SuppressFinalize(this); - } } \ No newline at end of file diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs new file mode 100644 index 0000000..190ccd2 --- /dev/null +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Hosting; +using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions; +using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; + +namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Binding; + +/// +/// Test program class for model binding scenarios. +/// +public class TestModelBindingProgram +{ + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureServices(services => + { + services.AddRouting() + .AddMvc(); + + services.AddSitecoreLayoutService() + .AddHttpHandler("mock", _ => new HttpClient() { BaseAddress = new Uri("http://layout.service") }) + .AsDefaultHandler(); + + services.AddSitecoreRenderingEngine(); + }) + .Configure(app => + { + app.UseRouting(); + app.UseSitecoreRenderingEngine(); + app.UseEndpoints(endpoints => + { + endpoints.MapDefaultControllerRoute(); + }); + }); + }); +} diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs index 7b65ba8..88d4a26 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs @@ -5,7 +5,11 @@ using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; using Sitecore.AspNetCore.SDK.TestData; -WebApplicationBuilder builder = WebApplication.CreateBuilder(args); +WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions +{ + Args = args, + ContentRootPath = Directory.GetCurrentDirectory() +}); builder.Services.AddRouting() .AddMvc(); diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs index 64f1474..98d6975 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs @@ -1,8 +1,21 @@ -using GraphQL.Client.Abstractions; +using System.IO; +using GraphQL.Client.Abstractions; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Options; using NSubstitute; +using Sitecore.AspNetCore.SDK.AutoFixture.Mocks; +using Sitecore.AspNetCore.SDK.LayoutService.Client.Configuration; +using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions; +using Sitecore.AspNetCore.SDK.LayoutService.Client.Request.Handlers; +using Sitecore.AspNetCore.SDK.LayoutService.Client.Serialization; +using Sitecore.AspNetCore.SDK.Pages.Configuration; +using Sitecore.AspNetCore.SDK.Pages.Extensions; +using Sitecore.AspNetCore.SDK.Pages.Request.Handlers.GraphQL; +using Sitecore.AspNetCore.SDK.Pages.Services; +using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; +using Sitecore.AspNetCore.SDK.TestData; namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests { @@ -12,14 +25,132 @@ public class TestWebApplicationFactory { public IGraphQLClient MockGraphQLClient { get; set; } = Substitute.For(); + public MockHttpMessageHandler MockClientHandler { get; set; } = new(); + + public Uri LayoutServiceUri { get; set; } = new("http://layout.service"); + protected override void ConfigureWebHost(IWebHostBuilder builder) { - builder.UseContentRoot(Path.GetFullPath(Directory.GetCurrentDirectory())) + builder.UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureTestServices(services => { - ServiceProvider serviceProvider = services.BuildServiceProvider(); ServiceDescriptor descriptor = new(typeof(IGraphQLClient), MockGraphQLClient); services.Replace(descriptor); + + // Configure mock layout service handlers + services.PostConfigure(options => + { + // Clear all existing handlers and set up our mock as the default + options.HandlerRegistry.Clear(); + options.HandlerRegistry["mock"] = serviceProvider => + { + HttpClient client = new HttpClient(MockClientHandler) { BaseAddress = LayoutServiceUri }; + + // Create mock options since IOptionsSnapshot is scoped + var mockOptions = Substitute.For>(); + var handlerOptions = new HttpLayoutRequestHandlerOptions(); + mockOptions.Get(Arg.Any()).Returns(handlerOptions); + + return new HttpLayoutRequestHandler( + client, + serviceProvider.GetRequiredService(), + mockOptions, + serviceProvider.GetRequiredService>()); + }; + + // For Pages tests, also add a "pages" handler + if (typeof(T).Name.Contains("Pages")) + { + options.HandlerRegistry["pages"] = serviceProvider => + { + var graphQLClient = serviceProvider.GetRequiredService(); + return new GraphQLEditingServiceHandler( + graphQLClient, + serviceProvider.GetRequiredService(), + serviceProvider.GetRequiredService>(), + serviceProvider.GetRequiredService()); + }; + options.DefaultHandler = "pages"; + } + else + { + options.DefaultHandler = "mock"; + } + }); + + // Check if we're configuring for Pages tests + if (typeof(T).Name.Contains("Pages")) + { + // Pages-specific configuration + services.AddRouting() + .AddMvc(); + + services.AddSitecoreLayoutService() + .AddSitecorePagesHandler(); + + services.AddSitecoreRenderingEngine(options => + { + options.AddDefaultPartialView("_ComponentNotFound"); + }) + .WithSitecorePages(TestConstants.ContextId, options => { options.EditingSecret = TestConstants.JssEditingSecret; }); + + // Configure PagesOptions for the middleware + services.Configure(options => + { + options.ConfigEndpoint = TestConstants.ConfigRoute; + }); + } + else + { + // Standard configuration for other tests + services.AddRouting() + .AddMvc(); + + services.AddSitecoreLayoutService() + .AddHttpHandler("mock", _ => new HttpClient() { BaseAddress = new Uri("http://layout.service") }) + .AsDefaultHandler(); + + services.AddSitecoreRenderingEngine(); + } + }) + .Configure(app => + { + // Check if we're configuring for Pages tests + if (typeof(T).Name.Contains("Pages")) + { + // Pages-specific middleware pipeline + app.UseMiddleware(); + app.UseRouting(); + app.UseEndpoints(endpoints => + { + // Map the config endpoint to PagesSetup controller + endpoints.MapControllerRoute( + name: "pagesconfig", + pattern: "api/editing/config", + defaults: new { controller = "PagesSetup", action = "Config" }); + + // Map the render endpoint to PagesSetup controller + endpoints.MapControllerRoute( + name: "pagesrender", + pattern: "api/editing/render", + defaults: new { controller = "PagesSetup", action = "Render" }); + + // Map the default route to Pages controller + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller=Pages}/{action=Index}"); + }); + } + else + { + // Standard middleware pipeline + app.UseRouting(); + app.UseSitecoreRenderingEngine(); + app.UseEndpoints(configure => + { + configure.MapDefaultControllerRoute(); + }); + } }); } } From 0620404bb69f9d60ff79a2c65b7ff369ba8c956f Mon Sep 17 00:00:00 2001 From: krishanthaudayakumara Date: Fri, 29 Aug 2025 15:56:57 +0530 Subject: [PATCH 2/4] refactor: modernize ModelBindingFixture with primary constructor syntax and remove unnecessary spaces in TestWebApplicationFactory --- .../Fixtures/Binding/ModelBindingFixture.cs | 14 ++++++-------- .../TestWebApplicationFactory.cs | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs index c8246bd..88ca2a5 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ModelBindingFixture.cs @@ -8,18 +8,16 @@ namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Bin public class ModelBindingFixture(TestWebApplicationFactory factory) : IClassFixture> { - private readonly TestWebApplicationFactory _factory = factory; - [Fact] public async Task SitecoreRouteModelBinding_ReturnsCorrectData() { - _factory.MockClientHandler.Responses.Push(new HttpResponseMessage + factory.MockClientHandler.Responses.Push(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder)) }); - HttpClient client = _factory.CreateClient(); + HttpClient client = factory.CreateClient(); string response = await client.GetStringAsync("WithBoundSitecoreRoute"); // assert that the SitecoreRouteProperty attribute binding worked @@ -35,13 +33,13 @@ public async Task SitecoreRouteModelBinding_ReturnsCorrectData() [Fact] public async Task SitecoreContextModelBinding_ReturnsCorrectData() { - _factory.MockClientHandler.Responses.Push(new HttpResponseMessage + factory.MockClientHandler.Responses.Push(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder)) }); - HttpClient client = _factory.CreateClient(); + HttpClient client = factory.CreateClient(); string response = await client.GetStringAsync("WithBoundSitecoreContext"); // assert that the SitecoreContextProperty attribute binding worked @@ -55,13 +53,13 @@ public async Task SitecoreContextModelBinding_ReturnsCorrectData() [Fact] public async Task SitecoreResponseModelBinding_ReturnsCorrectData() { - _factory.MockClientHandler.Responses.Push(new HttpResponseMessage + factory.MockClientHandler.Responses.Push(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder)) }); - HttpClient client = _factory.CreateClient(); + HttpClient client = factory.CreateClient(); string response = await client.GetStringAsync("WithBoundSitecoreResponse"); // assert that the SitecoreLayoutResponse attribute binding worked diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs index 98d6975..01434e2 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs @@ -45,19 +45,19 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) options.HandlerRegistry["mock"] = serviceProvider => { HttpClient client = new HttpClient(MockClientHandler) { BaseAddress = LayoutServiceUri }; - + // Create mock options since IOptionsSnapshot is scoped var mockOptions = Substitute.For>(); var handlerOptions = new HttpLayoutRequestHandlerOptions(); mockOptions.Get(Arg.Any()).Returns(handlerOptions); - + return new HttpLayoutRequestHandler( client, serviceProvider.GetRequiredService(), mockOptions, serviceProvider.GetRequiredService>()); }; - + // For Pages tests, also add a "pages" handler if (typeof(T).Name.Contains("Pages")) { From 19aa7cb9491c55c17d7145736c1c6c4e0acd3394 Mon Sep 17 00:00:00 2001 From: krishanthaudayakumara Date: Mon, 1 Sep 2025 12:14:36 +0530 Subject: [PATCH 3/4] replace string matching with interface-based test detection --- .../Fixtures/Binding/TestModelBindingProgram.cs | 3 ++- .../Fixtures/Pages/TestPagesProgram.cs | 3 ++- .../Interfaces/IPagesTestProgram.cs | 8 ++++++++ .../Interfaces/IStandardTestProgram.cs | 8 ++++++++ .../TestWebApplicationFactory.cs | 9 ++++++--- 5 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IPagesTestProgram.cs create mode 100644 tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IStandardTestProgram.cs diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs index 190ccd2..dd8e06e 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/TestModelBindingProgram.cs @@ -1,13 +1,14 @@ using Microsoft.AspNetCore.Hosting; using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions; using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; +using Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Interfaces; namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Binding; /// /// Test program class for model binding scenarios. /// -public class TestModelBindingProgram +public class TestModelBindingProgram : IStandardTestProgram { public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs index 88d4a26..d4a8049 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Pages/TestPagesProgram.cs @@ -3,6 +3,7 @@ using Sitecore.AspNetCore.SDK.Pages.Configuration; using Sitecore.AspNetCore.SDK.Pages.Extensions; using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; +using Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Interfaces; using Sitecore.AspNetCore.SDK.TestData; WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions @@ -43,6 +44,6 @@ /// /// Partial class allowing this TestProgram to be created by a WebApplicationFactory for integration testing. /// -public partial class TestPagesProgram +public partial class TestPagesProgram : IPagesTestProgram { } \ No newline at end of file diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IPagesTestProgram.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IPagesTestProgram.cs new file mode 100644 index 0000000..341b3df --- /dev/null +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IPagesTestProgram.cs @@ -0,0 +1,8 @@ +namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Interfaces; + +/// +/// Marker interface to identify test programs that require Pages functionality. +/// +public interface IPagesTestProgram +{ +} diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IStandardTestProgram.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IStandardTestProgram.cs new file mode 100644 index 0000000..faa4b96 --- /dev/null +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Interfaces/IStandardTestProgram.cs @@ -0,0 +1,8 @@ +namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Interfaces; + +/// +/// Marker interface to identify test programs that require standard Sitecore functionality without Pages. +/// +public interface IStandardTestProgram +{ +} diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs index 01434e2..14ca6f1 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs @@ -15,6 +15,7 @@ using Sitecore.AspNetCore.SDK.Pages.Request.Handlers.GraphQL; using Sitecore.AspNetCore.SDK.Pages.Services; using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; +using Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Interfaces; using Sitecore.AspNetCore.SDK.TestData; namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests @@ -23,6 +24,8 @@ public class TestWebApplicationFactory : WebApplicationFactory where T : class { + private bool IsPagesTest => typeof(IPagesTestProgram).IsAssignableFrom(typeof(T)); + public IGraphQLClient MockGraphQLClient { get; set; } = Substitute.For(); public MockHttpMessageHandler MockClientHandler { get; set; } = new(); @@ -59,7 +62,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) }; // For Pages tests, also add a "pages" handler - if (typeof(T).Name.Contains("Pages")) + if (IsPagesTest) { options.HandlerRegistry["pages"] = serviceProvider => { @@ -79,7 +82,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) }); // Check if we're configuring for Pages tests - if (typeof(T).Name.Contains("Pages")) + if (IsPagesTest) { // Pages-specific configuration services.AddRouting() @@ -116,7 +119,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) .Configure(app => { // Check if we're configuring for Pages tests - if (typeof(T).Name.Contains("Pages")) + if (IsPagesTest) { // Pages-specific middleware pipeline app.UseMiddleware(); From 41ebc59bbfb25f8c41c33b5e03cfaf7a4dce71f1 Mon Sep 17 00:00:00 2001 From: krishanthaudayakumara Date: Mon, 1 Sep 2025 14:10:30 +0530 Subject: [PATCH 4/4] Remove unnecessary comments --- .../TestWebApplicationFactory.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs index 14ca6f1..754c932 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/TestWebApplicationFactory.cs @@ -25,7 +25,7 @@ public class TestWebApplicationFactory where T : class { private bool IsPagesTest => typeof(IPagesTestProgram).IsAssignableFrom(typeof(T)); - + public IGraphQLClient MockGraphQLClient { get; set; } = Substitute.For(); public MockHttpMessageHandler MockClientHandler { get; set; } = new(); @@ -43,13 +43,11 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) // Configure mock layout service handlers services.PostConfigure(options => { - // Clear all existing handlers and set up our mock as the default options.HandlerRegistry.Clear(); options.HandlerRegistry["mock"] = serviceProvider => { HttpClient client = new HttpClient(MockClientHandler) { BaseAddress = LayoutServiceUri }; - // Create mock options since IOptionsSnapshot is scoped var mockOptions = Substitute.For>(); var handlerOptions = new HttpLayoutRequestHandlerOptions(); mockOptions.Get(Arg.Any()).Returns(handlerOptions); @@ -61,7 +59,6 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) serviceProvider.GetRequiredService>()); }; - // For Pages tests, also add a "pages" handler if (IsPagesTest) { options.HandlerRegistry["pages"] = serviceProvider => @@ -81,10 +78,8 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) } }); - // Check if we're configuring for Pages tests if (IsPagesTest) { - // Pages-specific configuration services.AddRouting() .AddMvc(); @@ -105,7 +100,6 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) } else { - // Standard configuration for other tests services.AddRouting() .AddMvc(); @@ -118,10 +112,8 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) }) .Configure(app => { - // Check if we're configuring for Pages tests if (IsPagesTest) { - // Pages-specific middleware pipeline app.UseMiddleware(); app.UseRouting(); app.UseEndpoints(endpoints => @@ -146,7 +138,6 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) } else { - // Standard middleware pipeline app.UseRouting(); app.UseSitecoreRenderingEngine(); app.UseEndpoints(configure =>