< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Utilities.VariablesMap
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/VariablesMap.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 69
Line coverage: 100%
Branch coverage
66%
Covered branches: 12
Total branches: 18
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetVariablesMap(...)75%44100%
GetSharedStateStore(...)75%44100%
GetCommonProperties(...)60%1010100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/VariablesMap.cs

#LineLine coverage
 1using System.Reflection;
 2using Kestrun.Hosting;
 3using Kestrun.SharedState;
 4
 5namespace Kestrun.Utilities;
 6
 7/// <summary>
 8/// Provides utility methods for mapping and flattening variables from various sources.
 9/// </summary>
 10public static class VariablesMap
 11{
 12    /// <summary>
 13    /// Populates the provided dictionary with variables from the request context and shared state store.
 14    /// </summary>
 15    /// <param name="ctx">The Kestrun context containing request information.</param>
 16    /// <param name="vars">The dictionary to populate with variables.</param>
 17    /// <returns>True if variables were successfully mapped; otherwise, false.</returns>
 18    public static bool GetVariablesMap(KestrunContext ctx, ref Dictionary<string, object?> vars)
 19    {
 20        // ① Initialize the dictionary
 121        vars ??= new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
 122        return GetCommonProperties(ctx, ref vars) // ② Add common request properties
 123        && GetSharedStateStore(ref vars); // ③ Add shared state variables
 24    }
 25
 26    /// <summary>
 27    /// Populates the provided dictionary with variables from the shared state store.
 28    /// </summary>
 29    /// <param name="vars">The dictionary to populate with shared state variables.</param>
 30    /// <returns>True if variables were successfully mapped; otherwise, false.</returns>
 31    public static bool GetSharedStateStore(ref Dictionary<string, object?> vars)
 32    {
 133        vars ??= new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
 634        foreach (var kv in SharedStateStore.Snapshot())
 35        {
 236            vars[kv.Key] = kv.Value; // 1) top-level JSON
 37        }
 38
 139        return true;
 40    }
 41
 42    /// <summary>
 43    /// Populates the provided dictionary with common request and server properties from the Kestrun context.
 44    /// </summary>
 45    /// <param name="ctx">The Kestrun context containing request information.</param>
 46    /// <param name="vars">The dictionary to populate with common properties.</param>
 47    /// <returns>True if properties were successfully mapped; otherwise, false.</returns>
 48    public static bool GetCommonProperties(KestrunContext ctx, ref Dictionary<string, object?> vars)
 49    {
 50        // ① Initialize the dictionary
 251        vars["Context"] = ctx;
 252        vars["Request"] = ctx.Request;
 253        vars["QueryString"] = ctx.Request.Query.ToDictionary(q => q.Key, q => q.Value.ToString());
 254        vars["Form"] = ctx.Request.Form;
 255        vars["Cookies"] = ctx.Request.Cookies;
 656        vars["Headers"] = ctx.Request.Headers.ToDictionary(h => h.Key, h => h.Value.ToString());
 257        vars["UserAgent"] = ctx.Request.Headers["User-Agent"].ToString();
 258        vars["ServerVersion"] = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
 259        vars["ServerOS"] = Environment.OSVersion.ToString();
 260        vars["ServerArch"] = Environment.Is64BitOperatingSystem ? "x64" : "x86";
 261        vars["ServerIP"] = ctx.HttpContext.Connection.LocalIpAddress?.ToString() ?? "unknown";
 262        vars["ServerPort"] = ctx.HttpContext.Connection.LocalPort;
 263        vars["ServerName"] = Environment.MachineName;
 264        vars["Timestamp"] = DateTimeOffset.UtcNow.ToString("O");
 65
 266        return true;
 67    }
 68}
 69