From a61a717b3d2f07e9d08737fce0c08c001527eb47 Mon Sep 17 00:00:00 2001 From: Artak Mkrtchyan Date: Thu, 6 Feb 2025 18:53:00 -0800 Subject: [PATCH 1/3] - Created an ITestServer abstraction to replace the TestServer dependency with from in the WebApplicationFactory. - Updated the WebApplicationFactory to depend on the ITestServer abstraction --- src/Hosting/TestHost/src/ITestServer.cs | 31 ++++++++++++ .../TestHost/src/PublicAPI.Unshipped.txt | 4 ++ src/Hosting/TestHost/src/TestServer.cs | 2 +- src/Mvc/Mvc.Testing/src/PublicAPI.Shipped.txt | 2 - .../Mvc.Testing/src/PublicAPI.Unshipped.txt | 4 ++ src/Mvc/Mvc.Testing/src/Resources.resx | 5 +- .../Mvc.Testing/src/WebApplicationFactory.cs | 47 ++++++++++++++----- .../Mvc.FunctionalTests/ApiExplorerTest.cs | 2 +- .../ComponentRenderingFunctionalTests.cs | 2 +- .../Infrastructure/MvcTestFixture.cs | 2 +- .../TestingInfrastructureInheritanceTests.cs | 2 +- 11 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 src/Hosting/TestHost/src/ITestServer.cs diff --git a/src/Hosting/TestHost/src/ITestServer.cs b/src/Hosting/TestHost/src/ITestServer.cs new file mode 100644 index 000000000000..6940074601a7 --- /dev/null +++ b/src/Hosting/TestHost/src/ITestServer.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Net.Http; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Server; + +namespace Microsoft.AspNetCore.TestHost; + +/// +/// A contract for a test server implementation. +/// +public interface ITestServer : IServer +{ + /// + /// Gets the web host associated with the test server. + /// + IWebHost Host { get; } + + /// + /// Creates a new for processing HTTP requests against the test server. + /// + /// A new instance. + HttpMessageHandler CreateHandler(); + + /// + /// Creates a new for processing HTTP requests against the test server. + /// + /// + HttpClient CreateClient(); +} diff --git a/src/Hosting/TestHost/src/PublicAPI.Unshipped.txt b/src/Hosting/TestHost/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..96a756e2b17a 100644 --- a/src/Hosting/TestHost/src/PublicAPI.Unshipped.txt +++ b/src/Hosting/TestHost/src/PublicAPI.Unshipped.txt @@ -1 +1,5 @@ #nullable enable +Microsoft.AspNetCore.TestHost.ITestServer +Microsoft.AspNetCore.TestHost.ITestServer.CreateClient() -> System.Net.Http.HttpClient! +Microsoft.AspNetCore.TestHost.ITestServer.CreateHandler() -> System.Net.Http.HttpMessageHandler! +Microsoft.AspNetCore.TestHost.ITestServer.Host.get -> Microsoft.AspNetCore.Hosting.IWebHost! diff --git a/src/Hosting/TestHost/src/TestServer.cs b/src/Hosting/TestHost/src/TestServer.cs index 1c9c295406ee..72550e58c6a4 100644 --- a/src/Hosting/TestHost/src/TestServer.cs +++ b/src/Hosting/TestHost/src/TestServer.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.TestHost; /// /// An implementation for executing tests. /// -public class TestServer : IServer +public class TestServer : ITestServer { private readonly IWebHost? _hostInstance; private bool _disposed; diff --git a/src/Mvc/Mvc.Testing/src/PublicAPI.Shipped.txt b/src/Mvc/Mvc.Testing/src/PublicAPI.Shipped.txt index cd734b7284fb..46f1e43e4eac 100644 --- a/src/Mvc/Mvc.Testing/src/PublicAPI.Shipped.txt +++ b/src/Mvc/Mvc.Testing/src/PublicAPI.Shipped.txt @@ -16,7 +16,6 @@ Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.CreateDefaul Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.CreateDefaultClient(System.Uri! baseAddress, params System.Net.Http.DelegatingHandler![]! handlers) -> System.Net.Http.HttpClient! Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.Dispose() -> void Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.Factories.get -> System.Collections.Generic.IReadOnlyList!>! -Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.Server.get -> Microsoft.AspNetCore.TestHost.TestServer! Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.WebApplicationFactory() -> void Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.WithWebHostBuilder(System.Action! configuration) -> Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory! Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions @@ -41,7 +40,6 @@ virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.Conf virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> void virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.CreateHost(Microsoft.Extensions.Hosting.IHostBuilder! builder) -> Microsoft.Extensions.Hosting.IHost! virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.CreateHostBuilder() -> Microsoft.Extensions.Hosting.IHostBuilder? -virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.CreateServer(Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> Microsoft.AspNetCore.TestHost.TestServer! virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.CreateWebHostBuilder() -> Microsoft.AspNetCore.Hosting.IWebHostBuilder? virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.Dispose(bool disposing) -> void virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.DisposeAsync() -> System.Threading.Tasks.ValueTask diff --git a/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt b/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..68910e215fc2 100644 --- a/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt +++ b/src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt @@ -1 +1,5 @@ #nullable enable +Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.Initialize() -> void +Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.Server.get -> Microsoft.AspNetCore.TestHost.TestServer? +Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.TestServer.get -> Microsoft.AspNetCore.TestHost.ITestServer? +virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory.CreateServer(Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> Microsoft.AspNetCore.TestHost.ITestServer! diff --git a/src/Mvc/Mvc.Testing/src/Resources.resx b/src/Mvc/Mvc.Testing/src/Resources.resx index 84ce491ca5ed..1c1ec82845da 100644 --- a/src/Mvc/Mvc.Testing/src/Resources.resx +++ b/src/Mvc/Mvc.Testing/src/Resources.resx @@ -1,4 +1,4 @@ - +