< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 50
Coverable lines: 50
Total lines: 231
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 - 01:25:22 Line coverage: 58.3% (28/48) Total lines: 214 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/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@7c4ce528870211ad6c2d2398c31ec13097fc5840

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 AuthorizationSchema
 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 ExtraImports
 31        An optional array of additional namespaces to import for the route.
 32    .PARAMETER ExtraRefs
 33        An optional array of additional assemblies to reference for the route.
 34    .PARAMETER AllowDuplicate
 35        If specified, allows the addition of duplicate routes with the same path and HTTP verb.
 36    .PARAMETER Arguments
 37        An optional hashtable of arguments to pass to the script block or code.
 38    .PARAMETER Endpoints
 39        An optional array of endpoint names to which the route should be bound.
 40        If not specified, the route will be bound to all endpoints.
 41    .PARAMETER DuplicateAction
 42        Specifies the action to take if a duplicate route is detected. Options are 'Throw', 'Skip', 'Allow', or 'Warn'.
 43        Default is 'Throw', which will raise an error if a duplicate route is found.
 44    .PARAMETER PassThru
 45        If specified, the function will return the created route object.
 46    .OUTPUTS
 47        Returns the Kestrun server instance with the new route added.
 48    .EXAMPLE
 49        Add-KrMapRoute -Server $myServer -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" }
 50        Adds a new map route to the specified Kestrun server with the given path and script block.
 51    .EXAMPLE
 52
 53        Add-KrMapRoute -Server $myServer -Path "/myroute" -Code "Write-Host 'Hello, World!'" -Language PowerShell
 54        Adds a new map route to the specified Kestrun server with the given path and code.
 55    .EXAMPLE
 56        Get-KrServer | Add-KrMapRoute -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" } -PassThru
 57        Adds a new map route to the current Kestrun server and returns the route object.
 58    .NOTES
 59        This function is part of the Kestrun PowerShell module and is used to manage routes
 60#>
 61function Add-KrMapRoute {
 62    [KestrunRuntimeApi('Definition')]
 63    [CmdletBinding(defaultParameterSetName = 'ScriptBlock', PositionalBinding = $true)]
 64    [OutputType([Kestrun.Hosting.KestrunHost])]
 65    param(
 66        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 67        [Kestrun.Hosting.KestrunHost]$Server,
 68
 69        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 70        [Kestrun.Hosting.Options.MapRouteOptions]$Options,
 71
 72        [Parameter(ParameterSetName = 'ScriptBlock')]
 73        [Parameter(ParameterSetName = 'Code')]
 74        [Parameter(ParameterSetName = 'CodeFilePath')]
 75        [Alias('Method')]
 076        [Kestrun.Utilities.HttpVerb[]]$Verbs = @([Kestrun.Utilities.HttpVerb]::Get),
 77
 78        [Parameter(ParameterSetName = 'ScriptBlock')]
 79        [Parameter(ParameterSetName = 'Code')]
 80        [Parameter(ParameterSetName = 'CodeFilePath')]
 81        [ValidatePattern('^/')]
 82        [Alias('Path')]
 83        [string]$Pattern = '/',
 84
 85        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'ScriptBlock')]
 86        [scriptblock]$ScriptBlock,
 87
 88        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 89        [Alias('CodeBlock')]
 90        [string]$Code,
 91
 92        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 93        [Kestrun.Scripting.ScriptLanguage]$Language,
 94
 95        [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')]
 96        [string]$CodeFilePath,
 97
 98        [Parameter(ParameterSetName = 'ScriptBlock')]
 99        [Parameter(ParameterSetName = 'Code')]
 100        [Parameter(ParameterSetName = 'CodeFilePath')]
 101        [string[]]$AuthorizationSchema = $null,
 102
 103        [Parameter(ParameterSetName = 'ScriptBlock')]
 104        [Parameter(ParameterSetName = 'Code')]
 105        [Parameter(ParameterSetName = 'CodeFilePath')]
 106        [string[]]$AuthorizationPolicy = $null,
 107
 108        [Parameter(ParameterSetName = 'Code')]
 109        [Parameter(ParameterSetName = 'CodeFilePath')]
 110        [string[]]$ExtraImports = $null,
 111
 112        [Parameter(ParameterSetName = 'Code')]
 113        [Parameter(ParameterSetName = 'CodeFilePath')]
 114        [System.Reflection.Assembly[]]$ExtraRefs = $null,
 115
 116        [Parameter(ParameterSetName = 'ScriptBlock')]
 117        [Parameter(ParameterSetName = 'Code')]
 118        [Parameter(ParameterSetName = 'CodeFilePath')]
 119        [hashtable]$Arguments,
 120
 121        [Parameter(ParameterSetName = 'ScriptBlock')]
 122        [Parameter(ParameterSetName = 'Code')]
 123        [Parameter(ParameterSetName = 'CodeFilePath')]
 124        [string[]]$Endpoints,
 125
 126        [Parameter()]
 127        [switch]$AllowDuplicate,
 128
 129        [Parameter()]
 130        [ValidateSet('Throw', 'Skip', 'Allow', 'Warn')]
 131        [string]$DuplicateAction = 'Throw',
 132
 133        [Parameter()]
 134        [switch]$PassThru
 135    )
 136    begin {
 137        # Ensure the server instance is resolved
 0138        $Server = Resolve-KestrunServer -Server $Server
 139    }
 140    process {
 141
 0142        $exists = Test-KrRoute -Path $Pattern -Verb $Verbs
 143
 0144        if ($exists) {
 0145            if ($AllowDuplicate -or $DuplicateAction -eq 'Allow') {
 0146                Write-KrLog -Level Warning -Message "Route '{Path}' ({Verbs}) already exists; adding another." -Values $
 0147            } elseif ($DuplicateAction -eq 'Skip') {
 0148                Write-KrLog -Level Verbose -Message "Route '{Path}' ({Verbs}) exists; skipping." -Values $Pattern, ($Ver
 149                return
 0150            } elseif ($DuplicateAction -eq 'Warn') {
 0151                Write-KrLog -Level Warning -Message "Route '{Path}' ({Verbs}) already exists." -Values $Pattern, ($Verbs
 152            } else {
 0153                throw [System.InvalidOperationException]::new(
 0154                    "Route '$Pattern' with method(s) $($Verbs -join ',') already exists.")
 155            }
 156        }
 157
 158        # -- if Options parameter is used, we can skip the rest of the parameters
 0159        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 0160            $Options = [Kestrun.Hosting.Options.MapRouteOptions]::new()
 0161            $Options.HttpVerbs = $Verbs
 0162            $Options.Pattern = $Pattern
 163
 0164            if ($null -ne $AuthorizationSchema) {
 0165                $Options.RequireSchemes = $AuthorizationSchema
 166            }
 0167            if ($null -ne $AuthorizationPolicy) {
 0168                $Options.RequirePolicies = $AuthorizationPolicy
 169            }
 170
 171            # Endpoints
 0172            if ($null -ne $Endpoints -and $Endpoints.Count -gt 0) {
 0173                $Options.Endpoints = $Endpoints
 174            }
 175            # ScriptCode configuration
 0176            $Options.ScriptCode.ExtraImports = $ExtraImports
 0177            $Options.ScriptCode.ExtraRefs = $ExtraRefs
 0178            if ($null -ne $Arguments) {
 0179                $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0180                foreach ($key in $Arguments.Keys) {
 0181                    $dict[$key] = $Arguments[$key]
 182                }
 0183                $Options.ScriptCode.Arguments = $dict
 184            }
 0185            switch ($PSCmdlet.ParameterSetName) {
 186                'ScriptBlock' {
 0187                    $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 0188                    $Options.ScriptCode.Code = $ScriptBlock.ToString()
 189                }
 190                'Code' {
 0191                    $Options.ScriptCode.Language = $Language
 0192                    $Options.ScriptCode.Code = $Code
 193                }
 194                'CodeFilePath' {
 0195                    if (-not (Test-Path -Path $CodeFilePath)) {
 0196                        throw "The specified code file path does not exist: $CodeFilePath"
 197                    }
 0198                    $extension = Split-Path -Path $CodeFilePath -Extension
 0199                    switch ($extension) {
 200                        '.ps1' {
 0201                            $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 202                        }
 203                        '.cs' {
 0204                            $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp
 205                        }
 206                        '.vb' {
 0207                            $Options.ScriptCode.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic
 208                        }
 209                        default {
 0210                            throw "Unsupported '$extension' code file extension."
 211                        }
 212                    }
 0213                    $Options.ScriptCode.Code = Get-Content -Path $CodeFilePath -Raw
 214                }
 215            }
 216        } else {
 0217            Write-Verbose 'Using provided MapRouteOptions instance.'
 218        }
 0219        if ($Server.RouteGroupStack.Count -gt 0) {
 0220            $grp = $Server.RouteGroupStack.Peek()
 0221            $Options = _KrMerge-MRO -Parent $grp -Child $Options
 222        }
 0223        [Kestrun.Hosting.KestrunHostMapExtensions]::AddMapRoute($Server, $Options) | Out-Null
 224
 0225        if ($PassThru.IsPresent) {
 226            # if the PassThru switch is specified, return the modified server instance
 0227            return $Server
 228        }
 229    }
 230}
 231

Methods/Properties

Add-KrMapRoute()