Skip to content

Commit b9c9f44

Browse files
authored
Fix tests in Dev Container (#965)
1 parent 00bb5b2 commit b9c9f44

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
}
2020
}
2121
},
22-
"postCreateCommand": "dotnet --list-sdks && echo 'Available .NET SDKs installed successfully!'"
22+
"postCreateCommand": "dotnet dev-certs https --trust && dotnet --list-sdks && echo 'Available .NET SDKs installed successfully!'"
2323
}

tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Connections;
3+
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.Extensions.DependencyInjection;
4-
using Microsoft.Extensions.DependencyInjection.Extensions;
55
using ModelContextProtocol.Tests.Utils;
66

77
namespace ModelContextProtocol.AspNetCore.Tests.Utils;
@@ -11,11 +11,11 @@ public class KestrelInMemoryTest : LoggedTest
1111
public KestrelInMemoryTest(ITestOutputHelper testOutputHelper)
1212
: base(testOutputHelper)
1313
{
14-
// Use SlimBuilder instead of EmptyBuilder to avoid having to call UseRouting() and UseEndpoints(_ => { })
15-
// or a helper that does the same every test. But clear out the existing socket transport to avoid potential port conflicts.
16-
Builder = WebApplication.CreateSlimBuilder();
17-
Builder.Services.RemoveAll<IConnectionListenerFactory>();
14+
Builder = WebApplication.CreateEmptyBuilder(new());
1815
Builder.Services.AddSingleton<IConnectionListenerFactory>(KestrelInMemoryTransport);
16+
Builder.WebHost.UseKestrelCore();
17+
Builder.Services.AddRoutingCore();
18+
Builder.Services.AddLogging();
1919
Builder.Services.AddSingleton(XunitLoggerProvider);
2020

2121
SocketsHttpHandler.ConnectCallback = (context, token) =>

tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTransport.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,30 @@ namespace ModelContextProtocol.AspNetCore.Tests.Utils;
77

88
public sealed class KestrelInMemoryTransport : IConnectionListenerFactory
99
{
10-
// socket accept queues keyed by listen port.
10+
// Socket accept queues keyed by listen port.
1111
private readonly ConcurrentDictionary<int, Channel<ConnectionContext>> _acceptQueues = [];
1212

1313
public KestrelInMemoryConnection CreateConnection(EndPoint endpoint)
1414
{
15+
if (!_acceptQueues.TryGetValue(GetEndpointPort(endpoint), out var acceptQueue))
16+
{
17+
throw new IOException($"No listener is bound to endpoint '{endpoint}'.");
18+
}
19+
1520
var connection = new KestrelInMemoryConnection();
16-
if (!GetAcceptQueue(endpoint).Writer.TryWrite(connection))
21+
if (!acceptQueue.Writer.TryWrite(connection))
1722
{
1823
throw new IOException("The KestrelInMemoryTransport has been shut down.");
1924
};
2025

2126
return connection;
2227
}
2328

24-
public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default) =>
25-
new(new KestrelInMemoryListener(endpoint, GetAcceptQueue(endpoint)));
26-
27-
private Channel<ConnectionContext> GetAcceptQueue(EndPoint endpoint) =>
28-
_acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded<ConnectionContext>());
29+
public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default)
30+
{
31+
var acceptQueue = _acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded<ConnectionContext>());
32+
return new(new KestrelInMemoryListener(endpoint, acceptQueue));
33+
}
2934

3035
private static int GetEndpointPort(EndPoint endpoint) =>
3136
endpoint switch

tests/ModelContextProtocol.TestOAuthServer/Program.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ public async Task RunServerAsync(string[]? args = null, CancellationToken cancel
9696

9797
var app = builder.Build();
9898

99-
app.UseRouting();
100-
app.UseEndpoints(_ => { });
101-
10299
// Set up the demo client
103100
var clientId = "demo-client";
104101
var clientSecret = "demo-secret";

tests/ModelContextProtocol.TestSseServer/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,6 @@ public static async Task MainAsync(string[] args, ILoggerProvider? loggerProvide
424424
.WithHttpTransport();
425425

426426
var app = builder.Build();
427-
app.UseRouting();
428-
app.UseEndpoints(_ => { });
429427

430428
// Handle the /stateless endpoint if no other endpoints have been matched by the call to UseRouting above.
431429
HandleStatelessMcp(app);

0 commit comments

Comments
 (0)