From 7e47449f8588dc4a1151aa1ba7698ee8218a9aa3 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 29 Dec 2015 16:49:06 -0800 Subject: [PATCH] Use default encoders all the time Fixes #234 --- .../Views/BaseView.cs | 10 +--- src/Microsoft.AspNet.Diagnostics/project.json | 7 +-- .../RuntimeInfoMiddlewareTest.cs | 57 ------------------- 3 files changed, 4 insertions(+), 70 deletions(-) diff --git a/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs b/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs index 7b37ec718d..f8c122ea03 100644 --- a/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs +++ b/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs @@ -12,7 +12,6 @@ using System.Text; using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNet.Http; -using Microsoft.Extensions.WebEncoders; namespace Microsoft.AspNet.Diagnostics.Views { @@ -44,17 +43,17 @@ namespace Microsoft.AspNet.Diagnostics.Views /// /// Html encoder used to encode content. /// - protected HtmlEncoder HtmlEncoder { get; set; } + protected HtmlEncoder HtmlEncoder { get; set; } = HtmlEncoder.Default; /// /// Url encoder used to encode content. /// - protected UrlEncoder UrlEncoder { get; set; } + protected UrlEncoder UrlEncoder { get; set; } = UrlEncoder.Default; /// /// JavaScript encoder used to encode content. /// - protected JavaScriptEncoder JavaScriptEncoder { get; set; } + protected JavaScriptEncoder JavaScriptEncoder { get; set; } = JavaScriptEncoder.Default; /// /// Execute an individual request @@ -66,9 +65,6 @@ namespace Microsoft.AspNet.Diagnostics.Views Request = Context.Request; Response = Context.Response; Output = new StreamWriter(Response.Body, Encoding.UTF8, 4096, leaveOpen: true); - HtmlEncoder = context.RequestServices.GetHtmlEncoder(); - UrlEncoder = context.RequestServices.GetUrlEncoder(); - JavaScriptEncoder = context.RequestServices.GetJavaScriptEncoder(); await ExecuteAsync(); Output.Dispose(); } diff --git a/src/Microsoft.AspNet.Diagnostics/project.json b/src/Microsoft.AspNet.Diagnostics/project.json index 9f7c86d5a6..82e3cd2621 100644 --- a/src/Microsoft.AspNet.Diagnostics/project.json +++ b/src/Microsoft.AspNet.Diagnostics/project.json @@ -17,15 +17,10 @@ "Microsoft.Extensions.Logging.Abstractions": "1.0.0-*", "Microsoft.Extensions.Options": "1.0.0-*", "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*", - "Microsoft.Extensions.WebEncoders": "1.0.0-*", "System.Diagnostics.DiagnosticSource": "4.0.0-*" }, "frameworks": { - "net451": { - "frameworkAssemblies": { - "System.Runtime": "" - } - }, + "net451": { }, "dotnet5.4": { "dependencies": { "System.Reflection.Extensions": "4.0.1-*" diff --git a/test/Microsoft.AspNet.Diagnostics.Tests/RuntimeInfoMiddlewareTest.cs b/test/Microsoft.AspNet.Diagnostics.Tests/RuntimeInfoMiddlewareTest.cs index 0e4d5412bf..a1ea6b3bfc 100644 --- a/test/Microsoft.AspNet.Diagnostics.Tests/RuntimeInfoMiddlewareTest.cs +++ b/test/Microsoft.AspNet.Diagnostics.Tests/RuntimeInfoMiddlewareTest.cs @@ -5,12 +5,9 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; -using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNet.Http; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.PlatformAbstractions; -using Microsoft.Extensions.WebEncoders.Testing; using Moq; using Xunit; @@ -159,59 +156,5 @@ namespace Microsoft.AspNet.Diagnostics.Tests Assert.Contains("Path2", response); } } - - [Fact] - public async void Invoke_WithMatchingPath_ReturnsInfoPage_UsingCustomHtmlEncoder() - { - // Arrange - var libraryManagerMock = new Mock(MockBehavior.Strict); - libraryManagerMock.Setup(l => l.GetLibraries()).Returns(new[] { - new Library("LibInfo1", "1.0.0-beta1", "Path1", string.Empty, Enumerable.Empty(), Enumerable.Empty()), - }); - - var runtimeEnvironmentMock = new Mock(MockBehavior.Strict); - runtimeEnvironmentMock.Setup(r => r.OperatingSystem).Returns("Windows"); - runtimeEnvironmentMock.Setup(r => r.RuntimeArchitecture).Returns("x64"); - runtimeEnvironmentMock.Setup(r => r.RuntimeType).Returns("clr"); - runtimeEnvironmentMock.Setup(r => r.RuntimeVersion).Returns("1.0.0"); - - RequestDelegate next = _ => - { - return Task.FromResult(null); - }; - - var middleware = new RuntimeInfoMiddleware( - next, - new RuntimeInfoPageOptions(), - libraryManagerMock.Object, - runtimeEnvironmentMock.Object); - - var buffer = new byte[4096]; - using (var responseStream = new MemoryStream(buffer)) - { - var contextMock = new Mock(MockBehavior.Strict); - contextMock - .SetupGet(c => c.Request.Path) - .Returns(new PathString("/runtimeinfo")); - contextMock - .SetupGet(c => c.Response.Body) - .Returns(responseStream); - contextMock - .SetupGet(c => c.RequestServices) - .Returns(new ServiceCollection() - .AddSingleton(new HtmlTestEncoder()) - .BuildServiceProvider()); - - // Act - await middleware.Invoke(contextMock.Object); - - // Assert - string response = Encoding.UTF8.GetString(buffer); - - Assert.True(response.Contains("HtmlEncode[[LibInfo1]]")); - Assert.True(response.Contains("HtmlEncode[[1.0.0-beta1]]")); - Assert.True(response.Contains("HtmlEncode[[Path1]]")); - } - } } }