< Summary - Kestrun — Combined Coverage

Information
Class: Public.Route.Add-KrMapRoute
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrMapRoute.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 56
Coverable lines: 56
Total lines: 255
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: 58.3% (28/48) Total lines: 214 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/04/2025 - 18:11:31 Line coverage: 58.3% (28/48) Total lines: 215 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0509/08/2025 - 20:34:03 Line coverage: 58% (29/50) Total lines: 219 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7209/13/2025 - 21:11:10 Line coverage: 58% (29/50) Total lines: 223 Tag: Kestrun/Kestrun@c00c04d65edffc6840698a5c67a70cae1ad411d910/13/2025 - 16:52:37 Line coverage: 0% (0/50) Total lines: 233 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e10/15/2025 - 01:01:18 Line coverage: 0% (0/50) Total lines: 231 Tag: Kestrun/Kestrun@7c4ce528870211ad6c2d2398c31ec13097fc584011/19/2025 - 02:25:56 Line coverage: 0% (0/54) Total lines: 246 Tag: Kestrun/Kestrun@98ff905e5605a920343154665980a71211a03c6d12/14/2025 - 20:04:52 Line coverage: 0% (0/56) Total lines: 255 Tag: Kestrun/Kestrun@a05ac8de57c6207e227b92ba360e9d58869ac80a

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrMapRoute.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds a new map route to the Kestrun server.
 4    .DESCRIPTION
 5        This function allows you to add a new map route to the Kestrun server by specifying the route path and the scrip
 6    .PARAMETER Server
 7        The Kestrun server instance to which the route will be added.
 8        If not specified, the function will attempt to resolve the current server context.
 9    .PARAMETER Options
 10        An instance of `Kestrun.Hosting.Options.MapRouteOptions` that contains the configuration for the route.
 11        This parameter is used to specify various options for the route, such as HTTP verbs, path, authorization schemes
 12    .PARAMETER Verbs
 13        Alias: Method
 14        The HTTP verbs (GET, POST, etc.) that the route should respond to. Defaults to GET.
 15    .PARAMETER Pattern
 16        Alias: Path
 17        The URL path for the new route.
 18    .PARAMETER ScriptBlock
 19        The script block to be executed when the route is accessed.
 20    .PARAMETER Code
 21        The code to be executed when the route is accessed, used in conjunction with the Language parameter.
 22    .PARAMETER Language
 23        The scripting language of the code to be executed.
 24    .PARAMETER CodeFilePath
 25        The file path to the code to be executed when the route is accessed.
 26    .PARAMETER AuthorizationScheme
 27        An optional array of authorization schemes that the route requires.
 28    .PARAMETER AuthorizationPolicy
 29        An optional array of authorization policies that the route requires.
 30    .PARAMETER CorsPolicy
 31        An optional CORS policy name that the route requires.
 32    .PARAMETER AllowAnonymous
 33        If specified, allows anonymous access to the route. it's mutually exclusive with AuthorizationScheme and Authori
 34    .PARAMETER ExtraImports
 35        An optional array of additional namespaces to import for the route.
 36    .PARAMETER ExtraRefs
 37        An optional array of additional assemblies to reference for the route.
 38    .PARAMETER AllowDuplicate
 39        If specified, allows the addition of duplicate routes with the same path and HTTP verb.
 40    .PARAMETER Arguments
 41        An optional hashtable of arguments to pass to the script block or code.
 42    .PARAMETER Endpoints
 43        An optional array of endpoint names to which the route should be bound.
 44        If not specified, the route will be bound to all endpoints.
 45    .PARAMETER DuplicateAction
 46        Specifies the action to take if a duplicate route is detected. Options are 'Throw', 'Skip', 'Allow', or 'Warn'.
 47        Default is 'Throw', which will raise an error if a duplicate route is found.
 48    .PARAMETER PassThru
 49        If specified, the function will return the created route object.
 50    .OUTPUTS
 51        Returns the Kestrun server instance with the new route added.
 52    .EXAMPLE
 53        Add-KrMapRoute -Server $myServer -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" }
 54        Adds a new map route to the specified Kestrun server with the given path and script block.
 55    .EXAMPLE
 56
 57        Add-KrMapRoute -Server $myServer -Path "/myroute" -Code "Write-Host 'Hello, World!'" -Language PowerShell
 58        Adds a new map route to the specified Kestrun server with the given path and code.
 59    .EXAMPLE
 60        Get-KrServer | Add-KrMapRoute -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" } -PassThru
 61        Adds a new map route to the current Kestrun server and returns the route object.
 62    .NOTES
 63        This function is part of the Kestrun PowerShell module and is used to manage routes
 64#>
 65function Add-KrMapRoute {
 66    [KestrunRuntimeApi('Definition')]
 67    [CmdletBinding(defaultParameterSetName = 'ScriptBlock', PositionalBinding = $true)]
 68    [OutputType([Kestrun.Hosting.KestrunHost])]
 69    param(
 70        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 71        [Kestrun.Hosting.KestrunHost]$Server,
 72
 73        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 74        [Kestrun.Hosting.Options.MapRouteOptions]$Options,
 75
 76        [Parameter(ParameterSetName = 'ScriptBlock')]
 77        [Parameter(ParameterSetName = 'Code')]
 78        [Parameter(ParameterSetName = 'CodeFilePath')]
 79        [Alias('Method')]
 080        [Kestrun.Utilities.HttpVerb[]]$Verbs = @('Get'),
 81
 82        [Parameter(ParameterSetName = 'ScriptBlock')]
 83        [Parameter(ParameterSetName = 'Code')]
 84        [Parameter(ParameterSetName = 'CodeFilePath')]
 85        [ValidatePattern('^/')]
 86        [Alias('Path')]
 87        [string]$Pattern = '/',
 88
 89        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'ScriptBlock')]
 90        [scriptblock]$ScriptBlock,
 91
 92        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 93        [Alias('CodeBlock')]
 94        [string]$Code,
 95
 96        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 97        [Kestrun.Scripting.ScriptLanguage]$Language,
 98
 99        [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')]
 100        [string]$CodeFilePath,
 101
 102        [Parameter(ParameterSetName = 'ScriptBlock')]
 103        [Parameter(ParameterSetName = 'Code')]
 104        [Parameter(ParameterSetName = 'CodeFilePath')]
 105        [string[]]$AuthorizationScheme = $null,
 106
 107        [Parameter(ParameterSetName = 'ScriptBlock')]
 108        [Parameter(ParameterSetName = 'Code')]
 109        [Parameter(ParameterSetName = 'CodeFilePath')]
 110        [string[]]$AuthorizationPolicy = $null,
 111
 112        [Parameter(ParameterSetName = 'ScriptBlock')]
 113        [Parameter(ParameterSetName = 'Code')]
 114        [Parameter(ParameterSetName = 'CodeFilePath')]
 115        [string]$CorsPolicy,
 116
 117        [Parameter(ParameterSetName = 'ScriptBlock')]
 118        [Parameter(ParameterSetName = 'Code')]
 119        [Parameter(ParameterSetName = 'CodeFilePath')]
 120        [switch]$AllowAnonymous,
 121
 122        [Parameter(ParameterSetName = 'Code')]
 123        [Parameter(ParameterSetName = 'CodeFilePath')]
 124        [string[]]$ExtraImports = $null,
 125
 126        [Parameter(ParameterSetName = 'Code')]
 127        [Parameter(ParameterSetName = 'CodeFilePath')]
 128        [System.Reflection.Assembly[]]$ExtraRefs = $null,
 129
 130        [Parameter(ParameterSetName = 'ScriptBlock')]
 131        [Parameter(ParameterSetName = 'Code')]
 132        [Parameter(ParameterSetName = 'CodeFilePath')]
 133        [hashtable]$Arguments,
 134
 135        [Parameter(ParameterSetName = 'ScriptBlock')]
 136        [Parameter(ParameterSetName = 'Code')]
 137        [Parameter(ParameterSetName = 'CodeFilePath')]
 138        [string[]]$Endpoints,
 139
 140        [Parameter()]
 141        [switch]$AllowDuplicate,
 142
 143        [Parameter()]
 144        [ValidateSet('Throw', 'Skip', 'Allow', 'Warn')]
 145        [string]$DuplicateAction = 'Throw',
 146
 147        [Parameter()]
 148        [switch]$PassThru
 149    )
 150    begin {
 151        # Ensure the server instance is resolved
 0152        $Server = Resolve-KestrunServer -Server $Server
 153    }
 154    process {
 155
 0156        $exists = Test-KrRoute -Path $Pattern -Verb $Verbs
 157
 0158        if ($exists) {
 0159            if ($AllowDuplicate -or $DuplicateAction -eq 'Allow') {
 0160                Write-KrLog -Level Warning -Message "Route '{Path}' ({Verbs}) already exists; adding another." -Values $
 0161            } elseif ($DuplicateAction -eq 'Skip') {
 0162                Write-KrLog -Level Verbose -Message "Route '{Path}' ({Verbs}) exists; skipping." -Values $Pattern, ($Ver
 163                return
 0164            } elseif ($DuplicateAction -eq 'Warn') {
 0165                Write-KrLog -Level Warning -Message "Route '{Path}' ({Verbs}) already exists." -Values $Pattern, ($Verbs
 166            } else {
 0167                throw [System.InvalidOperationException]::new(
 0168                    "Route '$Pattern' with method(s) $($Verbs -join ',') already exists.")
 169            }
 170        }
 171
 172        # -- if Options parameter is used, we can skip the rest of the parameters
 0173        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 0174            $Options = [Kestrun.Hosting.Options.MapRouteOptions]::new()
 0175            $Options.HttpVerbs = $Verbs
 0176            $Options.Pattern = $Pattern
 0177            if ($AllowAnonymous.IsPresent) {
 0178                if ( $null -ne $AuthorizationScheme -or $null -ne $AuthorizationPolicy) {
 0179                    throw [System.ArgumentException]::new(
 180                        'The AllowAnonymous parameter cannot be used together with AuthorizationScheme or AuthorizationP
 181                }
 182                # No authorization required
 0183                $Options.AllowAnonymous = $true
 184            } else {
 0185                if ($null -ne $AuthorizationScheme) {
 0186                    $Options.RequireSchemes.AddRange($AuthorizationScheme) | Out-Null
 187                }
 0188                if ($null -ne $AuthorizationPolicy) {
 0189                    $Options.RequirePolicies.AddRange($AuthorizationPolicy) | Out-Null
 190                }
 191            }
 0192            if (-not [string]::IsNullOrEmpty($CorsPolicy)) {
 0193                $Options.CorsPolicy = $CorsPolicy
 194            }
 195            # Endpoints
 0196            if ($null -ne $Endpoints -and $Endpoints.Count -gt 0) {
 0197                $Options.Endpoints = $Endpoints
 198            }
 199            # ScriptCode configuration
 0200            $Options.ScriptCode.ExtraImports = $ExtraImports
 0201            $Options.ScriptCode.ExtraRefs = $ExtraRefs
 0202            if ($null -ne $Arguments) {
 0203                $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0204                foreach ($key in $Arguments.Keys) {
 0205                    $dict[$key] = $Arguments[$key]
 206                }
 0207                $Options.ScriptCode.Arguments = $dict
 208            }
 0209            switch ($PSCmdlet.ParameterSetName) {
 210                'ScriptBlock' {
 0211                    $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 0212                    $Options.ScriptCode.Code = $ScriptBlock.ToString()
 213                }
 214                'Code' {
 0215                    $Options.ScriptCode.Language = $Language
 0216                    $Options.ScriptCode.Code = $Code
 217                }
 218                'CodeFilePath' {
 0219                    if (-not (Test-Path -Path $CodeFilePath)) {
 0220                        throw "The specified code file path does not exist: $CodeFilePath"
 221                    }
 0222                    $extension = Split-Path -Path $CodeFilePath -Extension
 0223                    switch ($extension) {
 224                        '.ps1' {
 0225                            $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 226                        }
 227                        '.cs' {
 0228                            $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp
 229                        }
 230                        '.vb' {
 0231                            $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic
 232                        }
 233                        default {
 0234                            throw "Unsupported '$extension' code file extension."
 235                        }
 236                    }
 0237                    $Options.ScriptCode.Code = Get-Content -Path $CodeFilePath -Raw
 238                }
 239            }
 240        } else {
 0241            Write-Verbose 'Using provided MapRouteOptions instance.'
 242        }
 0243        if ($Server.RouteGroupStack.Count -gt 0) {
 0244            $grp = $Server.RouteGroupStack.Peek()
 0245            $Options = _KrMerge-MRO -Parent $grp -Child $Options
 246        }
 0247        [Kestrun.Hosting.KestrunHostMapExtensions]::AddMapRoute($Server, $Options) | Out-Null
 248
 0249        if ($PassThru.IsPresent) {
 250            # if the PassThru switch is specified, return the modified server instance
 0251            return $Server
 252        }
 253    }
 254}
 255

Methods/Properties

Add-KrMapRoute()