@@ -107,12 +107,49 @@ public async Task ConnectAsync_Should_Connect_Successfully()
107107 [ Fact ]
108108 public async Task ConnectAsync_Throws_If_Already_Connected ( )
109109 {
110- await using var transport = new SseClientTransport ( _transportOptions , _serverConfig , NullLoggerFactory . Instance ) ;
111- transport . GetType ( ) . BaseType ! . GetField ( "_isConnected" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ? . SetValue ( transport , true ) ;
110+ using var mockHttpHandler = new MockHttpHandler ( ) ;
111+ using var httpClient = new HttpClient ( mockHttpHandler ) ;
112+ await using var transport = new SseClientTransport ( _transportOptions , _serverConfig , httpClient , NullLoggerFactory . Instance ) ;
113+ var tcsConnected = new TaskCompletionSource ( ) ;
114+ var tcsDone = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
115+ var callIndex = 0 ;
116+
117+ mockHttpHandler . RequestHandler = async ( request ) =>
118+ {
119+ switch ( callIndex ++ )
120+ {
121+ case 0 :
122+ return new HttpResponseMessage
123+ {
124+ StatusCode = HttpStatusCode . OK ,
125+ Content = new StringContent ( "event: endpoint\r \n data: http://localhost\r \n \r \n " )
126+ } ;
127+ case 1 :
128+ tcsConnected . SetResult ( ) ;
129+ await tcsDone . Task ;
130+ return new HttpResponseMessage
131+ {
132+ StatusCode = HttpStatusCode . OK ,
133+ Content = new StringContent ( "" )
134+ } ;
135+ default :
136+ return new HttpResponseMessage
137+ {
138+ StatusCode = HttpStatusCode . OK ,
139+ Content = new StringContent ( "" )
140+ } ;
141+ }
142+ } ;
112143
144+ var task = transport . ConnectAsync ( TestContext . Current . CancellationToken ) ;
145+ await tcsConnected . Task ;
146+ Assert . True ( transport . IsConnected ) ;
113147 var action = async ( ) => await transport . ConnectAsync ( ) ;
114148 var exception = await Assert . ThrowsAsync < McpTransportException > ( action ) ;
115149 Assert . Equal ( "Transport is already connected" , exception . Message ) ;
150+ tcsDone . SetResult ( ) ;
151+ await transport . CloseAsync ( ) ;
152+ await task ;
116153 }
117154
118155 [ Fact ]
0 commit comments