public void ForeverFrameTransportEscapesTags(string data, string expected)
        {
            var context = new TestContext("/");
            var sp = ServiceProviderHelper.CreateServiceProvider();

            var ms = new MemoryStream();
            context.MockResponse.Setup(m => m.Body).Returns(ms);

            var fft = ActivatorUtilities.CreateInstance<ForeverFrameTransport>(sp, context.MockHttpContext.Object);

            AssertEscaped(fft, ms, data, expected);
        }
        public void ForeverFrameTransportThrowsOnInvalidFrameId(string frameId)
        {
            var qs = new Dictionary<string, string> { { "frameId", frameId } };
            var context = new TestContext("/", qs);
            var sp = ServiceProviderHelper.CreateServiceProvider();

            var ms = new MemoryStream();
            context.MockResponse.Setup(m => m.Body).Returns(ms);

            var fft = ActivatorUtilities.CreateInstance<ForeverFrameTransport>(sp, context.MockHttpContext.Object);
            var connection = new Mock<ITransportConnection>();

            Assert.Throws(typeof(InvalidOperationException), () => fft.InitializeResponse(connection.Object));
        }
            public void MissingConnectionTokenFails()
            {
                var connection = new Mock<PersistentConnection>() { CallBase = true };
                var qs = new Dictionary<string, string>();
                var sp = ServiceProviderHelper.CreateServiceProvider();
                var context = new TestContext("/", qs);
                context.MockResponse.SetupProperty(r => r.StatusCode);
                connection.Object.Initialize(sp);

                var task = connection.Object.ProcessRequest(context.MockHttpContext.Object);

                Assert.True(task.IsCompleted);
                Assert.Equal(400, context.MockResponse.Object.StatusCode);
            }
        public void ProtocolResolvesCorrectly(string minProtocol, string maxProtocol, string clientProtocol, string expectedProtocol)
        {
            var minProtocolVersion = new Version(minProtocol);
            var maxProtocolVersion = new Version(maxProtocol);
            var protocolResolver = new ProtocolResolver(minProtocolVersion, maxProtocolVersion);

            var queryStrings = new Dictionary<string, string>
            {
                {  "clientProtocol", clientProtocol }
            };

            var context = new TestContext("/negotite", queryStrings);

            var version = protocolResolver.Resolve(context.MockRequest.Object);

            Assert.Equal(version, new Version(expectedProtocol));
        }
        public void ForeverFrameTransportSetsCorrectContentType()
        {
            var qs = new Dictionary<string, string> {
                { "frameId", "1" }
            };
            var context = new TestContext("/", qs);
            var sp = ServiceProviderHelper.CreateServiceProvider();

            var ms = new MemoryStream();
            context.MockResponse.SetupAllProperties();
            context.MockResponse.Setup(m => m.Body).Returns(ms);

            var fft = ActivatorUtilities.CreateInstance<ForeverFrameTransport>(sp, context.MockHttpContext.Object);
            fft.ConnectionId = "1";
            var connection = new Mock<ITransportConnection>();

            fft.InitializeResponse(connection.Object).Wait();

            Assert.Equal("text/html; charset=UTF-8", context.MockResponse.Object.ContentType);
        }
        public void ServerSentEventsTransportDisablesRequestBuffering()
        {
            var context = new TestContext("/");
            var sp = ServiceProviderHelper.CreateServiceProvider();

            var ms = new MemoryStream();
            var buffering = new Mock<IHttpBufferingFeature>();

            context.MockHttpContext.Setup(m => m.GetFeature<IHttpBufferingFeature>())
                .Returns(buffering.Object);
            context.MockResponse.SetupAllProperties();
            context.MockResponse.Setup(m => m.Body).Returns(ms);

            var sst = ActivatorUtilities.CreateInstance<ServerSentEventsTransport>(sp, context.MockHttpContext.Object);
            sst.ConnectionId = "1";
            var connection = new Mock<ITransportConnection>();

            sst.InitializeResponse(connection.Object).Wait();

            buffering.Verify(m => m.DisableRequestBuffering(), Times.Once());
        }
            public void UncleanDisconnectFiresOnDisconnected()
            {
                // Arrange
                var context = new TestContext("/", new Dictionary<string, string> { { "connectionToken", "1" } });

                var transport = new Mock<ITransport>();
                transport.SetupProperty(m => m.Disconnected);
                transport.SetupProperty(m => m.ConnectionId);
                transport.Setup(m => m.GetGroupsToken()).Returns(TaskAsyncHelper.FromResult(string.Empty));
                transport.Setup(m => m.ProcessRequest(It.IsAny<Connection>())).Returns(TaskAsyncHelper.Empty);

                var transportManager = new Mock<ITransportManager>();
                transportManager.Setup(m => m.GetTransport(context.MockHttpContext.Object)).Returns(transport.Object);

                var protectedData = new Mock<IProtectedData>();
                protectedData.Setup(m => m.Unprotect(It.IsAny<string>(), It.IsAny<string>()))
                             .Returns<string, string>((value, purpose) =>  value);

                var connection = new Mock<PersistentConnection>() { CallBase = true };
                var onDisconnectedCalled = false;
                connection.Protected().Setup("OnDisconnected", context.MockRequest.Object, "1", false).Callback(() =>
                {
                    onDisconnectedCalled = true;
                });

                var sp = ServiceProviderHelper.CreateServiceProvider(services =>
                {
                    services.AddInstance<ITransportManager>(transportManager.Object);
                    services.AddInstance<IProtectedData>(protectedData.Object);
                });

                connection.Object.Initialize(sp);

                // Act
                connection.Object.ProcessRequest(context.MockHttpContext.Object).Wait();
                transport.Object.Disconnected(/* clean: */ false);

                // Assert
                Assert.True(onDisconnectedCalled);
            }
        public void ForeverFrameTransportDisablesRequestBuffering()
        {
            var qs = new Dictionary<string, string> {
                { "frameId", "1" }
            };
            var context = new TestContext("/", qs);
            var sp = ServiceProviderHelper.CreateServiceProvider();

            var ms = new MemoryStream();
            var buffering = new Mock<IHttpBufferingFeature>();

            context.MockHttpContext.Setup(m => m.Features.Get<IHttpBufferingFeature>())
                .Returns(buffering.Object);
            context.MockResponse.SetupAllProperties();
            context.MockResponse.Setup(m => m.Body).Returns(ms);

            var fft = ActivatorUtilities.CreateInstance<ForeverFrameTransport>(sp, context.MockHttpContext.Object);
            fft.ConnectionId = "1";
            var connection = new Mock<ITransportConnection>();

            fft.InitializeResponse(connection.Object).Wait();

            buffering.Verify(m => m.DisableRequestBuffering(), Times.Once());
        }
            public void AuthenticatedUserWithColonsInUserName()
            {
                var connection = new Mock<PersistentConnection>() { CallBase = true };
                var context = new TestContext("/");
                context.MockHttpContext.Setup(m => m.User)
                       .Returns(new ClaimsPrincipal(new GenericIdentity("::11:::::::1:1")));

                string connectionId = Guid.NewGuid().ToString("d");

                var protectedData = new Mock<IProtectedData>();
                protectedData.Setup(m => m.Protect(It.IsAny<string>(), It.IsAny<string>()))
                    .Returns<string, string>((value, purpose) => value);
                protectedData.Setup(m => m.Unprotect(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((value, purpose) => value);

                var sp = ServiceProviderHelper.CreateServiceProvider(services =>
                {
                    services.AddInstance<IProtectedData>(protectedData.Object);
                });

                connection.Object.Initialize(sp);

                string cid;
                string message;
                int statusCode;

                Assert.Equal(true, connection.Object.TryGetConnectionId(context.MockHttpContext.Object, connectionId + ":::11:::::::1:1", out cid, out message, out statusCode));
                Assert.Equal(connectionId, cid);
            }
            public void UnauthenticatedUserWithAuthenticatedTokenFails()
            {
                var connection = new Mock<PersistentConnection>() { CallBase = true };
                var context = new TestContext("/");

                var protectedData = new Mock<IProtectedData>();
                protectedData.Setup(m => m.Protect(It.IsAny<string>(), It.IsAny<string>()))
                    .Returns<string, string>((value, purpose) => value);
                protectedData.Setup(m => m.Unprotect(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((value, purpose) => value);

                var sp = ServiceProviderHelper.CreateServiceProvider(services =>
                {
                    services.AddInstance<IProtectedData>(protectedData.Object);
                });

                connection.Object.Initialize(sp);

                string connectionId;
                string message;
                int statusCode;

                Assert.Equal(false, connection.Object.TryGetConnectionId(context.MockHttpContext.Object, "1:::11:::::::1:1", out connectionId, out message, out statusCode));
                Assert.Equal(403, statusCode);
            }
            private static IList<string> DoVerifyGroups(string groupsToken, string connectionId, bool hasProtectedData = true)
            {
                var connection = new Mock<PersistentConnection>() { CallBase = true };
                var qs = new Dictionary<string, string>();
                var context = new TestContext("/", qs);
                context.MockResponse.SetupProperty(r => r.StatusCode);
                qs["transport"] = "serverSentEvents";
                qs["connectionToken"] = "1";
                qs["groupsToken"] = groupsToken;

                var protectedData = new Mock<IProtectedData>();
                protectedData.Setup(m => m.Protect(It.IsAny<string>(), It.IsAny<string>()))
                    .Returns<string, string>((value, purpose) => value);

                protectedData.Setup(m => m.Unprotect(It.IsAny<string>(), It.IsAny<string>()))
                             .Returns<string, string>((value, purpose) => hasProtectedData ? value : null);

                var sp = ServiceProviderHelper.CreateServiceProvider(services =>
                {
                    services.AddInstance<IProtectedData>(protectedData.Object);
                });

                connection.Object.Initialize(sp);

                return connection.Object.VerifyGroups(connectionId, groupsToken);
            }
            public void AuthenticatedUserNameMatches()
            {
                var connection = new Mock<PersistentConnection>() { CallBase = true };
                var context = new TestContext("/");
                context.MockHttpContext.Setup(m => m.User)
                       .Returns(new ClaimsPrincipal(new GenericIdentity("Name")));

                var protectedData = new Mock<IProtectedData>();
                protectedData.Setup(m => m.Protect(It.IsAny<string>(), It.IsAny<string>()))
                    .Returns<string, string>((value, purpose) => value);
                protectedData.Setup(m => m.Unprotect(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((value, purpose) => value);

                var sp = ServiceProviderHelper.CreateServiceProvider(services =>
                {
                    services.AddSingleton<IProtectedData>(protectedData.Object);
                });

                connection.Object.Initialize(sp);

                string connectionId;
                string message;
                int statusCode;

                Assert.Equal(true, connection.Object.TryGetConnectionId(context.MockHttpContext.Object, "1:Name", out connectionId, out message, out statusCode));
                Assert.Equal("1", connectionId);
            }
            public void UnprotectedConnectionTokenFails()
            {
                var connection = new Mock<PersistentConnection>() { CallBase = true };
                var context = new TestContext("/");

                var protectedData = new Mock<IProtectedData>();
                protectedData.Setup(m => m.Protect(It.IsAny<string>(), It.IsAny<string>()))
                    .Returns<string, string>((value, purpose) => value);
                protectedData.Setup(m => m.Unprotect(It.IsAny<string>(), It.IsAny<string>()))
                             .Throws<InvalidOperationException>();

                var sp = ServiceProviderHelper.CreateServiceProvider(services =>
                {
                    services.AddSingleton<IProtectedData>(protectedData.Object);
                });

                connection.Object.Initialize(sp);

                string connectionId;
                string message;
                int statusCode;

                Assert.Equal(false, connection.Object.TryGetConnectionId(context.MockHttpContext.Object, "1", out connectionId, out message, out statusCode));
                Assert.Equal(null, connectionId);
                Assert.Equal(400, statusCode);
            }