question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Supporting unit testing websockets with TestHost

See original GitHub issue

Hello,

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:closed
  • Created 4 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
Marfusioscommented, Sep 27, 2019

PR was merged and it is ready to accept more unit tests https://github.com/Marfusios/websocket-client/pull/23

1reaction
Marfusioscommented, Sep 25, 2019

@sahb1239

what do you think about:

_client = _clientFactory();
if (_client is ClientWebSocket clientSpecific)
{
	await clientSpecific.ConnectAsync(uri, token).ConfigureAwait(false);
}

? It should be breaking-free update. Check branch https://github.com/Marfusios/websocket-client/commit/f19863f404cb6846c62aa53a9415d8bac5a97be7

Could you please provide PR with unit tests?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing Integration Tests for WebSocket Servers Using Jest ...
These are the kinds of questions I hope to address in this post on writing integration tests for WebSocket servers. Here's our outline:....
Read more >
Testing TestHost.WebSocketClient with ClientWebSocket . ...
My question is how can I ask ClientWebSocket to connect to TestHost.WebSocketClient ? c# · websocket · integration-testing · clientwebsocket ...
Read more >
TestServer.CreateWebSocketClient Method
Creates a WebSocketClient for interacting with the test server. ... TestHost. Assembly: Microsoft.AspNetCore.TestHost.dll. Package: Microsoft.AspNetCore.
Read more >
How to Unit Test URLSessionWebSocketTask
The best way to test web socket tasks is to run them against a local server. Network framework has direct support for creating...
Read more >
Integration Testing of Real-time communication in ASP. ...
Learn how you can incorporate these two concepts by building a robust integration tests infrastructure using SignalR and Kestrel.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found