// 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.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR.Tests.Common; using Microsoft.AspNetCore.Sockets.Client; using Microsoft.Extensions.Logging; using Moq; using Moq.Protected; using Xunit; namespace Microsoft.AspNetCore.SignalR.Client.Tests { public class HubConnectionTests { [Fact] public void CannotCreateHubConnectionWithNullUrl() { var exception = Assert.Throws( () => new HubConnection((Uri)null, Mock.Of(), Mock.Of())); Assert.Equal("url", exception.ParamName); } [Fact] public async Task CanDisposeNotStartedHubConnection() { await new HubConnection(new Uri("http://fakeuri.org"), Mock.Of(), Mock.Of()) .DisposeAsync(); } [Fact] public async Task CannotStartRunningHubConnection() { var mockHttpHandler = new Mock(); mockHttpHandler.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .Returns(async (request, cancellationToken) => { await Task.Yield(); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) }; }); using (var httpClient = new HttpClient(mockHttpHandler.Object)) { var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()); var hubConnection = new HubConnection(new Uri("http://fakeuri.org/"), Mock.Of(), new LoggerFactory()); try { await hubConnection.StartAsync(longPollingTransport, httpClient); var exception = await Assert.ThrowsAsync( async () => await hubConnection.StartAsync(longPollingTransport)); Assert.Equal("Cannot start a connection that is not in the Initial state.", exception.Message); } finally { await hubConnection.DisposeAsync(); } } } [Fact] public async Task CannotStartStoppedHubConnection() { var mockHttpHandler = new Mock(); mockHttpHandler.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .Returns(async (request, cancellationToken) => { await Task.Yield(); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) }; }); using (var httpClient = new HttpClient(mockHttpHandler.Object)) { var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()); var hubConnection = new HubConnection(new Uri("http://fakeuri.org/"), Mock.Of(), new LoggerFactory()); await hubConnection.StartAsync(longPollingTransport, httpClient); await hubConnection.DisposeAsync(); var exception = await Assert.ThrowsAsync( async () => await hubConnection.StartAsync(longPollingTransport)); Assert.Equal("Cannot start a connection that is not in the Initial state.", exception.Message); } } [Fact(Skip = "Not implemented")] public async Task InvokeThrowsIfHubConnectionNotStarted() { var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of(), Mock.Of()); var exception = await Assert.ThrowsAsync(async () => await hubConnection.Invoke("test")); Assert.Equal("Cannot invoke methods on non-started connections.", exception.Message); } [Fact(Skip = "Not implemented")] public async Task InvokeThrowsIfHubConnectionDisposed() { var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of(), Mock.Of()); await hubConnection.DisposeAsync(); var exception = await Assert.ThrowsAsync(async () => await hubConnection.Invoke("test")); Assert.Equal("Cannot invoke methods on disposed connections.", exception.Message); } [Fact] public async Task HubConnectionConnectedEventRaisedWhenTheClientIsConnected() { var mockHttpHandler = new Mock(); mockHttpHandler.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .Returns(async (request, cancellationToken) => { await Task.Yield(); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) }; }); using (var httpClient = new HttpClient(mockHttpHandler.Object)) { var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()); var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of(), new LoggerFactory()); try { var connectedEventRaised = false; hubConnection.Connected += () => connectedEventRaised = true; await hubConnection.StartAsync(longPollingTransport, httpClient); Assert.True(connectedEventRaised); } finally { await hubConnection.DisposeAsync(); } } } [Fact] public async Task ClosedEventRaisedWhenTheClientIsStopped() { var mockHttpHandler = new Mock(); mockHttpHandler.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .Returns(async (request, cancellationToken) => { await Task.Yield(); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) }; }); using (var httpClient = new HttpClient(mockHttpHandler.Object)) { var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()); var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of(), new LoggerFactory()); var closedEventTcs = new TaskCompletionSource(); hubConnection.Closed += e => closedEventTcs.SetResult(e); await hubConnection.StartAsync(longPollingTransport, httpClient); await hubConnection.DisposeAsync(); Assert.Null(await closedEventTcs.Task.OrTimeout()); } } [Fact] public async Task CannotCallInvokeOnClosedHubConnection() { var mockConnection = new Mock(); mockConnection .Setup(m => m.DisposeAsync()) .Callback(() => mockConnection.Raise(c => c.Closed += null, (Exception)null)) .Returns(Task.FromResult(null)); var hubConnection = new HubConnection(mockConnection.Object, Mock.Of(), new LoggerFactory()); await hubConnection.StartAsync(Mock.Of()); await hubConnection.DisposeAsync(); var exception = await Assert.ThrowsAsync( async () => await hubConnection.Invoke("test", typeof(int))); Assert.Equal("Connection has been terminated.", exception.Message); } [Fact] public async Task PendingInvocationsAreCancelledWhenConnectionClosesCleanly() { var mockConnection = new Mock(); mockConnection .Setup(m => m.DisposeAsync()) .Callback(() => mockConnection.Raise(c => c.Closed += null, (Exception)null)) .Returns(Task.FromResult(null)); var hubConnection = new HubConnection(mockConnection.Object, Mock.Of(), new LoggerFactory()); await hubConnection.StartAsync(Mock.Of()); var invokeTask = hubConnection.Invoke("testMethod", typeof(int)); await hubConnection.DisposeAsync(); await Assert.ThrowsAsync(async () => await invokeTask); } [Fact] public async Task PendingInvocationsAreTerminatedWithExceptionWhenConnectionClosesDueToError() { var exception = new InvalidOperationException(); var mockConnection = new Mock(); mockConnection .Setup(m => m.DisposeAsync()) .Callback(() => mockConnection.Raise(c => c.Closed += null, exception)) .Returns(Task.FromResult(null)); var hubConnection = new HubConnection(mockConnection.Object, Mock.Of(), new LoggerFactory()); await hubConnection.StartAsync(Mock.Of()); var invokeTask = hubConnection.Invoke("testMethod", typeof(int)); await hubConnection.DisposeAsync(); var thrown = await Assert.ThrowsAsync(exception.GetType(), async () => await invokeTask); Assert.Same(exception, thrown); } } }