SlugHelperTests
This commit is contained in:
parent
ed52e2f789
commit
7a5cb703ec
3 changed files with 29 additions and 2 deletions
26
IdentityShroud.Core.Tests/Helpers/SlugHelperTests.cs
Normal file
26
IdentityShroud.Core.Tests/Helpers/SlugHelperTests.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
using IdentityShroud.Core.Helpers;
|
||||||
|
|
||||||
|
namespace IdentityShroud.Core.Tests.Helpers;
|
||||||
|
|
||||||
|
public class SlugHelperTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData("", 40, "")]
|
||||||
|
[InlineData("test", 40, "test")]
|
||||||
|
[InlineData("Test", 40, "test")]
|
||||||
|
[InlineData("tést", 40, "test")]
|
||||||
|
[InlineData("foo_bar", 40, "foo-bar")]
|
||||||
|
[InlineData("foo bar", 40, "foo-bar")]
|
||||||
|
[InlineData("-foo", 40, "foo")]
|
||||||
|
[InlineData("foo-", 40, "foo")]
|
||||||
|
[InlineData("_foo", 40, "foo")]
|
||||||
|
[InlineData("foo_", 40, "foo")]
|
||||||
|
[InlineData("slug_would_be_too_long", 16, "slug-woul-frYeRw")] // not at word boundary
|
||||||
|
[InlineData("slug_would_be_too_long", 18, "slug-would-frYeRw")] // at word boundary
|
||||||
|
public void Test(string input, int max_length, string expected)
|
||||||
|
{
|
||||||
|
string result = SlugHelper.GenerateSlug(input, max_length);
|
||||||
|
|
||||||
|
Assert.Equal(expected, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using IdentityShroud.Core.DTO;
|
||||||
using IdentityShroud.Core.Messages;
|
using IdentityShroud.Core.Messages;
|
||||||
using Microsoft.AspNetCore.WebUtilities;
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,9 +73,9 @@ public static class SlugHelper
|
||||||
|
|
||||||
private static string GenerateHashSuffix(string text)
|
private static string GenerateHashSuffix(string text)
|
||||||
{
|
{
|
||||||
using (var sha256 = SHA256.Create())
|
using (var md5 = MD5.Create())
|
||||||
{
|
{
|
||||||
byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(text));
|
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
|
||||||
|
|
||||||
// Take first 4 bytes (will become ~5-6 base64url chars)
|
// Take first 4 bytes (will become ~5-6 base64url chars)
|
||||||
string base64Url = WebEncoders.Base64UrlEncode(hash, 0, 4);
|
string base64Url = WebEncoders.Base64UrlEncode(hash, 0, 4);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue