From c62a4ae8ae1c3bb0db3d194968cd406f23e975ec Mon Sep 17 00:00:00 2001 From: hishamco Date: Wed, 16 Jun 2021 11:02:12 +0300 Subject: [PATCH] Fix parsing localization cookie when the value is not present --- Oqtane.Client/Program.cs | 2 +- .../LocalizationCookieTests.cs | 30 +++++++++++++++++++ Oqtane.Test/Oqtane.Test.csproj | 3 +- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 Oqtane.Test/Oqtane.Client.Tests/LocalizationCookieTests.cs diff --git a/Oqtane.Client/Program.cs b/Oqtane.Client/Program.cs index ff27f5c0..f27f0c2e 100644 --- a/Oqtane.Client/Program.cs +++ b/Oqtane.Client/Program.cs @@ -137,7 +137,7 @@ namespace Oqtane.Client var jsRuntime = serviceProvider.GetRequiredService(); var interop = new Interop(jsRuntime); var localizationCookie = await interop.GetCookie(CookieRequestCultureProvider.DefaultCookieName); - var culture = CookieRequestCultureProvider.ParseCookieValue(localizationCookie).UICultures[0].Value; + var culture = CookieRequestCultureProvider.ParseCookieValue(localizationCookie)?.UICultures?[0].Value; var localizationService = serviceProvider.GetRequiredService(); var cultures = await localizationService.GetCulturesAsync(); diff --git a/Oqtane.Test/Oqtane.Client.Tests/LocalizationCookieTests.cs b/Oqtane.Test/Oqtane.Client.Tests/LocalizationCookieTests.cs new file mode 100644 index 00000000..4acc1bb9 --- /dev/null +++ b/Oqtane.Test/Oqtane.Client.Tests/LocalizationCookieTests.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Localization; +using Xunit; + +namespace Oqtane.Oqtane.Client.Tests +{ + public class LocalizationCookieTests + { + [Theory] + [InlineData("c=ar|uic=ar", "ar")] + [InlineData("c=ar", null)] + [InlineData("", null)] + [InlineData(null, null)] + public void ParseCookie(string localizationCookie, string expectedCulture) + { + // Arrange + var localizationCookieValue = CookieRequestCultureProvider.ParseCookieValue(localizationCookie); + + // Act + var culture = localizationCookieValue?.UICultures?[0].Value; + + // Assert + Assert.Equal(expectedCulture, culture); + } + } +} diff --git a/Oqtane.Test/Oqtane.Test.csproj b/Oqtane.Test/Oqtane.Test.csproj index 2705c3c5..b8b7748f 100644 --- a/Oqtane.Test/Oqtane.Test.csproj +++ b/Oqtane.Test/Oqtane.Test.csproj @@ -18,7 +18,8 @@ false - + +