Can't configure host url on WebApplicationFactory
Asked Answered
G

1

2

I am trying to host my api project using WebApplicationFactory. I have overriden ConfigureWebHost and added my desired url in UseUrls() method. I am setting the app to host on http://localhost:5000, but it is inaccessible from browser. The only way to call endpoints in the host is via httpClient created by CreateClient() method in WebApplicationFactory instance.

public class ApiHubFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
    {
        public string HostUrl { get; set; } = "https://localhost:5001";
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.UseUrls("http://localhost:5000", HostUrl);

            //Adding fake authentication scheme to bypass JWT authentication
            builder.ConfigureServices(services =>
            {
                services.AddAuthentication(options =>
                    {
                        options.DefaultAuthenticateScheme = TestAuthHandler.AuthenticationScheme;
                        options.DefaultScheme = TestAuthHandler.AuthenticationScheme;
                        options.DefaultChallengeScheme = TestAuthHandler.AuthenticationScheme;
                    })
                    .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.AuthenticationScheme, options => { });
            });
        }
}

I have tried to find out on what url the host is starting looking in httpclient. Despite I have set it to be http://localhost:5000, it is 127.0.0.1:80, which is also inaccessible from browser.

Also I have tried using settings, the answer is from How to configure url on Host created by WebApplicationFactory?

public class CustomWebApplicationFactory<TStartup>
    : WebApplicationFactory<TStartup> where TStartup : class
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        // Notice there is no `--` prefix in "urls"
        builder.UseSetting("urls", "http://localhost:1234");

        // other stuff as usual
    }
}
Gender answered 28/6, 2023 at 12:59 Comment(2)
have you tried with https?Sandman
yes, it doesn't helpGender
C
1

The thing is that WebApplicationFactory crates a whole application in memory, that is accessible only with HttpCLient that is created with the factory.

WebApplicationFactory<Program> _factory=...;
var client = _factory.CreateClient();

There is no other way to access your API. If this is what you really need then you can create just a regular WebHost like you are doing in your Program file and run an API in Kestrel.

public sealed class ApiTestHost: IDisposable
{
    public const string HostUrl = "http://localhost:54318";
    private readonly IWebHost host;

    public ApiTestHost()
    {
        host = WebHost.CreateDefaultBuilder()
            .UseEnvironment("Development")
            .UseStartup<Startup>()
            .UseKestrel()
            .UseUrls(HostUrl )
            .Build();
        host.Start();
    }

    public void Dispose()
    {
        host?.Dispose();
    }
}

Then you can just create an instance of ApiTestHost in every test but better to use smth like IClassFixture

If you want to stay with WebApplicationFactory you can refer to this DOC

Charbonneau answered 10/10, 2023 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.