public void ScriptInjectionFilterStream_ExceptionOnSubsequentWrite()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();

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

            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
            filterStream.Write(bytesToSend, 0, 20);

            // Assert
            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 - Attempt to write more bytes
            serverSocket.ThrowExceptionOnNextSendAsync();


            Task result = filterStream.WriteAsync(bytesToSend, 20, bytesToSend.Length - 20);

            // Assert
            TaskAssert.Faulted(result, "Exception should have been re-thrown");
            Assert.Equal("SendAsync after ThrowExceptionOnNextSendAsync was called.", result.Exception.InnerException.Message);
        }
Ejemplo 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");
        }
Ejemplo n.º 3
0
        public void TaskHelpers_WaitWithTimeout_HandlesTaskFault()
        {
            // Arrange
            TaskCompletionSource <string> tcs = new TaskCompletionSource <string>();
            TimeSpan timeout = TimeSpan.FromMilliseconds(1000);

            Task <string> result = TaskHelpers.WaitWithTimeout(tcs.Task, timeout, "Timed out");

            // Act
            tcs.SetException(new Exception("MY TEST ERROR"));

            // Assert
            TaskAssert.Faulted(result);
        }
Ejemplo 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.");
        }