Skip to content

[WebApplicationFactory] UseKestrel feedback #62332

Open
@martincostello

Description

@martincostello

Consume nightly builds now that #62231 is resolved, I hit a minor rough edge using the new functionality.

SDK version: 10.0.100-preview.6.25311.101

I'm not sure whether this is a bug or feature request or not, but I figured I'd log something about it and it can be decided if it's something I need to continue to workaround, or if it's something that can be improved.


First I have some tests that use vanilla WebApplicationFactory<T> to test in-memory. In these tests, I always want HTTPS to be used, so I disable redirects and change the base address to use HTTPS:

public AppFixture()
{
    ClientOptions.AllowAutoRedirect = false;
    ClientOptions.BaseAddress = new Uri("https://localhost");
}

This is just for convenience so that I can use CreateClient() in most places.

In a separate fixture derived from that fixture, I enable Kestrel to do browser-based tests:

public HttpServerFixture() => UseKestrel(0);

This however causes tests using CreateClient() with that fixture to fail as they attempt to connect to localhost:443 instead of the port assigned to Kestrel.

This is because of this line:

if (_useKestrel && object.ReferenceEquals(client.BaseAddress, WebApplicationFactoryClientOptions.DefaultBaseAddres))

Because the option was changed, Kestrel's endpoint is no longer applied by default.

The object identity use for comparison is internal, so to reset things back to get the previous behaviour is to do this:

public HttpServerFixture()
{
    ClientOptions.BaseAddress = new Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions().BaseAddress;
    UseKestrel(0);
}

It's ugly, but it works.

A suggested change here would be to check for equality to http://localhost and https://localhost (i.e. HTTP and HTTPS default ports) instead of reference equality on a specific Uri.

The PR where I tried this out can be found here: martincostello/costellobot#2334

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-mvc-testingMVC testing package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions