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"); }
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"); }
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."); }