Skip to content

Remove reflection from Connect test #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2025
Merged
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 @@ -107,12 +107,53 @@ public async Task ConnectAsync_Should_Connect_Successfully()
[Fact]
public async Task ConnectAsync_Throws_If_Already_Connected()
{
await using var transport = new SseClientTransport(_transportOptions, _serverConfig, NullLoggerFactory.Instance);
transport.GetType().BaseType!.GetField("_isConnected", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.SetValue(transport, true);
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new SseClientTransport(_transportOptions, _serverConfig, httpClient, NullLoggerFactory.Instance);
using var mreConnected = new ManualResetEventSlim(false);
using var mreDone = new ManualResetEventSlim(false);
var callIndex = 0;

mockHttpHandler.RequestHandler = (request) =>
{
switch (callIndex++)
{
case 0:
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("event: endpoint\r\ndata: http://localhost\r\n\r\n")
});
case 1:
mreConnected.Set();
mreDone.Wait();
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("")
});
default:
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("")
});
}
};

var task = Task.Run(async () =>
{
await transport.ConnectAsync(TestContext.Current.CancellationToken);
}, TestContext.Current.CancellationToken);

mreConnected.Wait(TestContext.Current.CancellationToken);
Assert.True(transport.IsConnected);
var action = async () => await transport.ConnectAsync();
var exception = await Assert.ThrowsAsync<McpTransportException>(action);
Assert.Equal("Transport is already connected", exception.Message);
mreDone.Set();
await transport.CloseAsync();
await task;
}

[Fact]
Expand Down