Ejemplo 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");
        }
Ejemplo n.º 2
0
        public void SocketReader_ReadLine_WaitsForMoreDataWhenLineIncomplete()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <string> result = reader.ReadLine(_dontCancel);

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

            // Act
            socket.SendString("This is the first part of the string. ", Encoding.ASCII);

            // Assert
            TaskAssert.NotCompleted(result, "After sending first sentence.");

            // Act
            socket.SendString("This is the second part of the string.\r", Encoding.ASCII);

            // Assert
            TaskAssert.NotCompleted(result, "After sending second sentence.");

            // Act
            socket.SendString("\nThis is an extra, third sentence.\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.ResultEquals(result, "This is the first part of the string. This is the second part of the string.", "After sending data.");
        }
Ejemplo n.º 3
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);
        }
        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");
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 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");
        }
Ejemplo n.º 7
0
        public void TaskHelpers_WaitWithCancellation_DoesNotCompleteUntilTaskCompletes()
        {
            // Arrange
            TaskCompletionSource <string> tcs = new TaskCompletionSource <string>();
            CancellationTokenSource       cts = new CancellationTokenSource();

            // Act
            Task <string> result = TaskHelpers.WaitWithCancellation(tcs.Task, cts.Token, null);

            // Assert
            TaskAssert.NotCompleted(result);
        }
        public void SendResponseHeader(string name, string value)
        {
            TaskCompletionSource <string> tcs;

            if (!_responseHeaderRequests.TryGetValue(name, out tcs))
            {
                tcs = new TaskCompletionSource <string>();
                _responseHeaderRequests[name] = tcs;
            }

            TaskAssert.NotCompleted(tcs.Task, "MockHttpSocketAdapter: Response header '{0}' can only be set once.", name);

            tcs.SetResult(value);
        }
        public void RevolvingBuffers_WaitForBufferEmptyAsync_WaitsAfterDataHasBeenWritten()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

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

            buffers.CopyDataToBuffer(data, 0, 2);

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

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed until data is read.");
        }
        public void RevolvingBuffers_WaitForBufferEmptyAsync_WaitsWhileBufferIsRead()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

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

            buffers.CopyDataToBuffer(data, 0, 2);
            Task task = buffers.WaitForBufferEmptyAsync();

            // Act
            buffers.GetBufferedData(); // Read the buffer

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed until buffer is released.");
        }
Ejemplo n.º 11
0
        public void SocketReader_ReadChar_KeepsWaitingIfNotEnoughDataIsReturned()
        {
            // Arrange
            byte[] sendBuffer = Encoding.UTF32.GetBytes("ΨΣ"); // Greek Psi, Sigma

            MockSocketAdapter socket = new MockSocketAdapter();

            SocketReader reader = new SocketReader(socket);

            reader.SetEncoding(Encoding.UTF32);

            // Act
            Task <char> result = reader.ReadChar(_dontCancel); // Try to read first character

            // Assert
            TaskAssert.NotCompleted(result, "Before sending any bytes");

            // Act
            socket.SendBytes(sendBuffer, 0, 1); // Send 1 byte

            // Assert
            TaskAssert.NotCompleted(result, "After sending 1 byte");

            // Act
            socket.SendBytes(sendBuffer, 1, 2); // Send 2 more bytes

            // Assert
            TaskAssert.NotCompleted(result, "After sending 2 more bytes");

            // Act
            socket.SendBytes(sendBuffer, 3, 2); // Send 2 more bytes

            // Assert
            TaskAssert.ResultEquals(result, 'Ψ', "After sending all bytes of first character.");

            // Act
            result = reader.ReadChar(_dontCancel); // Try to read second character

            // Assert
            TaskAssert.NotCompleted(result, "Before sending remaining bytes");

            // Act
            socket.SendBytes(sendBuffer, 5, 3); // Send remaining 3 bytes

            // Assert
            TaskAssert.ResultEquals(result, 'Σ', "After sending remaining bytes");
        }
        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_WaitsWhileThereIsStillMoreDataToReadAsync()
        {
            // Arrange
            char[] data = "Hello, world!".ToArray();

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

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

            // Act
            buffers.GetBufferedDataAsync(); // Release the buffer, read the next buffer

            // Assert
            TaskAssert.NotCompleted(task, "Task should not be completed while there is still more data.");
        }
        public void DelayConnectingHttpSocketAdapter_GetResponseStatusCode_ConnectsAndRequestsStatusCode()
        {
            // Arrange
            IHttpSocketAdapter delayAdapter = new DelayConnectingHttpSocketAdapter(ConnectOnlyOnce);

            // Act
            Task <int> result = delayAdapter.GetResponseStatusCode();

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

            // Act
            _createdAdapter.SendResponseStatusCode(304);

            // Assert
            TaskAssert.ResultEquals(result, 304);
        }
        public void DelayConnectingHttpSocketAdapter_GetResponseHeader_ConnectsAndGetsHeader()
        {
            // Arrange
            IHttpSocketAdapter delayAdapter = new DelayConnectingHttpSocketAdapter(ConnectOnlyOnce);

            // Act
            Task <string> result = delayAdapter.GetResponseHeader("Content-Length");

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

            // Act
            _createdAdapter.SendResponseHeader("Content-Length", "1234");

            // Assert
            TaskAssert.ResultEquals(result, "1234");
        }
        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);
        }
Ejemplo n.º 18
0
        public void HttpSocketAdapter_GetResponseStatusCode_ReturnsStatusCode()
        {
            // Arrange
            MockSocketAdapter serverSocket = new MockSocketAdapter();

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

            // Act
            Task <int> result = clientSocket.GetResponseStatusCode();

            // Assert
            TaskAssert.NotCompleted(result, "Before status line was sent.");

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

            // Assert
            TaskAssert.ResultEquals(result, 200, "After status line was sent.");
        }
Ejemplo n.º 19
0
        public void SocketReader_ReadLine_WaitsForDataReadingOneLine()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            SocketReader reader = new SocketReader(socket);

            // Act
            Task <string> result = reader.ReadLine(_dontCancel);

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

            // Act
            socket.SendString("This is a test.\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.ResultEquals(result, "This is a test.", "After sending data.");
        }
        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));
        }
Ejemplo n.º 21
0
        public void SocketReader_ReadChar_WaitsForDataReadingOneCharacter()
        {
            // Arrange
            MockSocketAdapter socket = new MockSocketAdapter();

            SocketReader reader = new SocketReader(socket);

            reader.SetEncoding(Encoding.Unicode);

            // Act
            Task <char> result = reader.ReadChar(_dontCancel);

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

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

            // Assert
            TaskAssert.ResultEquals(result, 'a', "After sending data");
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
        public void HttpSocketAdapter_GetResponseHeader_ReturnsHeaderWhenAvailable()
        {
            // 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);

            // Act
            Task <string> result = clientSocket.GetResponseHeader("content-encoding");

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

            // Act
            serverSocket.SendString("Content-Encoding: UTF-16\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.NotCompleted(result, "Header values should not be returned until all headers are read.");

            // Act - Blank newline means end of headers
            serverSocket.SendString("Content-Length: 45\r\n\r\n", Encoding.ASCII);

            // Assert
            TaskAssert.ResultEquals(result, "UTF-16");

            // Act
            Task <string> result2 = clientSocket.GetResponseHeader("content-length");

            // Assert
            TaskAssert.ResultEquals(result2, "45");

            // Act
            Task <string> result3 = clientSocket.GetResponseHeader("user-agent");

            // Assert
            TaskAssert.ResultEquals(result3, null);
        }
        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.");
        }
        public void SendResponseStatusCode(int statusCode)
        {
            TaskAssert.NotCompleted(_statusCodeTask.Task, "MockHttpSocketAdapter: Response Status Code can only be set once.");

            _statusCodeTask.SetResult(statusCode);
        }
        public void SendResponseComplete()
        {
            TaskAssert.NotCompleted(_responseCompleteTask.Task, "MockHttpSocketAdapter: Response Complete can only be sent once.");

            _responseCompleteTask.SetResult(true);
        }