public void FahClientCommand_ExecuteAsyncThrowsInvalidOperationExceptionWhenConnectionIsNotConnected()
        {
            // Arrange
            var command = new FahClientCommand(new FahClientConnection("foo", 2000));

            // Act & Assert
            Assert.ThrowsAsync <InvalidOperationException>(() => command.ExecuteAsync());
        }
 public void FahClientCommand_ExecuteAsyncThrowsInvalidOperationExceptionWhenTcpConnectionIsNoLongerConnected()
 {
     // Arrange
     using (var connection = new MockFahClientConnection())
     {
         connection.Open();
         var command = new FahClientCommand(connection);
         // Act & Assert
         Assert.ThrowsAsync <InvalidOperationException>(() => command.ExecuteAsync());
     }
 }
        public void FahClientCommand_ExecuteAsyncRethrowsExceptionFromStreamWriteAsyncAndClosesTheConnection()
        {
            // Arrange
            Func <TcpConnection> factory = () => new MockTcpConnection(() => new MockStreamThrowsOnWrite());

            using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory(factory)))
            {
                connection.Open();
                var command = new FahClientCommand(connection);
                // Act & Assert
                Assert.ThrowsAsync <IOException>(() => command.ExecuteAsync());
                Assert.IsFalse(connection.Connected);
            }
        }
        public void FahClientCommand_ExecuteWritesNullCommandTextToConnection()
        {
            // Arrange
            var tcpConnectionFactory = new MockTcpConnectionFactory();

            using (var connection = new FahClientConnection("foo", 2000, tcpConnectionFactory))
            {
                connection.Open();
                var command = new FahClientCommand(connection, null);
                // Act
                int bytesWritten = command.Execute();
                // Assert
                Assert.AreEqual(0, bytesWritten);
                var memoryStream = (MemoryStream)tcpConnectionFactory.TcpConnection.GetStream();
                Assert.AreEqual("", Encoding.ASCII.GetString(memoryStream.ToArray()));
            }
        }
        public async Task FahClientCommand_ExecuteAsyncWritesCommandTextToConnection()
        {
            // Arrange
            var tcpConnectionFactory = new MockTcpConnectionFactory();

            using (var connection = new FahClientConnection("foo", 2000, tcpConnectionFactory))
            {
                connection.Open();
                var command = new FahClientCommand(connection, "command text");
                // Act
                int bytesWritten = await command.ExecuteAsync();

                // Assert
                Assert.AreEqual(13, bytesWritten);
                var memoryStream = (MemoryStream)tcpConnectionFactory.TcpConnection.GetStream();
                Assert.AreEqual("command text\n", Encoding.ASCII.GetString(memoryStream.ToArray()));
            }
        }