Skip to content

Commit

Permalink
Add GetAsync tests and fix JsInteropCookieService split
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mobpilot3 authored and YuriyDurov committed Dec 4, 2024
1 parent c7e95a1 commit 8eb6d70
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task<IEnumerable<Cookie>> 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]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
29 changes: 29 additions & 0 deletions tests/BitzArt.Blazor.Cookies.Tests/GetAsyncTests.cs
Original file line number Diff line number Diff line change
@@ -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<IJSRuntime>();
jsRuntime.Setup(x => x.InvokeAsync<string>("eval", It.IsAny<object[]>()))
.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<IJSRuntime>();
jsRuntime.Setup(x => x.InvokeAsync<string>("eval", It.IsAny<object[]>()))
.ReturnsAsync("token=value=123; path=/");
var result = await new JsInteropCookieService(jsRuntime.Object).GetAsync("token");
Assert.Equal("value=123", result?.Value);
}
}
}
11 changes: 0 additions & 11 deletions tests/BitzArt.Blazor.Cookies.Tests/UnitTest1.cs

This file was deleted.

0 comments on commit 8eb6d70

Please sign in to comment.