// 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.Collections.Generic; using System.Net.Http; using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNet.Testing; using Microsoft.Net.Http.Headers; using Xunit; namespace Microsoft.AspNet.Mvc.FunctionalTests { public class LocalizationTest : IClassFixture> { private const string SiteName = nameof(LocalizationWebSite); private static readonly Assembly _assembly = typeof(LocalizationTest).GetTypeInfo().Assembly; public LocalizationTest(MvcTestFixture fixture) { Client = fixture.Client; } public HttpClient Client { get; } public static IEnumerable LocalizationData { get { var expected1 = @"en-gb-index partial mypartial "; yield return new[] { "en-GB", expected1 }; var expected2 = @"fr-index fr-partial mypartial "; yield return new[] { "fr", expected2 }; if (!TestPlatformHelper.IsMono) { // https://github.com/aspnet/Mvc/issues/2759 var expected3 = @"index partial mypartial "; yield return new[] { "!-invalid-!", expected3 }; } } } [Theory] [MemberData(nameof(LocalizationData))] public async Task Localization_SuffixViewName(string value, string expected) { // Arrange var cultureCookie = "c=" + value + "|uic=" + value; var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"); request.Headers.Add( "Cookie", new CookieHeaderValue("ASPNET_CULTURE", cultureCookie).ToString()); // Act var response = await Client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal(expected, body.Trim(), ignoreLineEndingDifferences: true); } public static IEnumerable LocalizationResourceData { get { var expected1 = "Hello there!!" + Environment.NewLine + "Learn More" + Environment.NewLine + "Hi John ! You are in 2015 year and today is Thursday"; yield return new[] { "en-GB", expected1 }; var expected2 = "Bonjour!" + Environment.NewLine + "apprendre Encore Plus" + Environment.NewLine + "Salut John ! Vous êtes en 2015 an aujourd'hui est Thursday"; yield return new[] { "fr", expected2 }; } } [Theory] [MemberData(nameof(LocalizationResourceData))] public async Task Localization_Resources_ReturnExpectedValues(string value, string expected) { // Arrange var cultureCookie = "c=" + value + "|uic=" + value; var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Home/Locpage"); request.Headers.Add( "Cookie", new CookieHeaderValue("ASPNET_CULTURE", cultureCookie).ToString()); // Act var response = await Client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal(expected, body.Trim()); } [Fact] public async Task Localization_InvalidModel_ValidationAttributes_ReturnsLocalizedErrorMessage() { // Arrange var expected = @"Nom non valide. Longueur minimale de nom est 6 Nom du produit est invalide
Nom non valide. Longueur minimale de nom est 6
Nom du produit est invalide
"; var cultureCookie = "c=fr|uic=fr"; var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Home/GetInvalidUser"); request.Headers.Add( "Cookie", new CookieHeaderValue("ASPNET_CULTURE", cultureCookie).ToString()); // Act var response = await Client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal(expected, body.Trim(), ignoreLineEndingDifferences: true); } } }