Exemple #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");
        }
        public void ScriptInjectionFilterStream_CompletePassthroughProcess()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();
            MockScriptInjectionFilterContext filterContext = new MockScriptInjectionFilterContext(contentType: "text/css");

            ScriptInjectionFilterStream filterStream = CreateFilterStream(serverSocket, filterContext);

            byte[] bytesToSend = Encoding.UTF8.GetBytes("body { font-size: xxlarge; } p { background-color: red }");

            // Act I - Write some bytes
            filterStream.Write(bytesToSend, 0, 20);

            // Assert
            Assert.Equal("body { font-size: xx", filterContext.GetResponseBody(Encoding.UTF8));
            Assert.True(serverSocket.IsClosed, "Server connection should be closed.");

            // Act II - Write some more bytes
            filterStream.Write(bytesToSend, 20, bytesToSend.Length - 20);

            // Assert
            Assert.Equal("body { font-size: xxlarge; } p { background-color: red }", filterContext.GetResponseBody(Encoding.UTF8));

            // Act III - Wait for complete
            Task completeTask = filterStream.FlushAsync();

            // Assert
            TaskAssert.Completed(completeTask, "Flush should complete immediately, because the filter is not being used.");
        }
        public void ScriptInjectionFilterStream_BecomesPassthroughOnTimeout()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();
            MockScriptInjectionFilterContext filterContext = new MockScriptInjectionFilterContext();

            ScriptInjectionFilterStream filterStream = CreateFilterStream(serverSocket, filterContext);

            byte[] bytesToSend = Encoding.UTF8.GetBytes("<html><head></head><body></body></html>");

            // Act I - Send some bytes
            Task writeTask = filterStream.WriteAsync(bytesToSend, 0, 20);

            // Assert
            TaskAssert.NotCompleted(writeTask, "Should be waiting for server to respond");

            // Act II - Wait for the request to time out
            System.Threading.Thread.Sleep(1100);

            // Assert
            TaskAssert.Completed(writeTask, "Write should complete when server fails to respond");
            Assert.True(serverSocket.IsClosed, "Should become passthrough");
            Assert.Equal("<html><head></head><", filterContext.GetResponseBody(Encoding.UTF8));
            AssertWithMessage.Equal(true, filterStream.ScriptInjectionTimedOut, "ScriptInjectionTimedOut");
        }
Exemple #4
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");
        }
Exemple #5
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");
        }
        public void DelayConnectingHttpSocketAdapter_CompleteRequest_DoesNotConnectIfNoDataAndNoResponse()
        {
            // Arrange
            IHttpSocketAdapter delayAdapter = new DelayConnectingHttpSocketAdapter(DoNotConnect);

            // Act
            Task result = delayAdapter.CompleteRequest();

            // Assert
            TaskAssert.Completed(result, "CompleteRequest should complete immediately when there is no request.");
        }
        public void RevolvingBuffers_WaitForBufferEmptyAsync_ReturnsImmediatelyIfNoData()
        {
            // Arrange
            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            // Act
            Task task = buffers.WaitForBufferEmptyAsync();

            // Assert
            TaskAssert.Completed(task, "Task should be completed immediately.");
        }
        public void DelayConnectingHttpSocketAdapter_WriteToRequestAsync_DoesNothingAfterFailureToConnect()
        {
            // Arrange
            IHttpSocketAdapter delayAdapter = new DelayConnectingHttpSocketAdapter(FailToConnect);

            byte[] bytesToWrite = Encoding.ASCII.GetBytes("Hello, world!");

            // Act
            Task result = delayAdapter.WriteToRequestAsync(bytesToWrite, 7, 5);

            // Assert
            TaskAssert.Completed(result);
        }
        public void DelayConnectingHttpSocketAdapter_WriteToRequestAsync_DoesNotAttemptToConnectAfterFailure()
        {
            // Arrange
            IHttpSocketAdapter delayAdapter = new DelayConnectingHttpSocketAdapter(FailToConnect);

            byte[] bytesToWrite = Encoding.ASCII.GetBytes("Hello, world!");
            delayAdapter.WriteToRequestAsync(bytesToWrite, 7, 5);

            // Act
            Task result = delayAdapter.WriteToRequestAsync(bytesToWrite, 7, 5);

            // Assert
            //  If we got here, a second connection was not attempted
            TaskAssert.Completed(result);
        }
        public void RevolvingBuffers_GetBufferedDataAsync_ReturnsBufferedData()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            buffers.CopyDataToBuffer(data, 0, data.Length);

            // Act
            Task <ArraySegment <char> > task = buffers.GetBufferedDataAsync();

            // Assert
            TaskAssert.Completed(task, "Task should be completed immediately.");
            Assert.Equal("Hello, world!", GetString(task.Result));
        }
        public void ScriptInjectionFilterStream_CompleteFilterProcess()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();
            MockScriptInjectionFilterContext filterContext = new MockScriptInjectionFilterContext();

            ScriptInjectionFilterStream filterStream = CreateFilterStream(serverSocket, filterContext);

            byte[] bytesToSend = Encoding.UTF8.GetBytes("<html><head></head><body></body></html>");

            // Act I - Write some bytes
            Task writeTask = filterStream.WriteAsync(bytesToSend, 0, 20);

            // Assert
            TaskAssert.NotCompleted(writeTask, "Write task should not complete until a successful response comes from the server.");

            // Act Ia - Response from the server
            serverSocket.SendString("HTTP/1.1 200 OK\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.Completed(writeTask, "The write task should complete after we receive the response line from the server.");
            Assert.Contains("<html><head></head><", serverSocket.SentContent);
            AssertWithMessage.Equal(0, filterContext.GetResponseBody(Encoding.UTF8).Length, "No content should be sent to the response yet.");
            Assert.False(serverSocket.IsClosed, "Server connection should not have been closed.");

            // Act II - Write some more bytes
            filterStream.Write(bytesToSend, 20, bytesToSend.Length - 20);

            // Assert
            Assert.Contains("body></body></html>", serverSocket.SentContent);
            AssertWithMessage.Equal(0, filterContext.GetResponseBody(Encoding.UTF8).Length, "No content should be sent to the response yet.");
            Assert.False(serverSocket.IsClosed, "Server connection should not have been closed.");

            // Act III - Wait for complete
            Task completeTask = filterStream.FlushAsync();

            // Assert
            TaskAssert.NotCompleted(completeTask, "Before response from server.");

            // Act IV - Send response from server
            serverSocket.SendString("Content-Length: 17\r\n\r\nXFiltered content", Encoding.ASCII);

            // Assert
            Assert.Equal("Filtered content", filterContext.GetResponseBody(Encoding.ASCII));
            TaskAssert.Completed(completeTask, "After response from server.");
            Assert.False(filterStream.ScriptInjectionTimedOut);
        }
        public void RevolvingBuffers_WaitForBufferEmptyAsync_ReturnsImmediatelyIfAllDataHasBeenRead()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            buffers.CopyDataToBuffer(data, 0, 3);
            buffers.GetBufferedData(); // Read the buffer
            buffers.GetBufferedData(); // Release the buffer and get an empty buffer

            // Act
            Task task = buffers.WaitForBufferEmptyAsync();

            // Assert
            TaskAssert.Completed(task, "Task should be completed immediately.");
        }
        public void RevolvingBuffers_WaitForBufferEmptyAsync_ReturnsAfterBufferIsReleasedAsync()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            buffers.CopyDataToBuffer(data, 0, 2);
            buffers.GetBufferedData(); // Read the buffer
            Task task = buffers.WaitForBufferEmptyAsync();

            // Act
            buffers.GetBufferedDataAsync(); // Release the buffer

            // Assert
            TaskAssert.Completed(task, "Task should be completed when buffer is released.");
        }
        public void RevolvingBuffers_GetBufferedDataAsync_ReturnsEmptyBufferWhenDisposed()
        {
            // Arrange
            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            // Act
            Task <ArraySegment <char> > task = buffers.GetBufferedDataAsync();

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed until data is buffered.");

            // Act
            buffers.Dispose();

            // Assert
            TaskAssert.Completed(task, "Task should be completed after buffers are disposed.");
            Assert.Equal("", GetString(task.Result));
        }
        public void DelayConnectingHttpSocketAdapter_WaitForResponseComplete_ConnectsAndWaits()
        {
            // Arrange
            IHttpSocketAdapter delayAdapter = new DelayConnectingHttpSocketAdapter(ConnectOnlyOnce);

            // Act
            Task result = delayAdapter.WaitForResponseComplete();

            // Assert
            Assert.True(_createdAdapter != null, "Adapter was not created.");
            TaskAssert.NotCompleted(result, "Status code should not be returned yet.");

            // Act
            _createdAdapter.SendResponseComplete();

            // Assert
            TaskAssert.Completed(result);
        }
        public void RevolvingBuffers_GetBufferedDataAsync_WaitsForBufferedData()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

            RevolvingBuffers <char> buffers = new RevolvingBuffers <char>(20);

            // Act
            Task <ArraySegment <char> > task = buffers.GetBufferedDataAsync();

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed until data is buffered.");

            // Act
            buffers.CopyDataToBuffer(data, 0, data.Length);

            // Assert
            TaskAssert.Completed(task, "Task should be completed after data is buffered.");
            Assert.Equal("Hello, world!", GetString(task.Result));
        }
        public static void ResultEquals<ResultType>(Task<ResultType> task, ResultType expected, string messageFormat = null, params object[] args)
        {
            TaskAssert.NotFaulted(task, messageFormat, args);
            TaskAssert.Completed(task, messageFormat, args);

            if (task.Result == null)
            {
                if (expected == null)
                {
                    return;
                }
            }
            else if (task.Result.Equals(expected))
            {
                return;
            }

            ThrowFailure(
                GetMethodFailureMessage(nameof(ResultEquals)),
                FormatMessage("Expected: <{0}>. Actual: <{1}>.", SafeParam(expected), SafeParam(task.Result)),
                FormatMessage(messageFormat, args));
        }
        public void ScriptInjectionFilterStream_ErrorResponseCodeFromServer()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();
            MockScriptInjectionFilterContext filterContext = new MockScriptInjectionFilterContext();

            ScriptInjectionFilterStream filterStream = CreateFilterStream(serverSocket, filterContext);

            byte[] bytesToSend = Encoding.UTF8.GetBytes("<html><head></head><body></body></html>");

            // Act - Attempt to write some bytes
            Task writeTask = filterStream.WriteAsync(bytesToSend, 0, 20);

            // Assert
            TaskAssert.NotCompleted(writeTask, "Task should not complete until the response line is read.");

            // Act - Write an error response
            serverSocket.SendString("HTTP/1.1 404 Not Found\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.Completed(writeTask, "Task should complete after the response line is received.");
            Assert.Equal("<html><head></head><", filterContext.GetResponseBody(Encoding.UTF8));
            Assert.True(serverSocket.IsClosed, "Server connection should be closed.");
        }