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_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); }