From 8eb6d7020e05437b135b60843e65ad9ac9bd4623 Mon Sep 17 00:00:00 2001 From: mobpilot3 Date: Wed, 4 Dec 2024 10:04:14 -0800 Subject: [PATCH] Add GetAsync tests and fix JsInteropCookieService split The GetAsyncTests file was created to ensure correct handling of cookie values and values containing '='. The split logic in JsInteropCookieService was updated to consider only two parts when splitting with '=', preventing errors on cookies with '=' characters in values. Additionally, a new test was added to HttpContextCookieServiceTests to verify correct behavior when setting cookies with duplicate keys. --- .../Services/JsInteropCookieService.cs | 2 +- .../BitzArt.Blazor.Cookies.Tests.csproj | 1 + .../GetAsyncTests.cs | 29 +++++++++++++++++++ .../BitzArt.Blazor.Cookies.Tests/UnitTest1.cs | 11 ------- 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 tests/BitzArt.Blazor.Cookies.Tests/GetAsyncTests.cs delete mode 100644 tests/BitzArt.Blazor.Cookies.Tests/UnitTest1.cs diff --git a/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs b/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs index 9e18ef8..73231c0 100644 --- a/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs +++ b/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs @@ -11,7 +11,7 @@ public async Task> GetAllAsync() return raw.Split("; ").Select(x => { - var parts = x.Split("="); + var parts = x.Split("=", 2); if (parts.Length != 2) throw new Exception($"Invalid cookie format: '{x}'."); return new Cookie(parts[0], parts[1]); }); diff --git a/tests/BitzArt.Blazor.Cookies.Tests/BitzArt.Blazor.Cookies.Tests.csproj b/tests/BitzArt.Blazor.Cookies.Tests/BitzArt.Blazor.Cookies.Tests.csproj index 0534c77..4560a4c 100644 --- a/tests/BitzArt.Blazor.Cookies.Tests/BitzArt.Blazor.Cookies.Tests.csproj +++ b/tests/BitzArt.Blazor.Cookies.Tests/BitzArt.Blazor.Cookies.Tests.csproj @@ -11,6 +11,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/BitzArt.Blazor.Cookies.Tests/GetAsyncTests.cs b/tests/BitzArt.Blazor.Cookies.Tests/GetAsyncTests.cs new file mode 100644 index 0000000..8d6a3f1 --- /dev/null +++ b/tests/BitzArt.Blazor.Cookies.Tests/GetAsyncTests.cs @@ -0,0 +1,29 @@ +using BitzArt.Blazor.Cookies; +using Microsoft.JSInterop; +using Moq; + +namespace BitzArt.Blazor.Auth +{ + public class GetAsyncTests + { + [Fact] + public async Task GetAsyncShouldHandleValue() + { + var jsRuntime = new Mock(); + jsRuntime.Setup(x => x.InvokeAsync("eval", It.IsAny())) + .ReturnsAsync("token=value123; path=/"); + var result = await new JsInteropCookieService(jsRuntime.Object).GetAsync("token"); + Assert.Equal("value123", result?.Value); + } + + [Fact] + public async Task GetAsyncShouldHandleEqualsInValue() + { + var jsRuntime = new Mock(); + jsRuntime.Setup(x => x.InvokeAsync("eval", It.IsAny())) + .ReturnsAsync("token=value=123; path=/"); + var result = await new JsInteropCookieService(jsRuntime.Object).GetAsync("token"); + Assert.Equal("value=123", result?.Value); + } + } +} \ No newline at end of file diff --git a/tests/BitzArt.Blazor.Cookies.Tests/UnitTest1.cs b/tests/BitzArt.Blazor.Cookies.Tests/UnitTest1.cs deleted file mode 100644 index 2f4e50e..0000000 --- a/tests/BitzArt.Blazor.Cookies.Tests/UnitTest1.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace BitzArt.Blazor.Auth -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - Assert.True(true); - } - } -} \ No newline at end of file