Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ public static IHostBuilder GetHostBuilder<TEntryPoint>(Action<IWebHostBuilder>?

configure?.Invoke(webHostBuilder);

webHostBuilder.UseTestServer(options => options.PreserveExecutionContext = true);

webHostBuilder.ConfigureServices(x => x.TryAddSingleton<HttpClient>(sp =>
((TestServer)sp.GetRequiredService<IServer>()).CreateClient()));
webHostBuilder.UseTestServerAndClient();

webHostBuilder.ConfigureServices((context, services) =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Xunit.DependencyInjection.AspNetCoreTesting;

public static class WebHostBuilderExtensions
{
public static IWebHostBuilder UseTestServerAndClient(this IWebHostBuilder webHostBuilder)
{
ArgumentNullException.ThrowIfNull(webHostBuilder);

webHostBuilder.UseTestServer(x => x.PreserveExecutionContext = true);
webHostBuilder.ConfigureServices(x =>
{
x.TryAddSingleton<HttpClient>(sp =>
((TestServer)sp.GetRequiredService<IServer>()).CreateClient());
});

return webHostBuilder;
}

public static IWebHostBuilder UseTestServerAndClient(this IWebHostBuilder webHostBuilder,
Action<TestServerOptions> testServerConfigure)
{
ArgumentNullException.ThrowIfNull(testServerConfigure);

webHostBuilder.UseTestServerAndClient()
.ConfigureServices(x => x.Configure(testServerConfigure));

return webHostBuilder;
}
}
2 changes: 1 addition & 1 deletion test/MinimalApiSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

var app = builder.Build();

// app.Map("/", () => "Hello world");
app.Map("/hello", () => "Hello world");
app.MapDefaultControllerRoute();

await app.RunAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ namespace Xunit.DependencyInjection.Test.AspNetCore;
public class MinimalApiTest(HttpClient httpClient, IRandomService randomService)
{
[Fact]
public async Task ResponseTest()
public async Task MinimalApiRouteResponseTest()
{
var responseText = await httpClient.GetStringAsync("/hello");
Assert.Equal("Hello world", responseText);
}

[Fact]
public async Task ControllerRouteResponseTest()
{
var responseText = await httpClient.GetStringAsync("/");
Assert.Equal("Hello world", responseText);
Expand Down
3 changes: 2 additions & 1 deletion test/Xunit.DependencyInjection.Test.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
using Xunit.DependencyInjection.AspNetCoreTesting;
using Xunit.DependencyInjection.Logging;

[assembly: CollectionBehavior(DisableTestParallelization = true)]
Expand All @@ -14,7 +15,7 @@ public class Startup
{
public void ConfigureHost(IHostBuilder hostBuilder) =>
hostBuilder.ConfigureWebHost(webHostBuilder => webHostBuilder
.UseTestServer(options => options.PreserveExecutionContext = true)
.UseTestServerAndClient()
.UseStartup<AspNetCoreStartup>());

private class AspNetCoreStartup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@

namespace Xunit.DependencyInjection.Test.AspNetCore;

public class TestServerTest(IServer server)
public class TestServerTest(HttpClient httpClient)
{
public static string Key { get; } = Guid.NewGuid().ToString("N");

private readonly HttpClient _httpClient = ((TestServer)server).CreateClient();

[Fact]
public async Task HttpTest()
{
using var response = await _httpClient.GetAsync("/");
using var response = await httpClient.GetAsync("/");

response.EnsureSuccessStatusCode();

Expand Down