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()`
This commit is contained in:
parent
40e479c915
commit
c0d27ee56d
|
|
@ -14,7 +14,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label class="order" for="ShippingDateTime">ShippingDateTime</label>
|
<label class="order" for="ShippingDateTime">ShippingDateTime</label>
|
||||||
<input type="datetime-local" id="ShippingDateTime" name="ShippingDateTime" value="1/1/0001 12:00:00 AM" />
|
<input type="datetime-local" id="ShippingDateTime" name="ShippingDateTime" value="01/01/0001 00:00:00" />
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label class="order" for="Products">Products</label>
|
<label class="order" for="Products">Products</label>
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="control-label col-md-2" for="JoinDate">JoinDate</label>
|
<label class="control-label col-md-2" for="JoinDate">JoinDate</label>
|
||||||
<div class="col-md-10">
|
<div class="col-md-10">
|
||||||
<input class="form-control input-validation-error" type="date" data-val="true" data-val-required="The JoinDate field is required." id="JoinDate" name="JoinDate" value="1/1/0001 12:00:00 AM +00:00" />
|
<input class="form-control input-validation-error" type="date" data-val="true" data-val-required="The JoinDate field is required." id="JoinDate" name="JoinDate" value="01/01/0001 00:00:00 +00:00" />
|
||||||
<span class="field-validation-error" data-valmsg-for="JoinDate" data-valmsg-replace="true">The JoinDate field is required.</span>
|
<span class="field-validation-error" data-valmsg-for="JoinDate" data-valmsg-replace="true">The JoinDate field is required.</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@
|
||||||
JoinDate
|
JoinDate
|
||||||
</dt>
|
</dt>
|
||||||
<dd>
|
<dd>
|
||||||
12/1/2014
|
01/12/2014
|
||||||
</dd>
|
</dd>
|
||||||
<dt>
|
<dt>
|
||||||
Salary
|
Salary
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
Vin: 8chars
|
Vin: 8chars
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Inspected Dates: 11/1/2008 12:00:00 AM -07:00
|
Inspected Dates: 01/11/2008 00:00:00 -07:00
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,9 @@ namespace Microsoft.AspNet.Builder
|
||||||
{
|
{
|
||||||
public static Configuration GetTestConfiguration(this IApplicationBuilder app)
|
public static Configuration GetTestConfiguration(this IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
|
// Unconditionally place CultureReplacerMiddleware as early as possible in the pipeline.
|
||||||
|
app.UseMiddleware<CultureReplacerMiddleware>();
|
||||||
|
|
||||||
var configurationProvider = app.ApplicationServices.GetService<ITestConfigurationProvider>();
|
var configurationProvider = app.ApplicationServices.GetService<ITestConfigurationProvider>();
|
||||||
var configuration = configurationProvider == null
|
var configuration = configurationProvider == null
|
||||||
? new Configuration()
|
? new Configuration()
|
||||||
|
|
@ -22,7 +25,7 @@ namespace Microsoft.AspNet.Builder
|
||||||
|
|
||||||
public static IApplicationBuilder UseErrorReporter(this IApplicationBuilder app)
|
public static IApplicationBuilder UseErrorReporter(this IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
return app.Use(next => new ErrorReporterMiddleware(next).Invoke);
|
return app.UseMiddleware<ErrorReporterMiddleware>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
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<CultureInfo>.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_replaceCulture.Before(methodUnderTest: null);
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_replaceCulture.After(methodUnderTest: null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Microsoft.AspNet.Http": "1.0.0-*",
|
"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.ConfigurationModel": "1.0.0-*",
|
||||||
"Microsoft.Framework.DependencyInjection": "1.0.0-*"
|
"Microsoft.Framework.DependencyInjection": "1.0.0-*"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue