-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SignalR] Better integration with TestServer #11888
Comments
Bear in mind that WebHostBuilder will be deprecated in future releases. |
Yes, we're aware :). The focus is on better integration with TestServer, not necessarily using WebHostBuilder. |
Please, these characteristics are very important to be able to test SignalR with TestServer in integration or unit tests. |
👀 |
There is now an API on HttpConnectionOptions to allow replacing the websocket so an extension method on the HubConnectionBuilder can be done now. |
@BrennanConroy which .Net version? .Net 5 or 6? Thanks. |
6, we don't add or change APIs in already released versions. |
Not sure if it helps, but here is an example to use named pipe as the underlying transport for testing: https://github.com/wegylexy/SignalRTunnel/blob/0afb58b11f88afcae8962a797f1eace14ce05096/src/Test/Test.cs#L71 |
Hey @BrennanConroy. I have migrated my integration tests to .NET 6 and I've replaced the WebSocket in HttpConnectionOptions with one created from TestServer. Currently having some issues with authentication - the access token is not sent with the request that establishes a connection to the server. Can you advise on how to add it to either the headers or query string? SignalR client code:
|
@dayadimova in case of browser's websocket implementation access token can be passed only with uri. And as you are creating WebSocket client manually, you need to add it to your uri as well. You can check default aspnetcore/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.cs Line 48 in a0b950f
The needed part is at the bottom
so you would need to update your code to something like var connection = new HubConnectionBuilder()
.WithUrl($"{_baseUrl}/hubs", options =>
{
options.AccessTokenProvider = () => Task.FromResult(token);
options.SkipNegotiation = true;
options.Transports = HttpTransportType.WebSockets;
options.WebSocketFactory = (context, cancellationToken) =>
{
var webSocketClient = _server.CreateWebSocketClient();
+ var url = $"{context.Uri}?access_token={token}";
+ var webSocket = webSocketClient.ConnectAsync(url, cancellationToken).GetAwaiter().GetResult();
return ValueTask.FromResult(webSocket);
};
options.Headers.Add(IntegrationTestConstants.CorrTokenHeaderKey, IntegrationTestConstants.CorrTokenHeaderValue);
})
.Build(); |
Awesome, thank you for the help, that worked 🙇 |
You want to return the connection task instead of blocking a thread. var connection = new HubConnectionBuilder()
.WithUrl($"{_baseUrl}/hubs", options =>
{
options.AccessTokenProvider = () => Task.FromResult(token);
options.SkipNegotiation = true;
options.Transports = HttpTransportType.WebSockets;
options.WebSocketFactory = (context, cancellationToken) =>
{
var webSocketClient = _server.CreateWebSocketClient();
var url = $"{context.Uri}?access_token={token}";
- var webSocket = webSocketClient.ConnectAsync(url, cancellationToken).GetAwaiter().GetResult();
+ var webSocketTask = webSocketClient.ConnectAsync(url, cancellationToken);
- return ValueTask.FromResult(webSocket);
+ return new(webSocketTask);
};
options.Headers.Add(IntegrationTestConstants.CorrTokenHeaderKey, IntegrationTestConstants.CorrTokenHeaderValue);
})
.Build(); |
Where is |
I think that's something they defined in their app and unrelated to what is being shown in the sample code. |
The following code snippet works:
How to use the |
I seem to be running into an issue with something similar to this. I am using .NET 6, and have created my TestServer and SignalR connection as follows in my integration test function:
However, my test fails with the following Error information: Message: Stack Trace:
My token is issued successfully. I noticed that if I remove the Message:
Stack Trace:
I have used Thanks in advance! |
Is there anything more clearly defined for testing SignalR with WebApplicationFactory yet? Can we also get some testing documentation in the official docs? |
We're using cookie authentication for our SignalR endpoints which is achieved via a REST endpoint. Currently, there doesn't seem to be a way to share a handler (and therefore the cookies) with TestServer. Is there plans to include this functionality/is there a workaround that I'm unaware of? |
It doesn't look like There is enough extensibility to implement it manually though. If you wrap the |
I am using .NET 7 and in my case, the following code was more than enough
|
Is there are new progress on this issue? Also just wanted to point out that the var retryPolicy = new RetryPolicy();
var hubConnection = new HubConnectionBuilder()
.WithUrl("http://localhost/hubPath", opt => opt.HttpMessageHandlerFactory = HttpMessageHandlerFactory)
.Build();
await hubConnection.StartAsync(); |
Right now, it's only possible to use TestServer with the non-WebSockets transports because we only provide a way to replace the
HttpMessageHandler
. We should consider providing a way to provide aWebSocket
"factory" so that we can integrate properly with TestServer.It might be most appropriate here to add an alternative to
WithUrl
onHubConnectionBuilder
. Consider this possible test code:It would be relatively simple to add this API once we have a way to swap out the WebSocket, though we'd probably need to do so in a new assembly to avoid layering issues.
The text was updated successfully, but these errors were encountered: