Supporting unit testing websockets with TestHost
See original GitHub issueHello,
In order to unit test my client implementation I would like to use the TestHost class provided by ASP.NET Core.
The Testhost class however returns a TestHost.WebSocketClient which has a method ConnectAsync which returns a WebSocket. Since the websocket client requires that the type should be ClientWebSocket (which inherits from WebSocket).
I can see the only major blocker for changing to WebSocket is the connection and reconnection logic. This could possible be fixed by updating the factory method to handle connection. For example (line 19 in WebsocketClient):
Func<Uri, Task<WebSocket>> clientFactory = null
The default implementation would be (the CancellationToken would need to be updated):
_clientFactory = clientFactory ?? (async (uri) =>
{
var client = new ClientWebSocket
{
Options = { KeepAliveInterval = new TimeSpan(0, 0, 5, 0) }
};
await client.ConnectAsync(uri, CancellationToken.None).ConfigureAwait(false);
return client;
});
This is however only part of the solution and will be a breaking change for the existing websocket client.
Are you willing to accept a PR to support this? Including a unit test with TestHost.
Sincerely, Søren
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (15 by maintainers)

Top Related StackOverflow Question
PR was merged and it is ready to accept more unit tests https://github.com/Marfusios/websocket-client/pull/23
@sahb1239
what do you think about:
? It should be breaking-free update. Check branch https://github.com/Marfusios/websocket-client/commit/f19863f404cb6846c62aa53a9415d8bac5a97be7
Could you please provide PR with unit tests?