From c0d27ee56db35c77ff6201febb31058c1ba6bde7 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Tue, 13 Jan 2015 12:46:03 -0800 Subject: [PATCH] Add `CultureReplacerMiddleware` for use in functional test web sites - #1766 - use `ReplaceCultureAttribute` to avoid `CultureReplacer` thread consistency checks - also update test expectations to match new formats nit: use `UseMiddleware()` extension method rather than `app.Use()` --- ...elpersWebSite.MvcTagHelper_Home.Order.html | 2 +- ...elpersWebSite.Employee.Create.Invalid.html | 2 +- ...sWebSite.Employee.Details.AfterCreate.html | 2 +- .../UpdateDealerVehicle_UpdateSuccessful.txt | 2 +- .../BuilderExtensions.cs | 5 ++- .../CultureReplacerMiddleware.cs | 44 +++++++++++++++++++ .../project.json | 2 + 7 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/CultureReplacerMiddleware.cs diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html index 33456a1703..8e38335701 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html @@ -14,7 +14,7 @@
- +
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Create.Invalid.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Create.Invalid.html index 6fb84c0bc6..3ac18fd762 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Create.Invalid.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Create.Invalid.html @@ -55,7 +55,7 @@
- + The JoinDate field is required.
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Details.AfterCreate.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Details.AfterCreate.html index 7fce00fbb3..43ad3ae875 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Details.AfterCreate.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/TagHelpersWebSite.Employee.Details.AfterCreate.html @@ -43,7 +43,7 @@ JoinDate
- 12/1/2014 + 01/12/2014
Salary diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/UpdateDealerVehicle_UpdateSuccessful.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/UpdateDealerVehicle_UpdateSuccessful.txt index afe01b1f2e..c39a47f1f6 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/UpdateDealerVehicle_UpdateSuccessful.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/UpdateDealerVehicle_UpdateSuccessful.txt @@ -4,7 +4,7 @@ Vin: 8chars
  • - Inspected Dates: 11/1/2008 12:00:00 AM -07:00 + Inspected Dates: 01/11/2008 00:00:00 -07:00
  • diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs index 2b253d444c..909b8b9a85 100644 --- a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs @@ -12,6 +12,9 @@ namespace Microsoft.AspNet.Builder { public static Configuration GetTestConfiguration(this IApplicationBuilder app) { + // Unconditionally place CultureReplacerMiddleware as early as possible in the pipeline. + app.UseMiddleware(); + var configurationProvider = app.ApplicationServices.GetService(); var configuration = configurationProvider == null ? new Configuration() @@ -22,7 +25,7 @@ namespace Microsoft.AspNet.Builder public static IApplicationBuilder UseErrorReporter(this IApplicationBuilder app) { - return app.Use(next => new ErrorReporterMiddleware(next).Invoke); + return app.UseMiddleware(); } } } \ No newline at end of file diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/CultureReplacerMiddleware.cs b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/CultureReplacerMiddleware.cs new file mode 100644 index 0000000000..d094030022 --- /dev/null +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/CultureReplacerMiddleware.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Testing; + +namespace Microsoft.AspNet.Mvc.TestConfiguration +{ + /// + /// A middleware that ensures web sites run in a consistent culture. Currently useful for tests that format dates, + /// times, or numbers. Will be more useful when we have localized resources. + /// + public class CultureReplacerMiddleware + { + // Have no current need to use cultures other than the ReplaceCultureAttribute defaults (en-GB, en-US). + private readonly ReplaceCultureAttribute _replaceCulture = new ReplaceCultureAttribute(); + + private readonly RequestDelegate _next; + + public CultureReplacerMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task Invoke(HttpContext context) + { + // Use ReplaceCultureAttribute to avoid thread consistency checks in CultureReplacer. await doesn't + // necessarily end on the original thread. For this case, problems arise when next middleware throws. Can + // remove the thread consistency checks once culture is (at least for .NET 4.6) handled using + // AsyncLocal. + try + { + _replaceCulture.Before(methodUnderTest: null); + await _next(context); + } + finally + { + _replaceCulture.After(methodUnderTest: null); + } + } + } +} \ No newline at end of file diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json index baf60416ff..8e45e11408 100644 --- a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json @@ -1,6 +1,8 @@ { "dependencies": { "Microsoft.AspNet.Http": "1.0.0-*", + "Microsoft.AspNet.Http.Extensions": "1.0.0-*", + "Microsoft.AspNet.Testing": "1.0.0-*", "Microsoft.Framework.ConfigurationModel": "1.0.0-*", "Microsoft.Framework.DependencyInjection": "1.0.0-*" },