Esempio n. 1
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");
        }
Esempio n. 2
0
        public void HttpSocketAdapter_GetResponseHeader_SocketException()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();

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

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

            Task <int>    responseCodeTask     = clientSocket.GetResponseStatusCode();
            Task <string> responseHeaderTask   = clientSocket.GetResponseHeader("test header");
            Task          responseCompleteTask = clientSocket.WaitForResponseComplete();

            TaskAssert.Completed(responseCodeTask, "GetResponseStatusCode should have failed.");
            AssertWithMessage.Equal(200, responseCodeTask.Result, "Wrong result for GetResponseStatusCode");

            // Act
            serverSocket.ThrowExceptionFromReceiveAsync();

            // Assert
            TaskAssert.Faulted(responseHeaderTask, "GetResponseHeader should have failed.");
            TaskAssert.Faulted(responseCompleteTask, "WaitForResponseComplete should have failed.");

            AssertWithMessage.Equal("An error occurred.", responseHeaderTask.Exception.InnerException.Message, "Wrong exception for GetResponseHeader");
            AssertWithMessage.Equal("An error occurred.", responseCompleteTask.Exception.InnerException.Message, "Wrong exception for WaitForResponseComplete");
        }
Esempio n. 3
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");
        }
Esempio n. 4
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.");
        }