From 5790142f81f2a94ad073834b1534acbf9b02ea3c Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Fri, 25 Oct 2019 18:36:02 +0800 Subject: Add NeutralResourcesLanguage. Conform to best practices. --- Timeline.Tests/IntegratedTests/I18nTest.cs | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Timeline.Tests/IntegratedTests/I18nTest.cs (limited to 'Timeline.Tests/IntegratedTests/I18nTest.cs') diff --git a/Timeline.Tests/IntegratedTests/I18nTest.cs b/Timeline.Tests/IntegratedTests/I18nTest.cs new file mode 100644 index 00000000..67bbea5c --- /dev/null +++ b/Timeline.Tests/IntegratedTests/I18nTest.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Timeline.Tests.Helpers; +using Xunit; +using FluentAssertions; + +namespace Timeline.Tests.IntegratedTests +{ + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1054:Uri parameters should not be strings")] + public class I18nTest : IClassFixture>, IDisposable + { + private readonly TestApplication _testApp; + private readonly HttpClient _client; + + public I18nTest(WebApplicationFactory factory) + { + _testApp = new TestApplication(factory); + _client = _testApp.Factory.CreateDefaultClient(); + } + + public void Dispose() + { + _client.Dispose(); + _testApp.Dispose(); + } + + private const string DirectUrl = "testing/i18n/direct"; + private const string LocalizerUrl = "testing/i18n/localizer"; + + [Theory] + [InlineData(DirectUrl)] + [InlineData(LocalizerUrl)] + public async Task DefaultShouldReturnEnglish(string url) + { + (await _client.GetStringAsync(url)).Should().ContainEquivalentOf("English"); + } + + [Theory] + [InlineData(DirectUrl, "en", true)] + [InlineData(LocalizerUrl, "en", true)] + [InlineData(DirectUrl, "en-US", true)] + [InlineData(LocalizerUrl, "en-US", true)] + [InlineData(DirectUrl, "zh", false)] + [InlineData(LocalizerUrl, "zh", false)] + public async Task ShouldWork(string url, string acceptLanguage, bool english) + { + var request = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri(url, UriKind.RelativeOrAbsolute) + }; + request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(acceptLanguage)); + var body = await (await _client.SendAsync(request)).Content.ReadAsStringAsync(); + body.Should().ContainEquivalentOf(english ? "English" : "中文"); + request.Dispose(); + } + } +} -- cgit v1.2.3