< Summary - Kestrun — Combined Coverage

Information
Class: Public.Request.Get-KrRequestCookie
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Request/Get-KrRequestCookie.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 86
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 14:53:17 Line coverage: 0% (0/2) Total lines: 28 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/04/2025 - 18:11:31 Line coverage: 0% (0/2) Total lines: 29 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0510/15/2025 - 01:01:18 Line coverage: 0% (0/14) Total lines: 86 Tag: Kestrun/Kestrun@7c4ce528870211ad6c2d2398c31ec13097fc5840

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Request/Get-KrRequestCookie.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Retrieves a cookie value from the HTTP request.
 4    .DESCRIPTION
 5        This function accesses the current HTTP request context and retrieves the value
 6        of the specified cookie by name.
 7    .PARAMETER Name
 8        The name of the cookie to retrieve from the HTTP request.
 9    .PARAMETER AsInt
 10        If specified, converts the cookie value to an integer.
 11    .PARAMETER AsBool
 12        If specified, converts the cookie value to a boolean.
 13    .PARAMETER AsDouble
 14        If specified, converts the cookie value to a double.
 15    .PARAMETER AsString
 16        If specified, converts the cookie value to a string.
 17    .PARAMETER ThrowIfMissing
 18        If specified, throws an error if the cookie is not found.
 19        By default, it returns $null if not found.
 20    .EXAMPLE
 21        $value = Get-KrRequestCookie -Name "param1"
 22        Retrieves the value of the cookie "param1" from the HTTP request.
 23    .EXAMPLE
 24        $id = Get-KrRequestCookie -Name "id" -AsInt -ThrowIfMissing
 25        Retrieves the value of the cookie "id" as an integer, throwing an error if it's missing.
 26    .EXAMPLE
 27        $flag = Get-KrRequestCookie -Name "flag" -AsBool
 28        Retrieves the value of the cookie "flag" as a boolean.
 29    .OUTPUTS
 30        Returns the value of the specified cookie, or $null if not found.
 31    .NOTES
 32        This function is designed to be used in the context of a Kestrun server response.
 33#>
 34function Get-KrRequestCookie {
 35    [KestrunRuntimeApi('Route')]
 36    [CmdletBinding(DefaultParameterSetName = 'Default')]
 37    [OutputType([string])]
 38    [OutputType([int])]
 39    [OutputType([bool])]
 40    [OutputType([double])]
 41    [OutputType([object])]
 42    param(
 43        [Parameter(Mandatory = $true)]
 44        [string]$Name,
 45
 46        [Parameter(parameterSetName = 'Int')]
 47        [switch]$AsInt,
 48
 49        [Parameter(parameterSetName = 'Bool')]
 50        [Alias('AsBoolean')]
 51        [switch]$AsBool,
 52
 53        [Parameter(parameterSetName = 'Double')]
 54        [switch]$AsDouble,
 55
 56        [Parameter(parameterSetName = 'String')]
 57        [switch]$AsString,
 58
 59        [Parameter()]
 60        [switch]$ThrowIfMissing
 61    )
 062    if ($null -ne $Context -and $null -ne $Context.Request -and $null -ne $Context.Request.Cookies) {
 063        if ($ThrowIfMissing -and -not $Context.Request.Cookies.ContainsKey($Name)) {
 064            throw [System.ArgumentException]::new("Missing required cookie: $Name", "$Name")
 65        }
 66        # Get the cookie value from the request
 067        $value = $Context.Request.Cookies[$Name]
 68
 069        if ($AsInt) {
 070            return [int]$value
 71        }
 072        if ($AsBool) {
 073            return [bool]$value
 74        }
 075        if ($AsDouble) {
 076            return [double]$value
 77        }
 078        if ($AsString) {
 079            return $value.ToString()
 80        }
 081        return $value
 82    } else {
 83        # Outside of route context
 084        Write-KrOutsideRouteWarning
 85    }
 86}

Methods/Properties

Get-KrRequestCookie()