public async Task Handle_InitializeMessage_Accepted()
        {
            // arrange
            var connection = new SocketConnectionMock();
            var handler    = new InitializeConnectionMessageHandler(
                new SocketSessionInterceptorMock());
            var message = new InitializeConnectionMessage();

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                AcceptConnectionMessage.Default.Serialize()));
            },
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                KeepConnectionAliveMessage.Default.Serialize()));
            });
        }
        public async Task Handle_InitializeMessage_Rejected_With_Extensions()
        {
            // arrange
            var connectionStatus = ConnectionStatus.Reject(
                "Foo",
                new Dictionary <string, object> {
                { "bar", "baz" }
            });

            var interceptor = new SocketSessionInterceptorMock(connectionStatus);
            var connection  = new SocketConnectionMock();
            var handler     = new InitializeConnectionMessageHandler(interceptor);
            var message     = new InitializeConnectionMessage();

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new RejectConnectionMessage(
                                    connectionStatus.Message,
                                    connectionStatus.Extensions)
                                .Serialize()));
            });
            Assert.True(connection.Closed);
        }
        public async Task Handle_InitializeMessage_Rejected()
        {
            // arrange
            var interceptor = new Mock <IConnectMessageInterceptor>();

            interceptor.Setup(t => t.OnReceiveAsync(
                                  It.IsAny <ISocketConnection>(),
                                  It.IsAny <InitializeConnectionMessage>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(ConnectionStatus.Reject());

            var connection = new SocketConnectionMock();
            var handler    = new InitializeConnectionMessageHandler(
                interceptor.Object);
            var message = new InitializeConnectionMessage(null);

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new RejectConnectionMessage(
                                    ConnectionStatus.Reject().Message)
                                .Serialize()));
            });
        }
        public void CanHandle_InitializeMessage_True()
        {
            // arrange
            var handler = new InitializeConnectionMessageHandler(null);
            var message = new InitializeConnectionMessage(null);

            // act
            bool result = handler.CanHandle(message);

            // assert
            Assert.True(result);
        }
        public void CanHandle_InitializeMessage_True()
        {
            // arrange
            var handler = new InitializeConnectionMessageHandler(
                new SocketSessionInterceptorMock());
            var message = new InitializeConnectionMessage();

            // act
            var result = handler.CanHandle(message);

            // assert
            Assert.True(result);
        }
        public async Task Handle_InitializeMessage_Rejected_With_Extensions()
        {
            // arrange
            var connectionStatus = ConnectionStatus.Reject(
                "Foo",
                new Dictionary <string, object> {
                { "bar", "baz" }
            });

            var interceptor = new Mock <IConnectMessageInterceptor>();

            interceptor.Setup(t => t.OnReceiveAsync(
                                  It.IsAny <ISocketConnection>(),
                                  It.IsAny <InitializeConnectionMessage>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(connectionStatus);

            var connection = new SocketConnectionMock();
            var handler    = new InitializeConnectionMessageHandler(
                interceptor.Object);
            var message = new InitializeConnectionMessage(null);

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new RejectConnectionMessage(
                                    connectionStatus.Message,
                                    connectionStatus.Extensions)
                                .Serialize()));
            });
            Assert.True(connection.Closed);
        }
        public async Task Handle_InitializeMessage_Rejected()
        {
            // arrange
            var interceptor = new SocketSessionInterceptorMock(ConnectionStatus.Reject());
            var connection  = new SocketConnectionMock();
            var handler     = new InitializeConnectionMessageHandler(interceptor);
            var message     = new InitializeConnectionMessage();

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new RejectConnectionMessage(
                                    ConnectionStatus.Reject().Message)
                                .Serialize()));
            });
        }