From 7c40759e32fba8e9484b42e77c1801ae83a3eae4 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 16 Dec 2015 11:49:20 -0800 Subject: [PATCH] Reacting to Routing changes --- .../Routing/AttributeRoute.cs | 11 +++++++- .../Routing/AttributeRouting.cs | 9 ++++++- .../Routing/AttributeRouteTest.cs | 2 ++ .../Routing/AttributeRoutingTest.cs | 23 +++++++--------- .../Routing/UrlHelperTest.cs | 17 +++++++----- .../AntiforgeryTestHelper.cs | 20 +++----------- .../InlineConstraintSampleTest.cs | 2 +- ...Generation_Home.EditWarehouse.Encoded.html | 2 +- ...ite.HtmlGeneration_Home.Index.Encoded.html | 22 ++++++++-------- ...ite.HtmlGeneration_Home.Order.Encoded.html | 2 +- ...on_Home.OrderUsingHtmlHelpers.Encoded.html | 4 +-- ...e.HtmlGeneration_Home.Product.Encoded.html | 2 +- .../RemoteAttributeTest.cs | 26 +++++++++++++------ 13 files changed, 79 insertions(+), 63 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs index 45eda4a834..6f423e0991 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRoute.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Abstractions; using Microsoft.AspNet.Mvc.Core; @@ -20,6 +21,7 @@ namespace Microsoft.AspNet.Mvc.Routing private readonly IRouter _target; private readonly IActionDescriptorsCollectionProvider _actionDescriptorsCollectionProvider; private readonly IInlineConstraintResolver _constraintResolver; + private readonly UrlEncoder _urlEncoder; private readonly ILoggerFactory _loggerFactory; private TreeRouter _router; @@ -28,6 +30,7 @@ namespace Microsoft.AspNet.Mvc.Routing IRouter target, IActionDescriptorsCollectionProvider actionDescriptorsCollectionProvider, IInlineConstraintResolver constraintResolver, + UrlEncoder urlEncoder, ILoggerFactory loggerFactory) { if (target == null) @@ -45,6 +48,11 @@ namespace Microsoft.AspNet.Mvc.Routing throw new ArgumentNullException(nameof(constraintResolver)); } + if (urlEncoder == null) + { + throw new ArgumentNullException(nameof(urlEncoder)); + } + if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); @@ -53,6 +61,7 @@ namespace Microsoft.AspNet.Mvc.Routing _target = target; _actionDescriptorsCollectionProvider = actionDescriptorsCollectionProvider; _constraintResolver = constraintResolver; + _urlEncoder = urlEncoder; _loggerFactory = loggerFactory; } @@ -95,7 +104,7 @@ namespace Microsoft.AspNet.Mvc.Routing { routeBuilder.Add(new TreeRouteLinkGenerationEntry() { - Binder = new TemplateBinder(routeInfo.ParsedTemplate, routeInfo.Defaults), + Binder = new TemplateBinder(routeInfo.ParsedTemplate, _urlEncoder, routeInfo.Defaults), Defaults = routeInfo.Defaults, Constraints = routeInfo.Constraints, Order = routeInfo.Order, diff --git a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs index 47100119e7..ab7683daf8 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Text.Encodings.Web; using Microsoft.AspNet.Mvc.Infrastructure; using Microsoft.AspNet.Routing; using Microsoft.Extensions.DependencyInjection; @@ -31,9 +32,15 @@ namespace Microsoft.AspNet.Mvc.Routing var actionDescriptorProvider = services.GetRequiredService(); var inlineConstraintResolver = services.GetRequiredService(); + var urlEncoder = services.GetRequiredService(); var loggerFactory = services.GetRequiredService(); - return new AttributeRoute(target, actionDescriptorProvider, inlineConstraintResolver, loggerFactory); + return new AttributeRoute( + target, + actionDescriptorProvider, + inlineConstraintResolver, + urlEncoder, + loggerFactory); } } } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTest.cs index a3f6f0d968..b5b88eba78 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTest.cs @@ -12,6 +12,7 @@ using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing.Tree; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Testing; +using Microsoft.Extensions.WebEncoders.Testing; using Moq; using Xunit; @@ -70,6 +71,7 @@ namespace Microsoft.AspNet.Mvc.Routing handler.Object, actionDescriptorsProvider.Object, Mock.Of(), + new UrlTestEncoder(), NullLoggerFactory.Instance); var requestServices = new Mock(MockBehavior.Strict); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs index 1216795d80..0ce1c6bb73 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRoutingTest.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Text.Encodings.Web; #if DNXCORE50 using System.Reflection; #endif @@ -13,9 +14,11 @@ using Microsoft.AspNet.Mvc.Controllers; using Microsoft.AspNet.Mvc.Infrastructure; using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing.Tree; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.OptionsModel; +using Microsoft.Extensions.WebEncoders.Testing; using Moq; using Xunit; @@ -184,25 +187,17 @@ namespace Microsoft.AspNet.Mvc.Routing .Setup(a => a.ActionDescriptors) .Returns(collection); - var services = new Mock(); - services - .Setup(s => s.GetService(typeof(IActionDescriptorsCollectionProvider))) - .Returns(actionDescriptorProvider.Object); - var routeOptions = new Mock>(); routeOptions .SetupGet(o => o.Value) .Returns(new RouteOptions()); - services - .Setup(s => s.GetService(typeof(IInlineConstraintResolver))) - .Returns(new DefaultInlineConstraintResolver(routeOptions.Object)); - - services - .Setup(s => s.GetService(typeof(ILoggerFactory))) - .Returns(NullLoggerFactory.Instance); - - return services.Object; + return new ServiceCollection() + .AddSingleton(actionDescriptorProvider.Object) + .AddSingleton(new DefaultInlineConstraintResolver(routeOptions.Object)) + .AddSingleton(NullLoggerFactory.Instance) + .AddSingleton(new UrlTestEncoder()) + .BuildServiceProvider(); } private class DisplayNameActionDescriptor : ActionDescriptor diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/UrlHelperTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/UrlHelperTest.cs index 8483d88d17..687e8f0ee3 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/UrlHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/UrlHelperTest.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; @@ -14,6 +15,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.OptionsModel; +using Microsoft.Extensions.WebEncoders.Testing; using Moq; using Xunit; @@ -951,6 +953,7 @@ namespace Microsoft.AspNet.Mvc.Routing var services = new ServiceCollection(); services.AddLogging(); services.AddRouting(); + services.AddSingleton(UrlEncoder.Default); return services.BuildServiceProvider(); } @@ -980,13 +983,15 @@ namespace Microsoft.AspNet.Mvc.Routing .Returns(context => null); routeBuilder.DefaultHandler = target.Object; - routeBuilder.MapRoute(string.Empty, - "{controller}/{action}/{id}", - new RouteValueDictionary(new { id = "defaultid" })); + routeBuilder.MapRoute( + string.Empty, + "{controller}/{action}/{id}", + new RouteValueDictionary(new { id = "defaultid" })); - routeBuilder.MapRoute("namedroute", - "named/{controller}/{action}/{id}", - new RouteValueDictionary(new { id = "defaultid" })); + routeBuilder.MapRoute( + "namedroute", + "named/{controller}/{action}/{id}", + new RouteValueDictionary(new { id = "defaultid" })); var mockHttpRoute = new Mock(); mockHttpRoute diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs index 4b8de63b96..b2935d7366 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTestHelper.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; @@ -14,18 +13,6 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests { public static string RetrieveAntiforgeryToken(string htmlContent, string actionUrl) { - return RetrieveAntiforgeryTokens( - htmlContent, - attribute => attribute.Value.EndsWith(actionUrl, StringComparison.OrdinalIgnoreCase) || - attribute.Value.EndsWith($"HtmlEncode[[{ actionUrl }]]", StringComparison.OrdinalIgnoreCase)) - .FirstOrDefault(); - } - - public static IEnumerable RetrieveAntiforgeryTokens( - string htmlContent, - Func predicate = null) - { - predicate = predicate ?? (_ => true); htmlContent = "" + htmlContent + ""; var reader = new StringReader(htmlContent); var htmlDocument = XDocument.Load(reader); @@ -34,8 +21,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests { foreach (var attribute in form.Attributes()) { - if (string.Equals(attribute.Name.LocalName, "action", StringComparison.OrdinalIgnoreCase) - && predicate(attribute)) + if (string.Equals(attribute.Name.LocalName, "action", StringComparison.OrdinalIgnoreCase)) { foreach (var input in form.Descendants("input")) { @@ -45,12 +31,14 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests (input.Attribute("name").Value == "__RequestVerificationToken" || input.Attribute("name").Value == "HtmlEncode[[__RequestVerificationToken]]")) { - yield return input.Attributes("value").First().Value; + return input.Attributes("value").First().Value; } } } } } + + throw new Exception($"Antiforgery token could not be located in {htmlContent}."); } public static CookieMetadata RetrieveAntiforgeryCookie(HttpResponseMessage response) diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintSampleTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintSampleTest.cs index d9db7ae22a..54bab8d6e5 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintSampleTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintSampleTest.cs @@ -491,7 +491,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests "GetProductByManufacturingDate", "dateTime", "2014-10-11T13:45:30", - "/products/GetProductByManufacturingDate/2014-10-11T13%3a45%3a30" + "/products/GetProductByManufacturingDate/2014-10-11T13%3A45%3A30" }; // Conventional Route, id:guid? constraint diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.EditWarehouse.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.EditWarehouse.Encoded.html index 737e085e83..d5494a238e 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.EditWarehouse.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.EditWarehouse.Encoded.html @@ -1,6 +1,6 @@ -
+
HtmlEncode[[City_1]] diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Index.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Index.Encoded.html index 07ca814913..d292ddbab7 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Index.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Index.Encoded.html @@ -1,10 +1,10 @@ @@ -39,32 +39,32 @@
Non-existent Area diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html index 2d882b76e3..cef19f6c7f 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Order.Encoded.html @@ -4,7 +4,7 @@ - +
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html index 93eda8f2fc..10cc5f20d8 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.OrderUsingHtmlHelpers.Encoded.html @@ -1,10 +1,10 @@ - + - +
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Product.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Product.Encoded.html index 3774684a26..8e76207964 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Product.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Product.Encoded.html @@ -4,7 +4,7 @@ - +
diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs index 2e317e18ea..99471a14f6 100644 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs @@ -2,10 +2,10 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Text.Encodings.Web; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Mvc.Abstractions; -using Microsoft.AspNet.Mvc.Infrastructure; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding.Validation; using Microsoft.AspNet.Mvc.Routing; @@ -14,6 +14,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.OptionsModel; +using Microsoft.Extensions.WebEncoders.Testing; using Moq; using Xunit; @@ -314,7 +315,7 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]); - Assert.Equal("/Controller/Action", rule.ValidationParameters["url"]); + Assert.Equal("/UrlEncode[[Controller]]/UrlEncode[[Action]]", rule.ValidationParameters["url"]); } // Test area is current in this case. @@ -332,7 +333,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]); - Assert.Equal("/Test/Controller/Action", rule.ValidationParameters["url"]); + Assert.Equal( + "/UrlEncode[[Test]]/UrlEncode[[Controller]]/UrlEncode[[Action]]", + rule.ValidationParameters["url"]); } // Explicit reference to the (current) root area. @@ -351,7 +354,7 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]); - Assert.Equal("/Controller/Action", rule.ValidationParameters["url"]); + Assert.Equal("/UrlEncode[[Controller]]/UrlEncode[[Action]]", rule.ValidationParameters["url"]); } // Test area is current in this case. @@ -370,7 +373,7 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]); - Assert.Equal("/Controller/Action", rule.ValidationParameters["url"]); + Assert.Equal("/UrlEncode[[Controller]]/UrlEncode[[Action]]", rule.ValidationParameters["url"]); } // Root area is current in this case. @@ -388,7 +391,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]); - Assert.Equal("/Test/Controller/Action", rule.ValidationParameters["url"]); + Assert.Equal( + "/UrlEncode[[Test]]/UrlEncode[[Controller]]/UrlEncode[[Action]]", + rule.ValidationParameters["url"]); } // Explicit reference to the current (Test) area. @@ -406,7 +411,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]); - Assert.Equal("/Test/Controller/Action", rule.ValidationParameters["url"]); + Assert.Equal( + "/UrlEncode[[Test]]/UrlEncode[[Controller]]/UrlEncode[[Action]]", + rule.ValidationParameters["url"]); } // Test area is current in this case. @@ -424,7 +431,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]); - Assert.Equal("/AnotherArea/Controller/Action", rule.ValidationParameters["url"]); + Assert.Equal( + "/UrlEncode[[AnotherArea]]/UrlEncode[[Controller]]/UrlEncode[[Action]]", + rule.ValidationParameters["url"]); } private static ClientModelValidationContext GetValidationContext(IUrlHelper urlHelper) @@ -558,6 +567,7 @@ namespace Microsoft.AspNet.Mvc { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(new NullLoggerFactory()); + serviceCollection.AddSingleton(new UrlTestEncoder()); var routeOptions = new RouteOptions(); var accessor = new Mock>();