< Summary - Kestrun — Combined Coverage

Information
Class: Public.Route.Add-KrRouteGroup
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrRouteGroup.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 163
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: 67.6% (23/34) Total lines: 159 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 18:11:31 Line coverage: 67.6% (23/34) Total lines: 160 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0509/08/2025 - 20:34:03 Line coverage: 66.6% (24/36) Total lines: 165 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7210/13/2025 - 16:52:37 Line coverage: 0% (0/35) Total lines: 163 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

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

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Creates a grouped route context (prefix + shared options) for nested Add-KrMapRoute calls.
 4    .DESCRIPTION
 5        While the ScriptBlock runs, all Add-KrMapRoute calls inherit:
 6          - Prefix (prepended to -Path)
 7          - AuthorizationSchema / AuthorizationPolicy
 8          - ExtraImports / ExtraRefs
 9          - Arguments (merged; child overrides keys)
 10        Supports nesting; inner groups inherit and can override unless -NoInherit is used.
 11    .PARAMETER Server
 12        The Kestrun server instance to which the route will be added.
 13        If not specified, the function will attempt to resolve the current server context.
 14    .PARAMETER Options
 15        The options to apply to all routes in the group.
 16    .PARAMETER Prefix
 17        The path prefix for the group (e.g. '/todoitems').
 18    .PARAMETER AuthorizationSchema
 19        Authorization schemes required by all routes in the group.
 20    .PARAMETER AuthorizationPolicy
 21        Authorization policies required by all routes in the group.
 22    .PARAMETER ExtraImports
 23        Extra namespaces added to all routes in the group.
 24    .PARAMETER ExtraRefs
 25        Extra assemblies referenced by all routes in the group.
 26    .PARAMETER Arguments
 27        Extra arguments injected into all routes in the group.
 28    .PARAMETER ScriptBlock
 29        ScriptBlock within which you call Add-KrMapRoute for relative paths.
 30    .PARAMETER FileName
 31        Path to a script file containing the scriptblock to execute.
 32    .PARAMETER NoInherit
 33        If set, do not inherit options from the parent group; only apply the current parameters.
 34    .PARAMETER PassThru
 35        If specified, the function will return the created route object.
 36    .EXAMPLE
 37        Add-KrRouteGroup -Prefix '/todoitems' -AuthorizationPolicy 'RequireUser' -ScriptBlock {
 38            Add-KrMapRoute -Verbs Get  -Path '/'      -ScriptBlock { 'all todos' }
 39            Add-KrMapRoute -Verbs Get  -Path '/{id}'  -ScriptBlock { "todo $($Context.Request.RouteValues['id'])" }
 40            Add-KrMapRoute -Verbs Post -Path '/'      -ScriptBlock { write-KrResponse -InputObject 'create' }
 41        }
 42        Adds a new route group to the specified Kestrun server with the given prefix and options.
 43    .EXAMPLE
 44        Add-KrRouteGroup -Prefix '/todoitems' -FileName 'C:\Scripts\TodoItems.ps1'
 45        Add the new route group defined in the specified file.
 46    .NOTES
 47        This function is part of the Kestrun PowerShell module and is used to manage routes
 48#>
 49function Add-KrRouteGroup {
 50    [KestrunRuntimeApi('Definition')]
 51    [CmdletBinding(DefaultParameterSetName = 'ScriptBlock', PositionalBinding = $true)]
 52    param(
 53        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 54        [Kestrun.Hosting.KestrunHost]$Server,
 55
 56        [Parameter(Mandatory, ParameterSetName = 'ScriptBlockWithOptions')]
 57        [Parameter(Mandatory, ParameterSetName = 'FileNameWithOptions')]
 58        [Kestrun.Hosting.Options.MapRouteOptions]$Options,
 59
 60        [Parameter(ParameterSetName = 'ScriptBlock')]
 61        [Parameter(ParameterSetName = 'FileName')]
 62        [string]$Prefix,
 63
 64        [Parameter(ParameterSetName = 'ScriptBlock')]
 65        [Parameter(ParameterSetName = 'FileName')]
 66        [string[]]$AuthorizationSchema,
 67
 68        [Parameter(ParameterSetName = 'ScriptBlock')]
 69        [Parameter(ParameterSetName = 'FileName')]
 70        [string[]]$AuthorizationPolicy,
 71
 72        [Parameter(ParameterSetName = 'ScriptBlock')]
 73        [Parameter(ParameterSetName = 'FileName')]
 74        [string[]]$ExtraImports,
 75
 76        [Parameter(ParameterSetName = 'ScriptBlock')]
 77        [Parameter(ParameterSetName = 'FileName')]
 78        [System.Reflection.Assembly[]]$ExtraRefs,
 79
 80        [Parameter(ParameterSetName = 'ScriptBlock')]
 81        [Parameter(ParameterSetName = 'FileName')]
 82        [hashtable]$Arguments,
 83
 84        [Parameter(Mandatory, Position = 0, ParameterSetName = 'ScriptBlock')]
 85        [Parameter(Mandatory, Position = 0, ParameterSetName = 'ScriptBlockWithOptions')]
 86        [scriptblock]$ScriptBlock,
 87
 88        [Parameter(Mandatory, ParameterSetName = 'FileName')]
 89        [Parameter(Mandatory, ParameterSetName = 'FileNameWithOptions')]
 90        [string]$FileName,
 91
 92        [Parameter()]
 93        [switch]$NoInherit,
 94
 95        [Parameter()]
 96        [switch]$PassThru
 97    )
 98    begin {
 99        # Ensure the server instance is resolved
 0100        $Server = Resolve-KestrunServer -Server $Server
 101    }
 102    process {
 103
 0104        if ($PSCmdlet.ParameterSetName -like 'FileName*') {
 0105            if (-not (Test-Path -Path $FileName)) {
 0106                throw "The specified file path does not exist: $FileName"
 107            }
 0108            $code = Get-Content -Path $FileName -Raw
 0109            $ScriptBlock = [scriptblock]::Create($code)
 110        }
 111
 112        # Normalize prefix: allow "todoitems" or "/todoitems"
 0113        if (-not [string]::IsNullOrWhiteSpace($Prefix) -and -not $Prefix.StartsWith('/')) {
 0114            $Prefix = "/$Prefix"
 115        }
 116        # Discover parent template (stack always aims to hold MapRouteOptions)
 0117        $parentOpts = $null
 0118        if (-not $NoInherit -and $server.RouteGroupStack.Count) {
 0119            $parentOpts = $server.RouteGroupStack.Peek()
 120        }
 121
 0122        $curr = if ($PSCmdlet.ParameterSetName -like '*WithOptions') {
 0123            $Options
 124        } else {
 0125            $dict = $null
 0126            if ($Arguments) {
 0127                $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0128                foreach ($k in $Arguments.Keys) { $dict[$k] = $Arguments[$k] }
 129            }
 0130            New-KrMapRouteOption -Property @{
 0131                RequireSchemes = $AuthorizationSchema
 0132                RequirePolicies = $AuthorizationPolicy
 0133                Pattern = $Prefix
 0134                ScriptCode = @{
 0135                    ExtraImports = $ExtraImports
 0136                    ExtraRefs = $ExtraRefs
 0137                    Arguments = $dict
 138                }
 139            }
 140        }
 141        # Merge with parent (parent first, then current overrides)
 0142        if ($parentOpts) {
 0143            $curr = _KrMerge-MRO -Parent $parentOpts -Child $curr
 144        }
 145
 0146        $Server.RouteGroupStack.Push($curr)
 147        try {
 0148            & $ScriptBlock
 149        } catch {
 0150            $msg = "Error inside route group '$($current.Prefix)': $($_.Exception.Message)"
 0151            throw [System.Exception]::new($msg, $_.Exception)
 152        } finally {
 0153            $null = $Server.RouteGroupStack.Pop()
 0154            if ($restorePath) { Set-Location $restorePath }
 155        }
 156
 0157        if ($PassThru.IsPresent) {
 158            # if the PassThru switch is specified, return the modified server instance
 0159            return $Server
 160        }
 161    }
 162}
 163

Methods/Properties

Add-KrRouteGroup()