// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.IO.Pipelines; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Sockets; using Microsoft.AspNetCore.Sockets.Internal; using Microsoft.Extensions.DependencyInjection; using Moq; using Xunit; namespace Microsoft.AspNetCore.SignalR.Tests { public class HubEndpointTests { [Fact] public async Task HubsAreDisposed() { var trackDispose = new TrackDispose(); var serviceProvider = CreateServiceProvider(s => s.AddSingleton(trackDispose)); var endPoint = serviceProvider.GetService>(); using (var connectionWrapper = new ConnectionWrapper()) { var endPointTask = endPoint.OnConnectedAsync(connectionWrapper.Connection); await connectionWrapper.ApplicationStartedReading; // kill the connection connectionWrapper.ConnectionState.Dispose(); await endPointTask; Assert.Equal(2, trackDispose.DisposeCount); } } [Fact] public async Task OnDisconnectedCalledWithExceptionIfHubMethodNotFound() { var hub = Mock.Of(); var endPointType = GetEndPointType(hub.GetType()); var serviceProvider = CreateServiceProvider(s => { s.AddSingleton(endPointType); s.AddTransient(hub.GetType(), sp => hub); }); dynamic endPoint = serviceProvider.GetService(endPointType); using (var connectionWrapper = new ConnectionWrapper()) { var endPointTask = endPoint.OnConnectedAsync(connectionWrapper.Connection); await connectionWrapper.ApplicationStartedReading; var buffer = connectionWrapper.ConnectionState.Application.Output.Alloc(); buffer.Write(Encoding.UTF8.GetBytes("0xdeadbeef")); await buffer.FlushAsync(); connectionWrapper.Dispose(); // InvalidCastException because the payload is not a JObject // which is expected by the formatter await Assert.ThrowsAsync(async () => await endPointTask); Mock.Get(hub).Verify(h => h.OnDisconnectedAsync(It.IsNotNull()), Times.Once()); } } [Fact] public async Task LifetimeManagerOnDisconnectedAsyncCalledIfLifetimeManagerOnConnectedAsyncThrows() { var mockLifetimeManager = new Mock>(); mockLifetimeManager .Setup(m => m.OnConnectedAsync(It.IsAny())) .Throws(new InvalidOperationException("Lifetime manager OnConnectedAsync failed.")); var mockHubActivator = new Mock>(); var serviceProvider = CreateServiceProvider(services => { services.AddSingleton(mockLifetimeManager.Object); services.AddSingleton(mockHubActivator.Object); }); var endPoint = serviceProvider.GetService>(); using (var connectionWrapper = new ConnectionWrapper()) { var exception = await Assert.ThrowsAsync( async () => await endPoint.OnConnectedAsync(connectionWrapper.Connection)); Assert.Equal("Lifetime manager OnConnectedAsync failed.", exception.Message); connectionWrapper.ConnectionState.Dispose(); mockLifetimeManager.Verify(m => m.OnConnectedAsync(It.IsAny()), Times.Once); mockLifetimeManager.Verify(m => m.OnDisconnectedAsync(It.IsAny()), Times.Once); // No hubs should be created since the connection is terminated mockHubActivator.Verify(m => m.Create(), Times.Never); mockHubActivator.Verify(m => m.Release(It.IsAny()), Times.Never); } } [Fact] public async Task HubOnDisconnectedAsyncCalledIfHubOnConnectedAsyncThrows() { var mockLifetimeManager = new Mock>(); var serviceProvider = CreateServiceProvider(services => { services.AddSingleton(mockLifetimeManager.Object); }); var endPoint = serviceProvider.GetService>(); using (var connectionWrapper = new ConnectionWrapper()) { var endPointTask = endPoint.OnConnectedAsync(connectionWrapper.Connection); connectionWrapper.ConnectionState.Dispose(); var exception = await Assert.ThrowsAsync(async () => await endPointTask); Assert.Equal("Hub OnConnected failed.", exception.Message); mockLifetimeManager.Verify(m => m.OnConnectedAsync(It.IsAny()), Times.Once); mockLifetimeManager.Verify(m => m.OnDisconnectedAsync(It.IsAny()), Times.Once); } } [Fact] public async Task LifetimeManagerOnDisconnectedAsyncCalledIfHubOnDisconnectedAsyncThrows() { var mockLifetimeManager = new Mock>(); var serviceProvider = CreateServiceProvider(services => { services.AddSingleton(mockLifetimeManager.Object); }); var endPoint = serviceProvider.GetService>(); using (var connectionWrapper = new ConnectionWrapper()) { var endPointTask = endPoint.OnConnectedAsync(connectionWrapper.Connection); connectionWrapper.ConnectionState.Dispose(); var exception = await Assert.ThrowsAsync(async () => await endPointTask); Assert.Equal("Hub OnDisconnected failed.", exception.Message); mockLifetimeManager.Verify(m => m.OnConnectedAsync(It.IsAny()), Times.Once); mockLifetimeManager.Verify(m => m.OnDisconnectedAsync(It.IsAny()), Times.Once); } } private static Type GetEndPointType(Type hubType) { var endPointType = typeof(HubEndPoint<>); return endPointType.MakeGenericType(hubType); } private static Type GetGenericType(Type genericType, Type hubType) { return genericType.MakeGenericType(hubType); } public class OnConnectedThrowsHub : Hub { public override Task OnConnectedAsync() { var tcs = new TaskCompletionSource(); tcs.SetException(new InvalidOperationException("Hub OnConnected failed.")); return tcs.Task; } } public class OnDisconnectedThrowsHub : Hub { public override Task OnDisconnectedAsync(Exception exception) { var tcs = new TaskCompletionSource(); tcs.SetException(new InvalidOperationException("Hub OnDisconnected failed.")); return tcs.Task; } } private class TestHub : Hub { private TrackDispose _trackDispose; public TestHub(TrackDispose trackDispose) { _trackDispose = trackDispose; } protected override void Dispose(bool dispose) { if (dispose) { _trackDispose.DisposeCount++; } } } private class TrackDispose { public int DisposeCount = 0; } private IServiceProvider CreateServiceProvider(Action addServices = null) { var services = new ServiceCollection(); services.AddOptions() .AddLogging() .AddSignalR(); addServices?.Invoke(services); return services.BuildServiceProvider(); } private class ConnectionWrapper : IDisposable { private PipelineFactory _factory; public StreamingConnectionState ConnectionState; public StreamingConnection Connection => ConnectionState.Connection; // Still kinda gross... public Task ApplicationStartedReading => ((PipelineReaderWriter)Connection.Transport.Input).ReadingStarted; public ConnectionWrapper(string format = "json") { _factory = new PipelineFactory(); var connectionManager = new ConnectionManager(_factory); ConnectionState = (StreamingConnectionState)connectionManager.CreateConnection(ConnectionMode.Streaming); ConnectionState.Connection.Metadata["formatType"] = format; ConnectionState.Connection.User = new ClaimsPrincipal(new ClaimsIdentity()); } public void Dispose() { ConnectionState.Dispose(); _factory.Dispose(); } } } }