Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[WIP] Decouple WebApplicationFactory from TestServer implementation #60370

Open
mkArtakMSFT opened this issue Feb 13, 2025 · 0 comments
Open
Assignees
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

Comments

@mkArtakMSFT
Copy link
Member

Background and Motivation

We have a long-standing popular issue tracking improvement of automated browser testing with real server. Part of the ask is to decouple the WebApplicationFactory from the TestServer implementation, as they're currently [tightly coupled] (

).

Unfortunately, the TestServer is also exposed in WebApplicationFactory via a public property Server. Hence, decoupling requires an API change proposed below.

The second change is exposing the initialization logic publicly. Without this change, customers had to call the CreateClient() API which would internally initialize the server but wouldn't use the returned HttpClient instance. This will make it more intuitive and avoid creating unnecessary objects. Here is a screenshot from a blogpost pointing out this odd usage pattern:

Image

With the proposed changes, customers can now simply call server.Initialize() instead which is more intuitive.

Proposed API

New abstraction / interface to depend on instead of the TestServer implementation.

namespace Microsoft.AspNetCore.TestHost;

public interface ITestServer : IServer
{
    IWebHost Host { get; }
    HttpMessageHandler CreateHandler();
    HttpClient CreateClient();
}

- public class TestServer : IServer
+ public class TestServer : ITestServer

Usage / API change in WebApplicationFactory:

namespace Microsoft.AspNetCore.Mvc.Testing;

public partial class WebApplicationFactory<TEntryPoint> : IDisposable, IAsyncDisposable where TEntryPoint : class
{
    [Obsolete("This property is obsolete. Consider utilizing the TestServer property instead.")]
    public TestServer? Server { get; }

   +public ITestServer? TestServer { get; }

   - private void EnsureServer()
   + public void Initialize()

   - protected virtual TestServer CreateServer(IWebHostBuilder builder)
   + protected virtual ITestServer CreateServer(IWebHostBuilder builder)
}

Usage Examples

Alternative Designs

Risks

@mkArtakMSFT mkArtakMSFT added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Feb 13, 2025
@mkArtakMSFT mkArtakMSFT self-assigned this Feb 13, 2025
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Feb 13, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
Projects
None yet
Development

No branches or pull requests

1 participant