Exemplo n.º 1
0
        public void SocketReader_ReadLine_WaitsForMoreDataWhenLineIncomplete()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <string> result = reader.ReadLine(_dontCancel);

            // Assert
            TaskAssert.NotCompleted(result, "Before sending data.");

            // Act
            socket.SendString("This is the first part of the string. ", Encoding.ASCII);

            // Assert
            TaskAssert.NotCompleted(result, "After sending first sentence.");

            // Act
            socket.SendString("This is the second part of the string.\r", Encoding.ASCII);

            // Assert
            TaskAssert.NotCompleted(result, "After sending second sentence.");

            // Act
            socket.SendString("\nThis is an extra, third sentence.\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.ResultEquals(result, "This is the first part of the string. This is the second part of the string.", "After sending data.");
        }
Exemplo n.º 2
0
            private async Task <int> ReadStatusLine()
            {
                string statusLine = await _socketReader.ReadLine(_cancelSource.Token);

                string[] parts = statusLine.Split(StatusLineSeparators, count: 3);

                if (parts.Length >= 2)
                {
                    int statusCode;

                    if (Int32.TryParse(parts[1], out statusCode))
                    {
                        return(statusCode);
                    }
                }

                // Status line was in an unexpected format. Assume "500: Internal Server Error"
                return(500);
            }
Exemplo n.º 3
0
        public void SocketReader_ReadLine_IgnoresUnmatchedLineFeed()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendString("This is a test\rof an unmatched \n\r\n", Encoding.ASCII);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <string> result = reader.ReadLine(_dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, "This is a test\rof an unmatched \n");
        }
Exemplo n.º 4
0
        public void SocketReader_ReadLine_ReadsOneLine()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendString("This is a test.\r\nThis is left out.\r\n", Encoding.ASCII);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <string> result = reader.ReadLine(_dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, "This is a test.");
        }
Exemplo n.º 5
0
        public void SocketReader_ReadLine_ReturnsEmptyStringWhenCancelled()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            CancellationTokenSource cts = new CancellationTokenSource();

            SocketReader  reader     = new SocketReader(socket);
            Task <string> lineResult = reader.ReadLine(cts.Token);

            // Act
            cts.Cancel();

            // Assert
            TaskAssert.ResultEquals(lineResult, String.Empty, "Task should return empty string when canceled.");
        }
Exemplo n.º 6
0
        public void SocketReader_ReadLine_ReadsLineLongerThanBuffer()
        {
            // Arrange
            string lineToSend = new String('Σ', count: 20000);

            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendString(lineToSend + "\r\n", Encoding.Unicode);

            SocketReader reader = new SocketReader(socket);

            reader.SetEncoding(Encoding.Unicode);

            // Act
            Task <string> result = reader.ReadLine(_dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, lineToSend);
        }
Exemplo n.º 7
0
        public void SocketReader_ReadLine_WaitsForDataReadingOneLine()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <string> result = reader.ReadLine(_dontCancel);

            // Assert
            TaskAssert.NotCompleted(result, "Before sending data.");

            // Act
            socket.SendString("This is a test.\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.ResultEquals(result, "This is a test.", "After sending data.");
        }