Exemplo n.º 1
0
        public void CheckMessageWhenInvalidFormatShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string invalidFormatData = "userName_sentMessage_lastMessage";

            mockSocket.TextToReceive = invalidFormatData;

            Assert.Single(invalidFormatData.Split("<sep>"));
            Assert.Throws <InvalidOperationException>(() => server.CheckMessage(mockSocket));
        }
Exemplo n.º 2
0
        public void CheckMessageWhenEmptySecondArgumentShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string trimmedReceivedData = "userName<sep><sep>lastMessageReceived<eof>";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.True(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Throws <ArgumentException>(() => server.CheckMessage(mockSocket));
        }
Exemplo n.º 3
0
        public void CheckMessageWhenNoEOFTagShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string trimmedReceivedData = "userName<sep>sentMessage<sep>lastMessageReceived";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.False(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Throws <InvalidOperationException>(() => server.CheckMessage(mockSocket));
        }
Exemplo n.º 4
0
        public void CheckMessageWhenAnyShouldHaveValidFormat()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string trimmedReceivedData = "userName<sep>sentMessage<sep>lastMessageReceived<eof>";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.True(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Equal("sentMessage", server.CheckMessage(mockSocket));
        }
Exemplo n.º 5
0
        public void CheckMessageWhenNewUserShouldSendOnlyGreetingMessage()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string initialTestMessage = "someUser<sep>Initial message<sep>testLastMessageReceived<eof>";

            mockSocket.TextToReceive = initialTestMessage;

            server.CheckMessage(mockSocket);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("server: someUser joined the chat.<eof>", item));

            string testMessage = "someUser<sep>Initial message<sep>server: someUser joined the chat.<eof>";

            mockSocket.TextToReceive = testMessage;

            Assert.False(server.IsNewUser("someUser"));
            Assert.Equal(3, testMessage.Split("<sep>").Length);
            Assert.True(testMessage.IndexOf("<eof>") > -1);
            Assert.Equal("Initial message", server.CheckMessage(mockSocket));
            Assert.False(server.IsNewUser("someUser"));

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("server: someUser joined the chat.<eof>", item),
                              item => Assert.Equal("someUser: Initial message<eof>", item));

            string trimmedReceivedData = "newUser<sep>sentMessage<sep>lastMessageReceived<eof>";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.True(server.IsNewUser("newUser"));
            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.True(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Equal("sentMessage", server.CheckMessage(mockSocket));
            Assert.False(server.IsNewUser("newUser"));

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("server: someUser joined the chat.<eof>", item),
                              item => Assert.Equal("someUser: Initial message<eof>", item),
                              item => Assert.Equal("server: newUser joined the chat.<eof>", item));
        }