< Summary - Kestrun — Combined Coverage

Information
Class: Public.Route.Add-KrStaticMapOverride
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrStaticMapOverride.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 37
Coverable lines: 37
Total lines: 192
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

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrStaticMapOverride.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        The HTTP verbs (GET, POST, etc.) that the route should respond to.
 14    .PARAMETER Pattern
 15        The URL path for the new route.
 16    .PARAMETER ScriptBlock
 17        The script block to be executed when the route is accessed.
 18    .PARAMETER Code
 19        The code to be executed when the route is accessed, used in conjunction with the Language parameter.
 20    .PARAMETER Language
 21        The scripting language of the code to be executed.
 22    .PARAMETER CodeFilePath
 23        The file path to the code to be executed when the route is accessed.
 24    .PARAMETER AuthorizationSchema
 25        An optional array of authorization schemes that the route requires.
 26    .PARAMETER AuthorizationPolicy
 27        An optional array of authorization policies that the route requires.
 28    .PARAMETER ExtraImports
 29        An optional array of additional namespaces to import for the route.
 30    .PARAMETER ExtraRefs
 31        An optional array of additional assemblies to reference for the route.
 32    .PARAMETER AllowDuplicate
 33        If specified, allows the addition of duplicate routes with the same path and HTTP verb.
 34    .PARAMETER Arguments
 35        An optional hashtable of arguments to pass to the script block or code.
 36    .PARAMETER DuplicateAction
 37        Specifies the action to take if a duplicate route is detected. Options are 'Throw', 'Skip', 'Allow', or 'Warn'.
 38        Default is 'Throw', which will raise an error if a duplicate route is found.
 39    .PARAMETER PassThru
 40        If specified, the function will return the created route object.
 41    .OUTPUTS
 42        Returns the Kestrun server instance with the new route added.
 43    .EXAMPLE
 44        Add-KrMapRoute -Server $myServer -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" }
 45        Adds a new map route to the specified Kestrun server with the given path and script block.
 46    .EXAMPLE
 47
 48        Add-KrMapRoute -Server $myServer -Path "/myroute" -Code "Write-Host 'Hello, World!'" -Language PowerShell
 49        Adds a new map route to the specified Kestrun server with the given path and code.
 50    .EXAMPLE
 51        Get-KrServer | Add-KrMapRoute -Path "/myroute" -ScriptBlock { Write-Host "Hello, World!" } -PassThru
 52        Adds a new map route to the current Kestrun server and returns the route object.
 53    .NOTES
 54        This function is part of the Kestrun PowerShell module and is used to manage routes
 55#>
 56function Add-KrStaticMapOverride {
 57    [KestrunRuntimeApi('Definition')]
 58    [CmdletBinding(defaultParameterSetName = 'ScriptBlock', PositionalBinding = $true)]
 59    [OutputType([Kestrun.Hosting.KestrunHost])]
 60    param(
 61        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 62        [Kestrun.Hosting.KestrunHost]$Server,
 63
 64        [Parameter(Mandatory = $true, ParameterSetName = 'Options', ValueFromPipeline = $true)]
 65        [Kestrun.Hosting.Options.MapRouteOptions]$Options,
 66
 67        [Parameter(ParameterSetName = 'ScriptBlock')]
 68        [Parameter(ParameterSetName = 'Code')]
 69        [Parameter(ParameterSetName = 'CodeFilePath')]
 070        [Kestrun.Utilities.HttpVerb[]]$Verbs = @([Kestrun.Utilities.HttpVerb]::Get),
 71
 72        [Parameter(ParameterSetName = 'ScriptBlock')]
 73        [Parameter(ParameterSetName = 'Code')]
 74        [Parameter(ParameterSetName = 'CodeFilePath')]
 75        [ValidatePattern('^/')]
 76        [alias('Path')]
 77        [string]$Pattern = '/',
 78
 79        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'ScriptBlock')]
 80        [scriptblock]$ScriptBlock,
 81
 82        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 83        [string]$Code,
 84
 85        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 86        [Kestrun.Scripting.ScriptLanguage]$Language,
 87
 88        [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')]
 89        [string]$CodeFilePath,
 90
 91        [Parameter(ParameterSetName = 'ScriptBlock')]
 92        [Parameter(ParameterSetName = 'Code')]
 93        [Parameter(ParameterSetName = 'CodeFilePath')]
 94        [string[]]$AuthorizationSchema = $null,
 95
 96        [Parameter(ParameterSetName = 'ScriptBlock')]
 97        [Parameter(ParameterSetName = 'Code')]
 98        [Parameter(ParameterSetName = 'CodeFilePath')]
 99        [string[]]$AuthorizationPolicy = $null,
 100
 101        [Parameter(ParameterSetName = 'ScriptBlock')]
 102        [Parameter(ParameterSetName = 'Code')]
 103        [Parameter(ParameterSetName = 'CodeFilePath')]
 104        [string[]]$ExtraImports = $null,
 105
 106        [Parameter(ParameterSetName = 'ScriptBlock')]
 107        [Parameter(ParameterSetName = 'Code')]
 108        [Parameter(ParameterSetName = 'CodeFilePath')]
 109        [System.Reflection.Assembly[]]$ExtraRefs = $null,
 110
 111        [Parameter(ParameterSetName = 'ScriptBlock')]
 112        [Parameter(ParameterSetName = 'Code')]
 113        [Parameter(ParameterSetName = 'CodeFilePath')]
 114        [hashtable]$Arguments,
 115
 116        [Parameter()]
 117        [switch]$PassThru
 118
 119    )
 120    begin {
 121        # Ensure the server instance is resolved
 0122        $Server = Resolve-KestrunServer -Server $Server
 0123        if ($null -eq $Server) {
 0124            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 125        }
 126    }
 127    process {
 128        # -- if Options parameter is used, we can skip the rest of the parameters
 0129        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 0130            $Options = [Kestrun.Hosting.Options.MapRouteOptions]::new()
 0131            $Options.HttpVerbs = $Verbs
 0132            $Options.Pattern = $Pattern
 0133            $Options.ExtraImports = $ExtraImports
 0134            $Options.ExtraRefs = $ExtraRefs
 0135            if ($null -ne $AuthorizationSchema) {
 0136                $Options.RequireSchemes = $AuthorizationSchema
 137            }
 0138            if ($null -ne $AuthorizationPolicy) {
 0139                $Options.RequirePolicies = $AuthorizationPolicy
 140            }
 141
 0142            if ($null -ne $Arguments) {
 0143                $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0144                foreach ($key in $Arguments.Keys) {
 0145                    $dict[$key] = $Arguments[$key]
 146                }
 0147                $Options.Arguments = $dict
 148            }
 0149            switch ($PSCmdlet.ParameterSetName) {
 150                'ScriptBlock' {
 0151                    $Options.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 0152                    $Options.Code = $ScriptBlock.ToString()
 153                }
 154                'Code' {
 0155                    $Options.Language = $Language
 0156                    $Options.Code = $Code
 157                }
 158                'CodeFilePath' {
 0159                    if (-not (Test-Path -Path $CodeFilePath)) {
 0160                        throw "The specified code file path does not exist: $CodeFilePath"
 161                    }
 0162                    $extension = Split-Path -Path $CodeFilePath -Extension
 0163                    switch ($extension) {
 164                        '.ps1' {
 0165                            $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 166                        }
 167                        '.cs' {
 0168                            $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp
 169                        }
 170                        '.vb' {
 0171                            $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic
 172                        }
 173                        default {
 0174                            throw "Unsupported '$extension' code file extension."
 175                        }
 176                    }
 0177                    $Options.ValidateCodeSettings.Code = Get-Content -Path $CodeFilePath -Raw
 178                }
 179            }
 180        } else {
 0181            Write-Verbose 'Using provided MapRouteOptions instance.'
 182        }
 0183        [Kestrun.Hosting.KestrunHostMapExtensions]::AddStaticMapOverride($Server, $Options) | Out-Null
 184
 0185        if ($PassThru.IsPresent) {
 186            # if the PassThru switch is specified, return the server instance
 187            # Return the modified server instance
 0188            return $Server
 189        }
 190    }
 191}
 192

Methods/Properties

Add-KrStaticMapOverride()