1
votes

Is there away to test SignalR using TestServer generated as part of integration tests (https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests view=aspnetcore-2.1)?

All my retries failed to perform handshake to the TestServer instance fail with Exception: System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it

Running in Debug/Console/IIS works perfectly

Server Code

 public void Configure(...)
 {
     ...
     app.UseSignalR(router => router.MapHub<MockHub>("/ws"));
     ...
}

TestCode

        ...
        var server = factory.Server;
        var connection = new HubConnectionBuilder()
              .WithUrl(server.BaseAddress)
              .Build();
        await connection.StartAsync();
2

2 Answers

4
votes

Looks like TestServer requires you to use the HttpMessageHandler created by it, so change your code to

.WithUrl(new Uri(server.BaseAddress, "ws"), o => { o.HttpMessageHandlerFactory = _ => server.CreateHandler(); })

0
votes

It should work. The only thing I see obviously wrong is that you're passing just the test host address to WithUrl, whereas, it should be the full path to your hub, i.e. something like:

.WithUrl(new Uri(server.BaseAddress, Consts.TethysWebSocketPath))