The Wayback Machine - https://web.archive.org/web/20220126145206/https://github.com/dotnet/aspnetcore/issues/39777
Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

WebApplicationFactory<T>.CreateClient() fails when using app.Urls.Add() in minimal api. #39777

Open
1 task done
drch- opened this issue Jan 26, 2022 · 0 comments
Open
1 task done

Comments

@drch-
Copy link

@drch- drch- commented Jan 26, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A minimal api that uses app.Urls.Add() throws an exception when under test with a WebApplicationFactory. The following example shows a minimal repro.

Removing the app.Urls.Add() works fine and the test passes.

// Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
app.Urls.Add($"http://0.0.0.0:{port}");

app.MapGet("/", () => "hello world");
app.Run();

public partial class Program { }
// UnitTest1.cs xunit v2.4.1
[Fact]
public async Task TestAppRunning()
{
    var app = new WebApplicationFactory<Program>();
    var client = app.CreateClient(); // throws InvalidOperationException
    var response = await client.GetAsync("/");
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

[xUnit.net 00:00:01.02]     test.UnitTest1.TestAppRunning [FAIL]
  Failed test.UnitTest1.TestAppRunning [94 ms]
  Error Message:
   System.InvalidOperationException : IServerAddressesFeature could not be found.
  Stack Trace:
     at Microsoft.AspNetCore.Builder.WebApplication.get_Urls()
   at Program.<Main>$(String[] args) in Program.cs:line 5
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Testing.DeferredHostBuilder.DeferredHost.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Start(IHost host)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateHost(IHostBuilder builder)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient()
   at test.UnitTest1.TestAppRunning() in UnitTest1.cs:line 23
--- End of stack trace from previous location ---

.NET Version

6.0.101

Anything else?

.NET SDK (reflecting any global.json):
Version: 6.0.101
Commit: ef49f6213a

Runtime Environment:
OS Name: debian
OS Platform: Linux
RID: linux-x64

Host (useful for support):
Version: 6.0.1
Commit: 3a25a7f1cc

.NET SDKs installed:
6.0.101

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.1
Microsoft.NETCore.App 6.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant