Skip to content

Commit

Permalink
cleanup and add link
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Oct 20, 2023
1 parent 859eef1 commit ecda891
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Examples/Razor/Razor.Playwright/TestSetup/RazorSut.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Hosting;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Mvc.Testing;
Expand Down Expand Up @@ -67,20 +68,22 @@ protected override IHost CreateHost(IHostBuilder builder)
loggingBuilder.Services.AddSingleton(_loggerProvider);
});

// Taken and modified from https://danieldonbavand.com/2022/06/13/using-playwright-with-the-webapplicationfactory-to-test-a-blazor-application/
// Create the host for TestServer now before we
// modify the builder to use Kestrel instead.
var testHost = builder.Build();

// Modify the host builder to use Kestrel instead
// of TestServer so we can listen on a real address.
builder.ConfigureWebHost((p) => p.UseKestrel());
builder.ConfigureWebHost(p => p.UseKestrel());

// Create and start the Kestrel server before the test server,
// otherwise due to the way the deferred host builder works
// for minimal hosting, the server will not get "initialized
// enough" for the address it is listening on to be available.
_host = builder.Build();
_host.Start();
_pooledDatabase.EnsureInitialized(_host);

// Extract the selected dynamic port out of the Kestrel server
// and assign it onto the client options for convenience so it
Expand All @@ -90,14 +93,14 @@ protected override IHost CreateHost(IHostBuilder builder)
var addresses = server.Features.Get<IServerAddressesFeature>();

ClientOptions.BaseAddress = addresses!.Addresses
.Select((p) => new Uri(p))
.Select(p => new Uri(p))
.Last();

// Return the host that uses TestServer, rather than the real one.
// Otherwise the internals will complain about the host's server
// not being an instance of the concrete type TestServer.
testHost.Start();
_pooledDatabase.EnsureInitialized(testHost);

return testHost;
}

Expand All @@ -124,8 +127,9 @@ protected override void Dispose(bool disposing)

private void EnsureServer()
{
// This forces WebApplicationFactory to bootstrap the server
var foo = base.Services;
typeof(WebApplicationFactory<Program>)
.GetMethod("EnsureServer", BindingFlags.NonPublic | BindingFlags.Instance)
!.Invoke(this, Array.Empty<object>());
}

public void SeedData(Action<BloggingContext> seedAction)
Expand Down

0 comments on commit ecda891

Please sign in to comment.