< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Runtime.VariablesMap
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Runtime/VariablesMap.cs
Tag: Kestrun/Kestrun@5f1d2b981c9d7292c11fd448428c6ab6c811c5de
Line coverage
92%
Covered lines: 25
Uncovered lines: 2
Coverable lines: 27
Total lines: 87
Line coverage: 92.5%
Branch coverage
58%
Covered branches: 14
Total branches: 24
Branch coverage: 58.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 11/19/2025 - 17:40:50 Line coverage: 92.5% (25/27) Branch coverage: 58.3% (14/24) Total lines: 87 Tag: Kestrun/Kestrun@fcf33342333cef0516fe0d0912a86709874fd026 11/19/2025 - 17:40:50 Line coverage: 92.5% (25/27) Branch coverage: 58.3% (14/24) Total lines: 87 Tag: Kestrun/Kestrun@fcf33342333cef0516fe0d0912a86709874fd026

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetVariablesMap(...)66.67%66100%
GetHostSharedState(...)50%4475%
GetSharedStateStore(...)50%4475%
GetCommonProperties(...)60%1010100%

File(s)

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

#LineLine coverage
 1using System.Reflection;
 2using Kestrun.Hosting;
 3using Kestrun.Models;
 4using Kestrun.SharedState;
 5
 6namespace Kestrun.Runtime;
 7
 8/// <summary>
 9/// Helper class to map common request and server properties, as well as shared state variables, into a dictionary.
 10/// </summary>
 11public static class VariablesMap
 12{
 13    /// <summary>
 14    /// Populates the provided dictionary with variables from the request context and shared state store.
 15    /// </summary>
 16    /// <param name="ctx">The Kestrun context containing request information.</param>
 17    /// <param name="vars">The dictionary to populate with variables.</param>
 18    /// <returns>True if variables were successfully mapped; otherwise, false.</returns>
 19    public static bool GetVariablesMap(KestrunContext ctx, ref Dictionary<string, object?> vars)
 20    {
 21        // ① Initialize the dictionary
 122        vars ??= new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
 123        return GetCommonProperties(ctx, ref vars) // ② Add common request properties
 124            && GetHostSharedState(ctx.Host, ref vars) // ③ Add host shared state variables
 125            && GetSharedStateStore(ref vars); // ③ Add shared state variables
 26    }
 27
 28    /// <summary>
 29    /// Populates the provided dictionary with variables from the shared state store.
 30    /// </summary>
 31    /// <param name="host">The Kestrun host instance.</param>
 32    /// <param name="vars">The dictionary to populate with shared state variables.</param>
 33    /// <returns>True if variables were successfully mapped; otherwise, false.</returns>
 34    private static bool GetHostSharedState(KestrunHost host, ref Dictionary<string, object?> vars)
 35    {
 136        vars ??= new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
 237        foreach (var kv in host.SharedState.Snapshot())
 38        {
 039            vars[kv.Key] = kv.Value; // 1) top-level JSON
 40        }
 41
 142        return true;
 43    }
 44
 45    /// <summary>
 46    /// Populates the provided dictionary with variables from the global shared state store.
 47    /// </summary>
 48    /// <param name="vars">The dictionary to populate with global shared state variables.</param>
 49    /// <returns>True if variables were successfully mapped; otherwise, false.</returns>
 50    private static bool GetSharedStateStore(ref Dictionary<string, object?> vars)
 51    {
 152        vars ??= new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
 253        foreach (var kv in GlobalStore.Snapshot())
 54        {
 055            vars[kv.Key] = kv.Value; // 1) top-level JSON
 56        }
 57
 158        return true;
 59    }
 60    /// <summary>
 61    /// Populates the provided dictionary with common request and server properties from the Kestrun context.
 62    /// </summary>
 63    /// <param name="ctx">The Kestrun context containing request information.</param>
 64    /// <param name="vars">The dictionary to populate with common properties.</param>
 65    /// <returns>True if properties were successfully mapped; otherwise, false.</returns>
 66    public static bool GetCommonProperties(KestrunContext ctx, ref Dictionary<string, object?> vars)
 67    {
 68        // ① Initialize the dictionary
 269        vars["Context"] = ctx;
 270        vars["Request"] = ctx.Request;
 271        vars["QueryString"] = ctx.Request.Query.ToDictionary(q => q.Key, q => q.Value.ToString());
 272        vars["Form"] = ctx.Request.Form;
 273        vars["Cookies"] = ctx.Request.Cookies;
 674        vars["Headers"] = ctx.Request.Headers.ToDictionary(h => h.Key, h => h.Value.ToString());
 275        vars["UserAgent"] = ctx.Request.Headers["User-Agent"].ToString();
 276        vars["ServerVersion"] = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
 277        vars["ServerOS"] = Environment.OSVersion.ToString();
 278        vars["ServerArch"] = Environment.Is64BitOperatingSystem ? "x64" : "x86";
 279        vars["ServerIP"] = ctx.HttpContext.Connection.LocalIpAddress?.ToString() ?? "unknown";
 280        vars["ServerPort"] = ctx.HttpContext.Connection.LocalPort;
 281        vars["ServerName"] = Environment.MachineName;
 282        vars["Timestamp"] = DateTimeOffset.UtcNow.ToString("O");
 83
 284        return true;
 85    }
 86}
 87