From 4275b6f3aa05e98348f5234659208c33b9ad2a88 Mon Sep 17 00:00:00 2001 From: Yuriy Durov Date: Wed, 4 Dec 2024 23:43:29 +0400 Subject: [PATCH] Refactor JsInteropCookieService --- .../Services/JsInteropCookieService.cs | 13 ++--- .../BitzArt.Blazor.Cookies.Tests.csproj | 2 +- .../JsInteropCookieServiceTests.cs | 47 +++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 tests/BitzArt.Blazor.Cookies.Tests/JsInteropCookieServiceTests.cs diff --git a/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs b/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs index 73231c0..08c7e3a 100644 --- a/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs +++ b/src/BitzArt.Blazor.Cookies/Services/JsInteropCookieService.cs @@ -9,12 +9,13 @@ public async Task> GetAllAsync() var raw = await js.InvokeAsync("eval", "document.cookie"); if (string.IsNullOrWhiteSpace(raw)) return []; - return raw.Split("; ").Select(x => - { - var parts = x.Split("=", 2); - if (parts.Length != 2) throw new Exception($"Invalid cookie format: '{x}'."); - return new Cookie(parts[0], parts[1]); - }); + return raw.Split("; ").Select(GetCookie); + } + + private Cookie GetCookie(string raw) + { + var parts = raw.Split("=", 2); + return new Cookie(parts[0], parts[1]); } public async Task GetAsync(string key) 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 4560a4c..4992a0a 100644 --- a/tests/BitzArt.Blazor.Cookies.Tests/BitzArt.Blazor.Cookies.Tests.csproj +++ b/tests/BitzArt.Blazor.Cookies.Tests/BitzArt.Blazor.Cookies.Tests.csproj @@ -24,7 +24,7 @@ - + diff --git a/tests/BitzArt.Blazor.Cookies.Tests/JsInteropCookieServiceTests.cs b/tests/BitzArt.Blazor.Cookies.Tests/JsInteropCookieServiceTests.cs new file mode 100644 index 0000000..ef2eaf1 --- /dev/null +++ b/tests/BitzArt.Blazor.Cookies.Tests/JsInteropCookieServiceTests.cs @@ -0,0 +1,47 @@ +using Microsoft.JSInterop; +using Moq; + +namespace BitzArt.Blazor.Cookies.Tests; + +public class JsInteropCookieServiceTests +{ + [Fact] + public async Task GetAsync_WithCookieNotPresent_ShouldReturnNull() + { + // Arrange + var jsRuntime = new Mock(); + jsRuntime.Setup(x => x.InvokeAsync("eval", It.IsAny())) + .ReturnsAsync(""); + + var sut = new JsInteropCookieService(jsRuntime.Object); + + // Act + var result = await sut.GetAsync("my-cookie"); + + // Assert + Assert.Null(result); + } + + [Theory] + [InlineData("my-cookie", "")] + [InlineData("my-cookie", "my-value")] + [InlineData("my-cookie", "value=123")] + [InlineData("my-cookie", "value_a=a value_b=b")] + [InlineData("my-cookie", "!@#$%^&*()-_=_-)(*&^%$#@!")] + [InlineData("my-cookie", "abc !@#$%^&*()-_ = _-)(*&^%$#@! def 123456789 ghi")] + public async Task GetAsync_WithCookiePresent_ShouldReturnValue(string cookieName, string cookieValue) + { + // Arrange + var jsRuntime = new Mock(); + jsRuntime.Setup(x => x.InvokeAsync("eval", It.IsAny())) + .ReturnsAsync($"{cookieName}={cookieValue}; path=/"); + + var sut = new JsInteropCookieService(jsRuntime.Object); + + // Act + var result = await sut.GetAsync(cookieName); + + // Assert + Assert.Equal(cookieValue, result?.Value); + } +} \ No newline at end of file