Remove reflection from Connect test (#57)
* Remove reflection from Connect test This removes the reflection from the double connection test and replaces it with a `MockHttpHandler` to let the connection actually succeed and then test the redundant `ConnectAsync` call. partial fix for #48 * remove unneeded using --------- Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
parent
08e7da100f
commit
d18da9a08d
1 changed files with 43 additions and 2 deletions
|
|
@ -107,12 +107,53 @@ public class SseClientTransportTests
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ConnectAsync_Throws_If_Already_Connected()
|
public async Task ConnectAsync_Throws_If_Already_Connected()
|
||||||
{
|
{
|
||||||
await using var transport = new SseClientTransport(_transportOptions, _serverConfig, NullLoggerFactory.Instance);
|
using var mockHttpHandler = new MockHttpHandler();
|
||||||
transport.GetType().BaseType!.GetField("_isConnected", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.SetValue(transport, true);
|
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 action = async () => await transport.ConnectAsync();
|
||||||
var exception = await Assert.ThrowsAsync<McpTransportException>(action);
|
var exception = await Assert.ThrowsAsync<McpTransportException>(action);
|
||||||
Assert.Equal("Transport is already connected", exception.Message);
|
Assert.Equal("Transport is already connected", exception.Message);
|
||||||
|
mreDone.Set();
|
||||||
|
await transport.CloseAsync();
|
||||||
|
await task;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue