77
88namespace ModelContextProtocol . AspNetCore . Tests ;
99
10- public abstract class SseServerIntegrationTestFixture : IAsyncDisposable
10+ public class SseServerIntegrationTestFixture : IAsyncDisposable
1111{
1212 private readonly KestrelInMemoryTransport _inMemoryTransport = new ( ) ;
13+
1314 private readonly Task _serverTask ;
1415 private readonly CancellationTokenSource _stopCts = new ( ) ;
1516
17+ // XUnit's ITestOutputHelper is created per test, while this fixture is used for
18+ // multiple tests, so this dispatches the output to the current test.
19+ private readonly DelegatingTestOutputHelper _delegatingTestOutputHelper = new ( ) ;
20+
1621 private HttpClientTransportOptions DefaultTransportOptions { get ; set ; } = new ( )
1722 {
1823 Endpoint = new ( "http://localhost:5000/" ) ,
1924 } ;
2025
21- protected SseServerIntegrationTestFixture ( )
26+ public SseServerIntegrationTestFixture ( )
2227 {
2328 var socketsHttpHandler = new SocketsHttpHandler
2429 {
@@ -34,10 +39,8 @@ protected SseServerIntegrationTestFixture()
3439 BaseAddress = new ( "http://localhost:5000/" ) ,
3540 } ;
3641
37- _serverTask = Program . MainAsync ( [ ] , CreateLoggerProvider ( ) , _inMemoryTransport , _stopCts . Token ) ;
42+ _serverTask = Program . MainAsync ( [ ] , new XunitLoggerProvider ( _delegatingTestOutputHelper ) , _inMemoryTransport , _stopCts . Token ) ;
3843 }
39-
40- protected abstract ILoggerProvider CreateLoggerProvider ( ) ;
4144
4245 public HttpClient HttpClient { get ; }
4346
@@ -50,17 +53,21 @@ public Task<McpClient> ConnectMcpClientAsync(McpClientOptions? options, ILoggerF
5053 TestContext . Current . CancellationToken ) ;
5154 }
5255
53- public virtual void Initialize ( ITestOutputHelper output , HttpClientTransportOptions clientTransportOptions )
56+ public void Initialize ( ITestOutputHelper output , HttpClientTransportOptions clientTransportOptions )
5457 {
58+ _delegatingTestOutputHelper . CurrentTestOutputHelper = output ;
5559 DefaultTransportOptions = clientTransportOptions ;
5660 }
5761
58- public virtual void TestCompleted ( )
62+ public void TestCompleted ( )
5963 {
64+ _delegatingTestOutputHelper . CurrentTestOutputHelper = null ;
6065 }
6166
62- public virtual async ValueTask DisposeAsync ( )
67+ public async ValueTask DisposeAsync ( )
6368 {
69+ _delegatingTestOutputHelper . CurrentTestOutputHelper = null ;
70+
6471 HttpClient . Dispose ( ) ;
6572 _stopCts . Cancel ( ) ;
6673
@@ -74,50 +81,4 @@ public virtual async ValueTask DisposeAsync()
7481
7582 _stopCts . Dispose ( ) ;
7683 }
77- }
78-
79- /// <summary>
80- /// SSE server fixture that routes logs to xUnit test output.
81- /// </summary>
82- public class SseServerWithXunitLoggerFixture : SseServerIntegrationTestFixture
83- {
84- // XUnit's ITestOutputHelper is created per test, while this fixture is used for
85- // multiple tests, so this dispatches the output to the current test.
86- private readonly DelegatingTestOutputHelper _delegatingTestOutputHelper = new ( ) ;
87-
88- protected override ILoggerProvider CreateLoggerProvider ( )
89- => new XunitLoggerProvider ( _delegatingTestOutputHelper ) ;
90-
91- public override void Initialize ( ITestOutputHelper output , HttpClientTransportOptions clientTransportOptions )
92- {
93- _delegatingTestOutputHelper . CurrentTestOutputHelper = output ;
94- base . Initialize ( output , clientTransportOptions ) ;
95- }
96-
97- public override void TestCompleted ( )
98- {
99- _delegatingTestOutputHelper . CurrentTestOutputHelper = null ;
100- base . TestCompleted ( ) ;
101- }
102-
103- public override async ValueTask DisposeAsync ( )
104- {
105- _delegatingTestOutputHelper . CurrentTestOutputHelper = null ;
106- await base . DisposeAsync ( ) ;
107- }
108- }
109-
110- /// <summary>
111- /// Fixture for tests that need to inspect server logs using MockLoggerProvider.
112- /// Use <see cref="SseServerWithXunitLoggerFixture"/> for tests that just need xUnit output.
113- /// </summary>
114- public class SseServerWithMockLoggerFixture : SseServerIntegrationTestFixture
115- {
116- private readonly MockLoggerProvider _mockLoggerProvider = new ( ) ;
117-
118- protected override ILoggerProvider CreateLoggerProvider ( )
119- => _mockLoggerProvider ;
120-
121- public IEnumerable < ( string Category , LogLevel LogLevel , EventId EventId , string Message , Exception ? Exception ) > ServerLogs
122- => _mockLoggerProvider . LogMessages ;
123- }
84+ }
0 commit comments