Пример #1
0
        public void SocketReader_ReadBytesIntoResponseHandler_WaitsForHandlerToComplete()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendString("This is a test. This sentence isn't read.", Encoding.UTF8);

            MockResponseHandler handler = new MockResponseHandler(Encoding.UTF8);

            handler.Block = true;

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <int> result = reader.ReadBytesIntoResponseHandler(15, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.NotCompleted(result, "Before handler is unblocked.");

            // Act
            handler.Block = false;

            // Assert
            TaskAssert.ResultEquals(result, 15, "After handler is unblocked.");
            Assert.Equal("This is a test.", handler.Response);
        }
Пример #2
0
        public void SocketReader_ReadBytesIntoResponseHandler_StopsReadingBytesIfCancelled()
        {
            // Arrange
            string stringToSend = "123456789";

            byte[] bufferToSend = Encoding.ASCII.GetBytes(stringToSend);

            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendBytes(bufferToSend, 0, bufferToSend.Length);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            CancellationTokenSource cts = new CancellationTokenSource();

            SocketReader reader = new SocketReader(socket);
            Task <int>   task   = reader.ReadBytesIntoResponseHandler(bufferToSend.Length + 1, handler.HandlerMethod, cts.Token);

            // Act
            cts.Cancel();

            // Assert
            TaskAssert.ResultEquals(task, bufferToSend.Length);
            Assert.Equal(stringToSend, handler.Response);
        }
Пример #3
0
        public void SocketReader_ReadBytesIntoResponseHandler_ReadsMoreBytes()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendString("This is a test. This is the next sentence.", Encoding.ASCII);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <int> result = reader.ReadBytesIntoResponseHandler(15, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, 15);
            Assert.Equal("This is a test.", handler.Response);

            // Act
            result = reader.ReadBytesIntoResponseHandler(27, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, 27);
            Assert.Equal("This is a test. This is the next sentence.", handler.Response);
        }
Пример #4
0
        public void SocketReader_ReadBytesIntoResponseHandler_WaitsForData()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            MockResponseHandler handler = new MockResponseHandler(Encoding.Unicode);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <int> result = reader.ReadBytesIntoResponseHandler(30, handler.HandlerMethod, _dontCancel);

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

            // Act
            socket.SendString("This is ", Encoding.Unicode);

            // Assert
            TaskAssert.NotCompleted(result, "After some data sent");

            // Act
            socket.SendString("a test. And then some.", Encoding.Unicode);

            // Assert
            TaskAssert.ResultEquals(result, 30, "After all data sent");
            Assert.Equal("This is a test.", handler.Response);
        }
Пример #5
0
        public void HttpSocketAdapter_ReadsContentIntoResponseHandler()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();

            serverSocket.SendString("HTTP/1.1 200 OK\r\n", Encoding.ASCII);
            serverSocket.SendString("Content-Length: 40\r\n\r\n", Encoding.ASCII);
            serverSocket.SendString("1234567890", Encoding.Unicode);

            HttpSocketAdapter clientSocket = new HttpSocketAdapter("GET", new Uri("http://bing.com"), serverSocket);

            MockResponseHandler handler = new MockResponseHandler(Encoding.Unicode);

            // Act
            clientSocket.SetResponseHandler(handler.HandlerMethod);

            // Assert
            Assert.Equal("1234567890", handler.Response);

            // Act
            Task responseComplete = clientSocket.WaitForResponseComplete();

            // Assert
            TaskAssert.NotCompleted(responseComplete, "After sending first chunk of data");

            // Act - Send too much data to complete Content-Length
            serverSocket.SendString("abcdefghijklmnopqrstuvwxyz", Encoding.Unicode);

            // Assert - Only Content-Length was read
            Assert.Equal("1234567890abcdefghij", handler.Response);
            TaskAssert.Completed(responseComplete, "After sending remaining data");
        }
Пример #6
0
        public void HttpSocketAdapter_ReadsChunkedContentIntoResponseHandler()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();

            serverSocket.SendString("HTTP/1.1 200 OK\r\n", Encoding.ASCII);
            serverSocket.SendString("Transfer-Encoding: chunked\r\n\r\n", Encoding.ASCII);

            HttpSocketAdapter clientSocket = new HttpSocketAdapter("GET", new Uri("http://bing.com"), serverSocket);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            // Act
            clientSocket.SetResponseHandler(handler.HandlerMethod);

            // Assert
            Assert.Equal("", handler.Response);

            // Act
            serverSocket.SendString("C\r\nHello, world\r\n", Encoding.ASCII);

            // Assert
            Assert.Equal("Hello, world", handler.Response);

            // Act
            Task responseComplete = clientSocket.WaitForResponseComplete();

            // Assert
            TaskAssert.NotCompleted(responseComplete, "After sending 'Hello, World'");

            // Act
            serverSocket.SendString("8\r\nwide", Encoding.ASCII);

            // Assert
            Assert.Equal("Hello, worldwide", handler.Response);
            TaskAssert.NotCompleted(responseComplete, "After sending 'wide'");

            // Act
            serverSocket.SendString("!!\r\n", Encoding.ASCII);

            // Assert
            Assert.Equal("Hello, worldwide!!\r\n", handler.Response);
            TaskAssert.NotCompleted(responseComplete, "after sending '!!\r\n'");

            // Act
            serverSocket.SendString("\r\n", Encoding.ASCII);

            // Assert
            Assert.Equal("Hello, worldwide!!\r\n", handler.Response);
            TaskAssert.NotCompleted(responseComplete, "after sending '\r\n'");

            // Act
            serverSocket.SendString("0\r\n", Encoding.ASCII);

            // Assert
            Assert.Equal("Hello, worldwide!!\r\n", handler.Response);
            TaskAssert.Completed(responseComplete, "after completing the response");
        }
Пример #7
0
        public void HttpSocketAdapter_ThrowsExceptionForMalformedChunkedResponse()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();

            serverSocket.SendString("HTTP/1.1 200 OK\r\n", Encoding.ASCII);
            serverSocket.SendString("Transfer-Encoding: chunked\r\n\r\n", Encoding.ASCII);

            HttpSocketAdapter clientSocket = new HttpSocketAdapter("GET", new Uri("http://bing.com"), serverSocket);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            clientSocket.SetResponseHandler(handler.HandlerMethod);

            // Act
            serverSocket.SendString("Hi!\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.Faulted(clientSocket.WaitForResponseComplete(), "ResponseReader should throw an exception if the response is malformed. Otherwise, the response will be blank and there will be nothing pointing to why it happened.");
        }
Пример #8
0
        public void SocketReader_ReadBytesIntoResponseHandler_ContinuesWaitingForBytes()
        {
            // Arrange
            string stringToSend = "123456789";

            byte[] bufferToSend = Encoding.ASCII.GetBytes(stringToSend);

            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendBytes(bufferToSend, 0, bufferToSend.Length);

            MockResponseHandler handler = new MockResponseHandler(Encoding.ASCII);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <int> task = reader.ReadBytesIntoResponseHandler(bufferToSend.Length + 1, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.NotCompleted(task);
        }
Пример #9
0
        public void SocketReader_ReadBytesIntoResponseHandler_ReadMoreBytesThanBuffer()
        {
            // Arrange
            string stringToSend = new String('Σ', count: 20000);

            byte[] bufferToSend = Encoding.UTF32.GetBytes(stringToSend);

            MockSocketAdapter socket = new MockSocketAdapter();

            socket.SendBytes(bufferToSend, 0, bufferToSend.Length);

            MockResponseHandler handler = new MockResponseHandler(Encoding.UTF32);

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <int> result = reader.ReadBytesIntoResponseHandler(bufferToSend.Length, handler.HandlerMethod, _dontCancel);

            // Assert
            TaskAssert.ResultEquals(result, bufferToSend.Length);
            Assert.Equal(stringToSend, handler.Response);
        }