Use Kestrel for all in-memory HTTP tests (#225)
* Add route pattern parameter to MapMcp * Add configureOptionsAsync * Use Kestrel for all in-memory HTTP tests * Use ApplicationStopping token for SSE responses * Use RegisterNotificationHandler in MapMcp tests * Work around SocketsHttpHandler bug where it doesn't call DispseAsync on the stream returned by the ConnectCallback
This commit is contained in:
parent
848c9df433
commit
faf12b6a94
1 changed files with 631 additions and 642 deletions
|
|
@ -0,0 +1,50 @@
|
|||
using Microsoft.AspNetCore.Connections;
|
||||
using System.Net;
|
||||
using System.Threading.Channels;
|
||||
|
||||
namespace ModelContextProtocol.Tests.Utils;
|
||||
|
||||
public sealed class KestrelInMemoryTransport : IConnectionListenerFactory, IConnectionListener
|
||||
{
|
||||
private readonly Channel<ConnectionContext> _acceptQueue = Channel.CreateUnbounded<ConnectionContext>();
|
||||
private EndPoint? _endPoint;
|
||||
|
||||
public EndPoint EndPoint => _endPoint ?? throw new InvalidOperationException("EndPoint is not set. Call BindAsync first.");
|
||||
|
||||
public KestrelInMemoryConnection CreateConnection()
|
||||
{
|
||||
var connection = new KestrelInMemoryConnection();
|
||||
_acceptQueue.Writer.TryWrite(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public async ValueTask<ConnectionContext?> AcceptAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (await _acceptQueue.Reader.WaitToReadAsync(cancellationToken))
|
||||
{
|
||||
while (_acceptQueue.Reader.TryRead(out var item))
|
||||
{
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_endPoint = endpoint;
|
||||
return new ValueTask<IConnectionListener>(this);
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
return UnbindAsync(default);
|
||||
}
|
||||
|
||||
public ValueTask UnbindAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
_acceptQueue.Writer.TryComplete();
|
||||
return default;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue