Exemple #1
0
        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());
                }
            }
        }
Exemple #2
0
 private static Task ProcessRequest(MemoryHost host, string transport, string connectionToken)
 {
     return(host.Get("http://foo/echo/connect?transport=" + transport + "&connectionToken=" + connectionToken));
 }
Exemple #3
0
        public async Task GroupsTokenIsPerConnectionId()
        {
            using (var host = new MemoryHost())
            {
                IProtectedData protectedData = null;

                host.Configure(app =>
                {
                    var config = new HubConfiguration
                    {
                        Resolver = new DefaultDependencyResolver()
                    };

                    app.MapSignalR <MyGroupConnection>("/echo", config);

                    protectedData = config.Resolver.Resolve <IProtectedData>();
                });

                var connection = new Client.Connection("http://memoryhost/echo");

                using (connection)
                {
                    var inGroup = new AsyncManualResetEvent();

                    connection.Received += data =>
                    {
                        if (data == "group")
                        {
                            inGroup.Set();
                        }
                    };

                    await connection.Start(host);

                    await inGroup.WaitAsync(TimeSpan.FromSeconds(10));

                    Assert.NotNull(connection.GroupsToken);

                    var spyWh            = new AsyncManualResetEvent();
                    var hackerConnection = new Client.Connection(connection.Url)
                    {
                        ConnectionId = "hacker"
                    };

                    var url      = GetUrl(protectedData, connection, connection.GroupsToken);
                    var response = await host.Get(url, r => { }, isLongRunning : true);

                    var reader = new EventSourceStreamReader(hackerConnection, response.GetStream());

                    reader.Message = sseEvent =>
                    {
                        if (sseEvent.EventType == EventType.Data &&
                            sseEvent.Data != "initialized" &&
                            sseEvent.Data != "{}")
                        {
                            spyWh.Set();
                        }
                    };

                    reader.Start();
                    await connection.Send("random");

                    Assert.False(await spyWh.WaitAsync(TimeSpan.FromSeconds(5)));
                }
            }
        }
Exemple #4
0
        public void ConnectionIdsCantBeUsedAsGroups()
        {
            using (var host = new MemoryHost())
            {
                IProtectedData protectedData = null;

                host.Configure(app =>
                {
                    var config = new ConnectionConfiguration
                    {
                        Resolver = new DefaultDependencyResolver()
                    };

                    app.MapConnection <MyConnection>("/echo", config);

                    protectedData = config.Resolver.Resolve <IProtectedData>();
                });

                var connection = new Client.Connection("http://memoryhost/echo");

                var connectionTcs = new TaskCompletionSource <string>();
                var spyTcs        = new TaskCompletionSource <string>();

                connection.Received += data =>
                {
                    connectionTcs.SetResult(data.Trim());
                };

                connection.Start(host).Wait();

                var tcs = new TaskCompletionSource <object>();
                EventSourceStreamReader reader = null;

                Task.Run(async() =>
                {
                    try
                    {
                        string url   = GetUrl(protectedData, connection);
                        var response = await host.Get(url);
                        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();
                        tcs.TrySetResult(null);
                    }
                    catch (Exception ex)
                    {
                        tcs.TrySetException(ex);
                    }
                });

                tcs.Task.Wait();

                connection.SendWithTimeout("STUFFF");

                Assert.True(connectionTcs.Task.Wait(TimeSpan.FromSeconds(5)));
                Assert.Equal("STUFFF", connectionTcs.Task.Result);
                Assert.False(spyTcs.Task.Wait(TimeSpan.FromSeconds(5)));

                connection.Stop();
            }
        }
Exemple #5
0
 public override Task Get(string uri)
 {
     return(_host.Get(uri, r => { }, isLongRunning: false));
 }
 public override Task Get(string uri, bool disableWrites)
 {
     return(_host.Get(uri, disableWrites));
 }