зеркало из https://github.com/aspnet/Localization.git
Add 'Microsoft.AspNet.Localization' Unit Tests
This commit is contained in:
Родитель
3d2c013a48
Коммит
efcbb2a664
|
@ -1,7 +1,7 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.22823.1
|
||||
VisualStudioVersion = 14.0.23107.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FB313677-BAB3-4E49-8CDB-4FA4A9564767}"
|
||||
EndProject
|
||||
|
@ -28,6 +28,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{B723DB83-A
|
|||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Localization.Tests", "test\Microsoft.Framework.Localization.Tests\Microsoft.Framework.Localization.Tests.xproj", "{287AD58D-DF34-4F16-8616-FD78FA1CADF9}"
|
||||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Localization.Tests", "test\Microsoft.AspNet.Localization.Tests\Microsoft.AspNet.Localization.Tests.xproj", "{19A2A931-5C60-47A0-816A-0DC9C4CE5736}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -62,6 +64,10 @@ Global
|
|||
{287AD58D-DF34-4F16-8616-FD78FA1CADF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{287AD58D-DF34-4F16-8616-FD78FA1CADF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{287AD58D-DF34-4F16-8616-FD78FA1CADF9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{19A2A931-5C60-47A0-816A-0DC9C4CE5736}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{19A2A931-5C60-47A0-816A-0DC9C4CE5736}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{19A2A931-5C60-47A0-816A-0DC9C4CE5736}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{19A2A931-5C60-47A0-816A-0DC9C4CE5736}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -74,5 +80,6 @@ Global
|
|||
{BD22AE1C-6631-4DA6-874D-0DC0F803CEAB} = {FB313677-BAB3-4E49-8CDB-4FA4A9564767}
|
||||
{F3988D3A-A4C8-4FD7-BAFE-13E0D0A1659A} = {FB313677-BAB3-4E49-8CDB-4FA4A9564767}
|
||||
{287AD58D-DF34-4F16-8616-FD78FA1CADF9} = {B723DB83-A670-4BCB-95FB-195361331AD2}
|
||||
{19A2A931-5C60-47A0-816A-0DC9C4CE5736} = {B723DB83-A670-4BCB-95FB-195361331AD2}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
using Microsoft.AspNet.Localization;
|
||||
using Microsoft.AspNet.TestHost;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Framework.Localization.Tests
|
||||
{
|
||||
public class AccessLanguageHeaderRequestCultureProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public async void GetFallbackLanguage()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions
|
||||
{
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA"),
|
||||
new CultureInfo("en-US")
|
||||
}
|
||||
};
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal("ar-SA", requestCulture.Culture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("jp,ar-SA,en-US");
|
||||
var count = client.DefaultRequestHeaders.AcceptLanguage.Count;
|
||||
var response = await client.GetAsync(string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
using Microsoft.AspNet.Localization;
|
||||
using Microsoft.AspNet.TestHost;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Framework.Localization.Tests
|
||||
{
|
||||
public class CookieRequestCultureProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public async void GetCultureInfoFromPersistentCookie()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
var provider = new CookieRequestCultureProvider();
|
||||
provider.CookieName = "Preferences";
|
||||
options.RequestCultureProviders.Insert(0, provider);
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal("ar-SA", requestCulture.Culture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var culture = new CultureInfo("ar-SA");
|
||||
var requestCulture = new RequestCulture(culture);
|
||||
var value = CookieRequestCultureProvider.MakeCookieValue(requestCulture);
|
||||
client.DefaultRequestHeaders.Add("Cookie", new CookieHeaderValue("Preferences", value).ToString());
|
||||
var response = await client.GetAsync(string.Empty);
|
||||
Assert.Equal("c=ar-SA|uic=ar-SA",value);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetDefaultCultureInfoIfCultureKeysAreMissingOrInvalid()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
var provider = new CookieRequestCultureProvider();
|
||||
provider.CookieName = "Preferences";
|
||||
options.RequestCultureProviders.Insert(0, provider);
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal(options.DefaultRequestCulture.Culture.Name, requestCulture.Culture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
client.DefaultRequestHeaders.Add("Cookie", new CookieHeaderValue("Preferences", "uic=ar-SA").ToString());
|
||||
var response = await client.GetAsync(string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetDefaultCultureInfoIfCookieDoesNotExist()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
var provider = new CookieRequestCultureProvider();
|
||||
provider.CookieName = "Preferences";
|
||||
options.RequestCultureProviders.Insert(0, provider);
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal(options.DefaultRequestCulture.Culture.Name, requestCulture.Culture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync(string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
using Microsoft.AspNet.Localization;
|
||||
using Microsoft.AspNet.TestHost;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.Globalization;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Framework.Localization.Tests
|
||||
{
|
||||
public class CustomRequestCultureProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public async void CustomRequestCultureProviderThatGetsCultureInfoFromUrl()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
|
||||
{
|
||||
var culture = GetCultureInfoFromUrl(context);
|
||||
var requestCulture = new RequestCulture(culture);
|
||||
return Task.FromResult(requestCulture);
|
||||
}));
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal("ar", requestCulture.Culture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/ar/page");
|
||||
}
|
||||
}
|
||||
|
||||
private CultureInfo GetCultureInfoFromUrl(HttpContext context)
|
||||
{
|
||||
var currentCulture = "en";
|
||||
var segments = context.Request.Path.Value.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (segments.Length > 1 && segments[0].Length == 2)
|
||||
{
|
||||
if (CultureInfoCache.KnownCultureNames.Contains(segments[0]))
|
||||
currentCulture = segments[0];
|
||||
else
|
||||
throw new InvalidOperationException($"The '{segments[0]}' is invalid culture name.");
|
||||
}
|
||||
return CultureInfoCache.GetCultureInfo(currentCulture);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>19a2a931-5c60-47a0-816a-0dc9c4ce5736</ProjectGuid>
|
||||
<RootNamespace>Microsoft.AspNet.Localization.Tests</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
|
@ -0,0 +1,172 @@
|
|||
// Copyright (c) .NET Foundation. 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.Features;
|
||||
using Microsoft.AspNet.Localization;
|
||||
using Microsoft.AspNet.TestHost;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Framework.Localization.Tests
|
||||
{
|
||||
public class QueryStringRequestCultureProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public async void GetCultureInfoFromQueryString()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal("ar-SA", requestCulture.Culture.Name);
|
||||
Assert.Equal("ar-YE", requestCulture.UICulture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/page?culture=ar-SA&ui-culture=ar-YE");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetDefaultCultureInfoIfCultureKeysAreMissing()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal(options.DefaultRequestCulture.Culture.Name, requestCulture.Culture.Name);
|
||||
Assert.Equal(options.DefaultRequestCulture.UICulture.Name, requestCulture.UICulture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/page");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetDefaultCultureInfoIfCultureIsInvalid()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal(options.DefaultRequestCulture.Culture.Name, requestCulture.Culture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/page?culture=ar-XY&ui-culture=ar-SA");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetDefaultCultureInfoIfUICultureIsInvalid()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal(options.DefaultRequestCulture.UICulture.Name, requestCulture.UICulture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/page?culture=ar-SA&ui-culture=ar-XY");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetSameCultureInfoIfCultureKeyIsMissing()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal("ar-SA", requestCulture.Culture.Name);
|
||||
Assert.Equal("ar-SA", requestCulture.UICulture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/page?ui-culture=ar-SA");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetSameCultureInfoIfUICultureKeyIsMissing()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal("ar-SA", requestCulture.Culture.Name);
|
||||
Assert.Equal("ar-SA", requestCulture.UICulture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/page?culture=ar-SA");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetCultureInfoFromQueryStringWithCustomKeys()
|
||||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
var provider = new QueryStringRequestCultureProvider();
|
||||
provider.QueryStringKey = "c";
|
||||
provider.UIQueryStringKey = "uic";
|
||||
options.RequestCultureProviders.Insert(0, provider);
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
var requestCulture = requestCultureFeature.RequestCulture;
|
||||
Assert.Equal("ar-SA", requestCulture.Culture.Name);
|
||||
Assert.Equal("ar-YE", requestCulture.UICulture.Name);
|
||||
return Task.FromResult(0);
|
||||
});
|
||||
}))
|
||||
{
|
||||
var client = server.CreateClient();
|
||||
var response = await client.GetAsync("/page?c=ar-SA&uic=ar-YE");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Localization": "1.0.0-*",
|
||||
"Microsoft.AspNet.TestHost": "1.0.0-*",
|
||||
"xunit": "2.1.0-*",
|
||||
"xunit.runner.dnx": "2.1.0-*"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": { },
|
||||
"dnxcore50": { }
|
||||
},
|
||||
"commands": {
|
||||
"test": "xunit.runner.dnx"
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче