Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/Auth0.AspNetCore.Authentication.Api/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;

Expand All @@ -10,21 +9,12 @@ internal abstract class Utils
{
/// <summary>
/// Creates a Base64-encoded JSON string containing the SDK agent name and version.
/// The version is retrieved from the assembly of <see cref="AuthenticationBuilderExtensions" />.
/// </summary>
/// <returns>A Base64-encoded JSON string with agent name and version.</returns>
public static string CreateAgentString()
{
Version? sdkVersion = typeof(AuthenticationBuilderExtensions).GetTypeInfo().Assembly.GetName().Version;
var agentJson =
$"{{\"name\":\"aspnetcore-api\",\"version\":\"{BuildVersionString(sdkVersion)}\"}}";
$"{{\"name\":\"aspnetcore-api\",\"version\":\"{Version.Current}\"}}";
return Convert.ToBase64String(Encoding.UTF8.GetBytes(agentJson));
}

internal static string BuildVersionString(Version? sdkVersion)
{
return sdkVersion != null
? $"{sdkVersion.Major}.{sdkVersion.Minor}.{sdkVersion.Build}"
: "0.0.0";
}
}
6 changes: 6 additions & 0 deletions src/Auth0.AspNetCore.Authentication.Api/Version.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Auth0.AspNetCore.Authentication.Api;

internal static class Version
{
public const string Current = "1.0.0-beta.6";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void CreateAgentString_ReturnsBase64EncodedJson_With_Correct_Name_And_Ver
var agentString = Utils.CreateAgentString();
var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(agentString));
decoded.Should().Contain("\"name\":\"aspnetcore-api\"");
decoded.Should().MatchRegex("\"version\":\"\\d+\\.\\d+\\.\\d+\"");
decoded.Should().Contain($"\"version\":\"{Version.Current}\"");
}

[Fact]
Expand All @@ -20,29 +20,4 @@ public void CreateAgentString_Returns_Valid_Base64_String()
Action act = () => Convert.FromBase64String(agentString);
act.Should().NotThrow();
}

[Fact]
public void CreateAgentString_Returns_Json_With_Default_Version_If_Assembly_Version_Is_Null()
{
// Simulate null version by using a test double if possible, otherwise check for "0.0.0" fallback
// Since static method and type, we can only check for the fallback value in the output
var agentString = Utils.CreateAgentString();
var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(agentString));
decoded.Should().MatchRegex("\"version\":\"\\d+\\.\\d+\\.\\d+\"");
}

[Fact]
public void BuildVersionString_ReturnsCorrectString_When_Version_Is_Not_Null()
{
var version = new Version(1, 2, 3);
var result = Utils.BuildVersionString(version);
result.Should().Be("1.2.3");
}

[Fact]
public void BuildVersionString_Returns_Zero_Version_When_Version_Is_Null()
{
var result = Utils.BuildVersionString(null);
result.Should().Be("0.0.0");
}
}
Loading