csharp-sdk/tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.cs
Copilot a83d6198a5
Centralize test timeout constants to fix CI flakiness (#1224)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
Co-authored-by: Eric StJohn <ericstj@microsoft.com>
2026-01-30 13:47:11 -08:00

53 lines
1.8 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Tests.Utils;
namespace ModelContextProtocol.AspNetCore.Tests.Utils;
public class KestrelInMemoryTest : LoggedTest
{
public KestrelInMemoryTest(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
Builder = WebApplication.CreateEmptyBuilder(new());
Builder.Services.AddSingleton<IConnectionListenerFactory>(KestrelInMemoryTransport);
Builder.WebHost.UseKestrelCore();
Builder.Services.AddRoutingCore();
Builder.Services.AddLogging();
Builder.Services.AddSingleton<ILoggerProvider>(MockLoggerProvider);
Builder.Services.AddSingleton(XunitLoggerProvider);
Builder.Logging.SetMinimumLevel(LogLevel.Debug);
SocketsHttpHandler.ConnectCallback = (context, token) =>
{
var connection = KestrelInMemoryTransport.CreateConnection(context.DnsEndPoint);
return new(connection.ClientStream);
};
HttpClient = new HttpClient(SocketsHttpHandler);
ConfigureHttpClient(HttpClient);
}
public WebApplicationBuilder Builder { get; }
public HttpClient HttpClient { get; set; }
public SocketsHttpHandler SocketsHttpHandler { get; } = new();
public KestrelInMemoryTransport KestrelInMemoryTransport { get; } = new();
protected static void ConfigureHttpClient(HttpClient httpClient)
{
httpClient.BaseAddress = new Uri("http://localhost:5000/");
httpClient.Timeout = TestConstants.HttpClientTimeout;
}
public override void Dispose()
{
HttpClient.Dispose();
base.Dispose();
}
}