public static IDisposable TryGetClient(bool scaleout, int nodes, out IHttpClient client) { if (scaleout) { MemoryHost[] hosts; UseScaleout(nodes, out hosts, out client); return(new DisposableAction(() => { for (int i = 0; i < nodes; i++) { hosts[i].Dispose(); } })); } else { var host = new MemoryHost(); host.Configure(app => { var config = new HubConfiguration() { Resolver = new DefaultDependencyResolver() }; app.MapSignalR(config); }); client = host; return(host); } }
public void UnauthenticatedUserCanInvokeMethodsByDefault() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { })); app.MapHubs("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); var hub = connection.CreateHubProxy("NoAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string>("invoked", (id, time) => { Assert.NotNull(id); wh.Set(); }); connection.Start(host).Wait(); hub.InvokeWithTimeout("InvokedFromClient"); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3))); connection.Stop(); } }
public async Task DisconnectFiresForPersistentConnectionWhenClientDisconnects() { using (var host = new MemoryHost()) { var connectWh = new AsyncManualResetEvent(); var disconnectWh = new AsyncManualResetEvent(); var dr = new DefaultDependencyResolver(); var configuration = dr.Resolve <IConfigurationManager>(); host.Configure(app => { var config = new ConnectionConfiguration { Resolver = dr }; app.MapSignalR <MyConnection>("/echo", config); configuration.DisconnectTimeout = TimeSpan.FromSeconds(6); dr.Register(typeof(MyConnection), () => new MyConnection(connectWh, disconnectWh)); }); var connection = new Client.Connection("http://foo/echo"); await connection.Start(host); Assert.True(await connectWh.WaitAsync(TimeSpan.FromSeconds(10)), "Connect never fired"); ((Client.IConnection)connection).Disconnect(); Assert.True(await disconnectWh.WaitAsync(TimeSpan.FromSeconds(20)), "Disconnect never fired"); } }
public void PrefixMatchingIsNotGreedyExactMatch() { using (var host = new MemoryHost()) { host.Configure(app => { var config = new ConnectionConfiguration { Resolver = new DefaultDependencyResolver() }; app.MapSignalR <MyConnection>("/echo", config); app.MapSignalR <MyConnection2>("/echo2", config); }); var tcs = new TaskCompletionSource <string>(); var connection = new Connection("http://foo/echo"); using (connection) { connection.Received += data => { tcs.TrySetResult(data); }; connection.Start(host).Wait(); connection.Send(""); Assert.Equal("MyConnection", tcs.Task.Result); } } }
public void AuthenticatedAndAuthorizedUserCanReceiveHubMessagesFromHubsAuthorizedSpecifyingUserAndRole() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity("User"), new string[] { "test", "Admin" })); app.MapHubs("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); var hub = connection.CreateHubProxy("UserAndRoleAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string, object>("joined", (id, time, authInfo) => { Assert.NotNull(id); wh.Set(); }); connection.Start(host).Wait(); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3))); connection.Stop(); } }
public void ConnectionUsesClientSetTransportConnectTimeout() { TimeSpan defaultTransportConnectTimeout = TimeSpan.Zero; using (var host = new MemoryHost()) { host.Configure(app => { var config = new ConnectionConfiguration { Resolver = new DefaultDependencyResolver() }; defaultTransportConnectTimeout = config.Resolver.Resolve <IConfigurationManager>().TransportConnectTimeout; app.MapSignalR <MyConnection>("/echo", config); }); var tcs = new TaskCompletionSource <string>(); var connection = new Connection("http://foo/echo"); var newTimeout = TimeSpan.FromSeconds(4); using (connection) { Assert.Equal(((IConnection)connection).TotalTransportConnectTimeout, TimeSpan.Zero); connection.TransportConnectTimeout = newTimeout; connection.Start(host).Wait(); Assert.Equal(((IConnection)connection).TotalTransportConnectTimeout - defaultTransportConnectTimeout, newTimeout); } } }
public async Task SendToSpecificClientsFromOutsideOfHub() { using (var host = new MemoryHost()) { IHubContext <IBasicClient> hubContext = null; host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; app.MapSignalR(configuration); hubContext = configuration.Resolver.Resolve <IConnectionManager>().GetHubContext <SendToSome, IBasicClient>(); }); var connection1 = new HubConnection("http://foo/"); using (connection1) { var wh1 = new TaskCompletionSource <object>(); var hub1 = connection1.CreateHubProxy("SendToSome"); await connection1.Start(host); hub1.On("send", () => wh1.TrySetResult(null)); hubContext.Clients.Clients(new[] { connection1.ConnectionId }).send(); await wh1.Task.OrTimeout(TimeSpan.FromSeconds(10)); } } }
public async Task PrefixMatchingIsNotGreedy() { using (var host = new MemoryHost()) { host.Configure(app => { var config = new ConnectionConfiguration { Resolver = new DefaultDependencyResolver() }; app.MapSignalR <MyConnection>("/echo", config); app.MapSignalR <MyConnection2>("/echo2", config); }); var tcs = new TaskCompletionSource <string>(); var connection = new Connection("http://foo/echo2"); using (connection) { connection.Received += data => { tcs.TrySetResult(data); }; await connection.Start(host); var ignore = connection.Send(""); var dataReceived = await tcs.Task.OrTimeout(); Assert.Equal("MyConnection2", dataReceived); } } }
private static void UseScaleout(int nodes, out MemoryHost[] hosts, out IHttpClient client) { hosts = new MemoryHost[nodes]; var eventBus = new EventBus(); var protectedData = new DefaultProtectedData(); for (var i = 0; i < nodes; ++i) { var host = new MemoryHost(); host.Configure(app => { var config = new HubConfiguration() { Resolver = new DefaultDependencyResolver() }; var bus = new DelayedMessageBus(host.InstanceName, eventBus, config.Resolver, TimeSpan.Zero); config.Resolver.Register(typeof(IMessageBus), () => bus); app.MapSignalR(config); config.Resolver.Register(typeof(IProtectedData), () => protectedData); }); hosts[i] = host; } client = new LoadBalancer(hosts); }
public async Task UnauthenticatedUserCannotReceiveHubMessagesFromAuthorizedHubs() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("AuthHub"); await Assert.ThrowsAnyAsync <Exception>(() => connection.Start(host).OrTimeout()); } } }
public async Task AuthenticatedUserCanReceiveHubMessagesFromIncomingAuthorizedHubs() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity("Admin"), new string[] { })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("IncomingAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string, object>("joined", (id, time, authInfo) => { Assert.NotNull(id); wh.Set(); }); await connection.Start(host); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3))); } } }
public async Task UnauthenticatedUserCannotInvokeMethodsWhenAuthenticationRequiredGlobally() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; configuration.Resolver.Resolve <IHubPipeline>().RequireAuthentication(); WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("NoAuthHub"); await Assert.ThrowsAnyAsync <Exception>(() => connection.Start(host).OrTimeout()); } } }
public async Task AuthenticatedUserCanReceiveHubMessagesWhenAuthenticationRequiredGlobally() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; configuration.Resolver.Resolve <IHubPipeline>().RequireAuthentication(); WithUser(app, new GenericPrincipal(new GenericIdentity("test"), new string[] { })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("NoAuthHub"); var wh = new TaskCompletionSource <object>(); hub.On <string, string, object>("joined", (id, time, authInfo) => { Assert.NotNull(id); wh.TrySetResult(null); }); await connection.Start(host); await wh.Task.OrTimeout(); } } }
public async Task UnauthenticatedUserCannotInvokeMethodsInIncomingAuthorizedHubs() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { "User", "NotAdmin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("IncomingAuthHub"); await connection.Start(host); await Assert.ThrowsAnyAsync <Exception>(() => hub.Invoke("InvokedFromClient").OrTimeout()); } } }
public void AuthenticatedUserCanReceiveHubMessagesWhenAuthenticationRequiredGlobally() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; configuration.Resolver.Resolve <IHubPipeline>().RequireAuthentication(); WithUser(app, new GenericPrincipal(new GenericIdentity("test"), new string[] { })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("NoAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string, object>("joined", (id, time, authInfo) => { Assert.NotNull(id); wh.Set(); }); connection.Start(host).Wait(); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3))); } } }
public async Task UnauthenticatedUserCannotInvokeMethodsInIncomingAuthorizedHubs() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { "User", "NotAdmin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("IncomingAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string>("invoked", (id, time) => { Assert.NotNull(id); wh.Set(); }); await connection.Start(host); Assert.Throws <AggregateException>(() => hub.InvokeWithTimeout("InvokedFromClient")); Assert.False(wh.WaitOne(TimeSpan.FromSeconds(3))); } } }
public void UnauthorizedUserCannotReceiveHubMessagesFromHubsAuthorizedWithRoles() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity("test"), new string[] { "User", "NotAdmin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("AdminAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string, object>("joined", (id, time, authInfo) => { Assert.NotNull(id); wh.Set(); }); Assert.Throws <AggregateException>(() => connection.Start(host).Wait()); } } }
public void UnauthorizedUserCannotInvokeMethodsInHubsAuthorizedSpecifyingUserAndRole() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity("test"), new string[] { "User", "Admin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("UserAndRoleAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string>("invoked", (id, time) => { Assert.NotNull(id); wh.Set(); }); Assert.Throws <AggregateException>(() => connection.Start(host).Wait()); } } }
public async Task DisconnectFiresForHubsWhenClientDisconnects() { using (var host = new MemoryHost()) { var dr = new DefaultDependencyResolver(); var configuration = dr.Resolve <IConfigurationManager>(); var connectWh = new TaskCompletionSource <object>(); var disconnectWh = new TaskCompletionSource <object>(); host.Configure(app => { var config = new HubConfiguration { Resolver = dr }; app.MapSignalR("/signalr", config); configuration.DisconnectTimeout = TimeSpan.FromSeconds(6); dr.Register(typeof(MyHub), () => new MyHub(connectWh, disconnectWh)); }); var connection = new HubConnection("http://foo/"); connection.CreateHubProxy("MyHub"); await connection.Start(host); await connectWh.Task.OrTimeout(TimeSpan.FromSeconds(10)); ((Client.IConnection)connection).Disconnect(); await disconnectWh.Task.OrTimeout(TimeSpan.FromSeconds(20)); } }
public void UnauthenticatedUserCannotInvokeMethodsWhenAuthenticationRequiredGlobally() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; configuration.Resolver.Resolve <IHubPipeline>().RequireAuthentication(); WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("NoAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string>("invoked", (id, time) => { Assert.NotNull(id); wh.Set(); }); Assert.Throws <AggregateException>(() => connection.Start(host).Wait()); } } }
public void ReconnectDoesntFireAfterTimeOut(TransportType transportType, MessageBusType messageBusType) { using (var host = new MemoryHost()) { var conn = new MyReconnect(); host.Configure(app => { var config = new ConnectionConfiguration { Resolver = new DefaultDependencyResolver() }; UseMessageBus(messageBusType, config.Resolver); app.MapSignalR <MyReconnect>("/endpoint", config); var configuration = config.Resolver.Resolve <IConfigurationManager>(); configuration.DisconnectTimeout = TimeSpan.FromSeconds(6); configuration.ConnectionTimeout = TimeSpan.FromSeconds(2); configuration.KeepAlive = null; config.Resolver.Register(typeof(MyReconnect), () => conn); }); var connection = new Client.Connection("http://foo/endpoint"); var transport = CreateTransport(transportType, host); connection.Start(transport).Wait(); Thread.Sleep(TimeSpan.FromSeconds(5)); connection.Stop(); Assert.Equal(0, conn.Reconnects); } }
public void ConnectionsWithTheSameConnectionIdLongPollingCloseGracefully() { using (var host = new MemoryHost()) { host.Configure(app => { var config = new ConnectionConfiguration { Resolver = new DefaultDependencyResolver() }; app.MapSignalR <MyGroupEchoConnection>("/echo", config); config.Resolver.Register(typeof(IProtectedData), () => new EmptyProtectedData()); }); string id = Guid.NewGuid().ToString("d"); var tasks = new List <Task>(); for (int i = 0; i < 1000; i++) { tasks.Add(ProcessRequest(host, "longPolling", id)); } ProcessRequest(host, "longPolling", id); Task.WaitAll(tasks.ToArray()); Assert.True(tasks.All(t => !t.IsFaulted)); } }
public async Task AuthenticatedAndAuthorizedUserCanInvokeMethodsInHubsAuthorizedSpecifyingUserAndRole() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity("User"), new string[] { "Admin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("UserAndRoleAuthHub"); var wh = new ManualResetEvent(false); hub.On <string, string>("invoked", (id, time) => { Assert.NotNull(id); wh.Set(); }); await connection.Start(host); await hub.Invoke("InvokedFromClient").OrTimeout(); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3))); } } }
public async Task SendToSpecificClientsFromOutsideOfHub() { using (var host = new MemoryHost()) { IHubContext <IBasicClient> hubContext = null; host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; app.MapSignalR(configuration); hubContext = configuration.Resolver.Resolve <IConnectionManager>().GetHubContext <SendToSome, IBasicClient>(); }); var connection1 = new HubConnection("http://foo/"); using (connection1) { var wh1 = new AsyncManualResetEvent(initialState: false); var hub1 = connection1.CreateHubProxy("SendToSome"); await connection1.Start(host); hub1.On("send", wh1.Set); hubContext.Clients.Clients(new[] { connection1.ConnectionId }).send(); Assert.True(await wh1.WaitAsync(TimeSpan.FromSeconds(10))); } } }
public async Task UnauthenticatedUserCanInvokeMethodsByDefault() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("NoAuthHub"); var wh = new TaskCompletionSource <object>(); hub.On <string, string>("invoked", (id, time) => { Assert.NotNull(id); wh.TrySetResult(null); }); await connection.Start(host); await hub.Invoke("InvokedFromClient").OrTimeout(); await wh.Task.OrTimeout(); } } }
public async Task UnauthenticatedUserCanReceiveHubMessagesFromIncomingAuthorizedHubs() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity(""), new string[] { "User", "NotAdmin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("IncomingAuthHub"); var wh = new TaskCompletionSource <object>(); hub.On <string, string, object>("joined", (id, time, authInfo) => { Assert.NotNull(id); wh.TrySetResult(null); }); await connection.Start(host); await wh.Task.OrTimeout(TimeSpan.FromSeconds(3)); } } }
public async Task UnauthorizedUserCannotReceiveHubMessagesFromHubsAuthorizedSpecifyingUserAndRole() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity("User"), new string[] { "User", "NotAdmin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("UserAndRoleAuthHub"); hub.On <string, string, object>("joined", (id, time, authInfo) => { }); var ex = await Assert.ThrowsAsync <HttpClientException>(() => connection.Start(host)).OrTimeout(); Assert.Equal(HttpStatusCode.Forbidden, ex.Response.StatusCode); } } }
public async Task UnauthorizedUserCannotInvokeMethodsInHubsAuthorizedWithRoles() { using (var host = new MemoryHost()) { host.Configure(app => { var configuration = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; WithUser(app, new GenericPrincipal(new GenericIdentity("test"), new string[] { "User", "NotAdmin" })); app.MapSignalR("/signalr", configuration); }); var connection = CreateHubConnection("http://foo/"); using (connection) { var hub = connection.CreateHubProxy("AdminAuthHub"); hub.On <string, string>("invoked", (id, time) => { }); var ex = await Assert.ThrowsAsync <HttpClientException>(() => connection.Start(host)).OrTimeout(); Assert.Equal(HttpStatusCode.Forbidden, ex.Response.StatusCode); } } }
public async Task ConnectionIdsCantBeUsedAsGroups() { using (var host = new MemoryHost()) { IProtectedData protectedData = null; host.Configure(app => { var config = new ConnectionConfiguration { Resolver = new DefaultDependencyResolver() }; app.MapSignalR <MyConnection>("/echo", config); protectedData = config.Resolver.Resolve <IProtectedData>(); }); var connection = new Client.Connection("http://memoryhost/echo"); using (connection) { var connectionTcs = new TaskCompletionSource <string>(); var spyTcs = new TaskCompletionSource <string>(); connection.Received += data => { connectionTcs.SetResult(data.Trim()); }; await connection.Start(host).OrTimeout(); EventSourceStreamReader reader = null; await Task.Run(async() => { var url = GetUrl(protectedData, connection); var response = await host.Get(url, r => { }, isLongRunning: true); reader = new EventSourceStreamReader(connection, response.GetStream()); reader.Message = sseEvent => { if (sseEvent.EventType == EventType.Data && sseEvent.Data != "initialized" && sseEvent.Data != "{}") { spyTcs.TrySetResult(sseEvent.Data); } }; reader.Start(); }); await connection.Send("STUFFF").OrTimeout(); Assert.Equal("STUFFF", await connectionTcs.Task.OrTimeout()); } } }
public async Task NoReconnectsAfterFallbackDueToDisconnect() { using (var host = new MemoryHost()) { var serverReconnects = 0; host.Configure(app => { Func <AppFunc, AppFunc> middleware = (next) => { return(env => { var request = new OwinRequest(env); var response = new OwinResponse(env); if (!request.Path.Value.Contains("negotiate") && !request.QueryString.Value.Contains("longPolling")) { return Task.CompletedTask; } return next(env); }); }; app.Use(middleware); var config = new ConnectionConfiguration { Resolver = new DefaultDependencyResolver() }; config.Resolver.Register(typeof(MyReconnect), () => new MyReconnect(() => serverReconnects++)); config.Resolver.Resolve <IConfigurationManager>().TransportConnectTimeout = TimeSpan.FromSeconds(60); app.MapSignalR <MyReconnect>("/echo", config); }); var connection = new Connection("http://foo/echo"); using (connection) { var clientReconnects = 0; connection.Reconnecting += () => { clientReconnects++; }; await connection.Start(host).OrTimeout(); // Give SSE an opportunity to reconnect await Task.Delay(TimeSpan.FromSeconds(5)); Assert.Equal(connection.State, ConnectionState.Connected); Assert.Equal(connection.Transport.Name, "longPolling"); Assert.Equal(0, serverReconnects); Assert.Equal(0, clientReconnects); } } }