< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Enable-KrStatusCodePage
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Enable-KrStatusCodePage.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 40
Coverable lines: 40
Total lines: 220
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 10/13/2025 - 16:52:37 Line coverage: 0% (0/40) Total lines: 220 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Enable-KrStatusCodePage.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Enables Status Code Pages for a Kestrun server.
 4.DESCRIPTION
 5    Wraps KestrunHostStatusCodePagesExtensions to configure how 4xx–5xx responses
 6    produce a body: default text, redirect, re-execute, template body, options object,
 7    or a scripted handler via LanguageOptions (PowerShell/C#).
 8.PARAMETER Server
 9    The Kestrun server instance (resolved if omitted via Resolve-KestrunServer).
 10.PARAMETER LocationFormat
 11    The location URL for Redirect mode (e.g. "/error/{0}").
 12.PARAMETER PathFormat
 13    The path to re-execute for ReExecute mode (e.g. "/error/{0}").
 14.PARAMETER QueryFormat
 15    The query string to append for ReExecute mode (e.g. "?code={0}").
 16.PARAMETER ContentType
 17    The content type for Template mode (e.g. "text/plain" or "application/json").
 18.PARAMETER BodyFormat
 19    The body format string for Template mode (e.g. "Oops {0}").
 20.PARAMETER LanguageOptions
 21    A pre-configured LanguageOptions object for custom scripted handling.
 22.PARAMETER ScriptBlock
 23    A PowerShell script block for custom scripted handling. The script block
 24    receives a single parameter: the HttpContext object.
 25.PARAMETER Code
 26    A code string for custom scripted handling.
 27.PARAMETER Language
 28    The scripting language for the code string (PowerShell, CSharp, VisualBasic).
 29.PARAMETER CodeFilePath
 30    A file path to a code file for custom scripted handling (.ps1, .cs, .vb).
 31.PARAMETER ExtraRefs
 32    Additional assembly references for custom scripted handling.
 33.PARAMETER Arguments
 34    Additional arguments (key-value pairs) for custom scripted handling.
 35.PARAMETER PassThru
 36    If specified, the function will return the created route object.
 37.EXAMPLE
 38    Enable-KrStatusCodePage -Mode Default
 39.EXAMPLE
 40    Enable-KrStatusCodePage -Mode Redirect -Location "/error/{0}"
 41.EXAMPLE
 42    Enable-KrStatusCodePage -Mode ReExecute -Path "/error/{0}" -Query "?code={0}"
 43.EXAMPLE
 44    Enable-KrStatusCodePage -Mode Template -ContentType "text/plain" -BodyFormat "Oops {0}"
 45.EXAMPLE
 46    $opts = [Microsoft.AspNetCore.Diagnostics.StatusCodePagesOptions]::new()
 47    Enable-KrStatusCodePage -Mode Options -Options $opts
 48.EXAMPLE
 49    $lo = [Kestrun.Hosting.Options.LanguageOptions]::new()
 50    $lo.ExtraImports = @('System.Net')
 51    $lo.Code = {
 52        param($Context)
 53        $statusCode = $Context.Response.StatusCode
 54        $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString()
 55        $message = "Custom handler: Status code $statusCode - $reasonPhrase"
 56        $Context.Response.ContentType = 'text/plain'
 57        $Context.Response.WriteAsync($message) | Out-Null
 58    }
 59    Enable-KrStatusCodePage -Mode LanguageOptions -LanguageOptions $lo
 60.EXAMPLE
 61    Enable-KrStatusCodePage -Mode ScriptBlock -ScriptBlock {
 62        param($Context)
 63        $statusCode = $Context.Response.StatusCode
 64        $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString()
 65        $message = "Custom handler: Status code $statusCode - $reasonPhrase"
 66        $Context.Response.ContentType = 'text/plain'
 67        $Context.Response.WriteAsync($message) | Out-Null
 68    }
 69.EXAMPLE
 70    Enable-KrStatusCodePage -Mode Code -Language PowerShell -Code {
 71        param($Context)
 72        $statusCode = $Context.Response.StatusCode
 73        $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString()
 74        $message = "Custom handler: Status code $statusCode - $reasonPhrase"
 75        $Context.Response.ContentType = 'text/plain'
 76        $Context.Response.WriteAsync($message) | Out-Null
 77    }
 78.EXAMPLE
 79    Enable-KrStatusCodePage -Mode CodeFilePath -CodeFilePath 'C:\Scripts\StatusCodeHandler.ps1'
 80.NOTES
 81    This function is part of the Kestrun PowerShell module and is used to manage Kestrun servers
 82    and their middleware components.
 83#>
 84function Enable-KrStatusCodePage {
 85    [KestrunRuntimeApi('Definition')]
 86    [CmdletBinding(DefaultParameterSetName = 'Default')]
 87    [OutputType([Kestrun.Hosting.KestrunHost])]
 88    param(
 89        [Parameter(ValueFromPipeline = $true)]
 90        [Kestrun.Hosting.KestrunHost] $Server,
 91
 92        # Redirect
 93        [Parameter(Mandatory = $true, ParameterSetName = 'Redirect')]
 94        [string] $LocationFormat,
 95
 96        # Re-execute
 97        [Parameter(Mandatory = $true, ParameterSetName = 'ReExecute')]
 98        [string] $PathFormat,
 99        [Parameter(ParameterSetName = 'ReExecute')]
 100        [string] $QueryFormat,
 101
 102        # Template body
 103        [Parameter(Mandatory = $true, ParameterSetName = 'Template')]
 104        [string] $ContentType,
 105        [Parameter(Mandatory = $true, ParameterSetName = 'Template')]
 106        [string] $BodyFormat,
 107
 108        # Custom via LanguageOptions or ScriptBlock
 109        [Parameter(ParameterSetName = 'LanguageOptions')]
 110        [Kestrun.Hosting.Options.LanguageOptions] $LanguageOptions,
 111
 112        [Parameter(ParameterSetName = 'ScriptBlock')]
 113        [scriptblock] $ScriptBlock,
 114
 115        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 116        [Alias('CodeBlock')]
 117        [string]$Code,
 118        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 119        [Kestrun.Scripting.ScriptLanguage]$Language,
 120
 121        [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')]
 122        [string]$CodeFilePath,
 123
 124        [Parameter(ParameterSetName = 'ScriptBlock')]
 125        [Parameter(ParameterSetName = 'Code')]
 126        [Parameter(ParameterSetName = 'CodeFilePath')]
 127        [System.Reflection.Assembly[]]$ExtraRefs = $null,
 128
 129        [Parameter(ParameterSetName = 'ScriptBlock')]
 130        [Parameter(ParameterSetName = 'Code')]
 131        [Parameter(ParameterSetName = 'CodeFilePath')]
 132        [hashtable]$Arguments,
 133
 134        [switch] $PassThru
 135    )
 136    begin {
 0137        $Server = Resolve-KestrunServer -Server $Server
 138    }
 139    process {
 0140        $statusCodeOptions = [Kestrun.Hosting.Options.StatusCodeOptions]::new($Server)
 0141        switch ($PSCmdlet.ParameterSetName) {
 142            'Default' {
 143                # Nothing to configure; use default plain text handler
 144            }
 145            'Template' {
 0146                if ([string]::IsNullOrWhiteSpace($ContentType)) { throw '-ContentType is required for Mode=Template.' }
 0147                if ([string]::IsNullOrWhiteSpace($BodyFormat)) { throw '-BodyFormat is required for Mode=Template.' }
 0148                $statusCodeOptions.ContentType = $ContentType
 0149                $statusCodeOptions.BodyFormat = $BodyFormat
 150            }
 151            'Redirect' {
 0152                if ([string]::IsNullOrWhiteSpace($LocationFormat)) { throw '-LocationFormat is required for Mode=Redirec
 0153                $statusCodeOptions.LocationFormat = $LocationFormat
 154            }
 155            'ReExecute' {
 0156                if ([string]::IsNullOrWhiteSpace($PathFormat)) { throw '-Path is required for Mode=ReExecute.' }
 0157                $statusCodeOptions.PathFormat = $PathFormat
 0158                $statusCodeOptions.QueryFormat = $QueryFormat
 159            }
 160            'LanguageOptions' {
 0161                $statusCodeOptions.LanguageOptions = $LanguageOptions
 162            }
 163            default {
 0164                $lo = [Kestrun.Hosting.Options.LanguageOptions]::new()
 0165                $lo.ExtraImports = $ExtraImports
 0166                $lo.ExtraRefs = $ExtraRefs
 0167                if ($null -ne $Arguments) {
 0168                    $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0169                    foreach ($key in $Arguments.Keys) {
 0170                        $dict[$key] = $Arguments[$key]
 171                    }
 0172                    $lo.Arguments = $dict
 173                }
 174
 0175                switch ($PSCmdlet.ParameterSetName) {
 176                    'ScriptBlock' {
 0177                        $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 0178                        $lo.Code = $ScriptBlock.ToString()
 179                    }
 180                    'Code' {
 0181                        $lo.Language = $Language
 0182                        $lo.Code = $Code
 183                    }
 184                    'CodeFilePath' {
 0185                        if (-not (Test-Path -Path $CodeFilePath)) {
 0186                            throw "The specified code file path does not exist: $CodeFilePath"
 187                        }
 0188                        $extension = Split-Path -Path $CodeFilePath -Extension
 0189                        switch ($extension) {
 190                            '.ps1' {
 0191                                $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 192                            }
 193                            '.cs' {
 0194                                $lo.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp
 195                            }
 196                            '.vb' {
 0197                                $lo.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic
 198                            }
 199                            default {
 0200                                throw "Unsupported '$extension' code file extension."
 201                            }
 202                        }
 0203                        $lo.Code = Get-Content -Path $CodeFilePath -Raw
 204                    }
 205                    default {
 0206                        throw "Unrecognized ParameterSetName: $($PSCmdlet.ParameterSetName)"
 207                    }
 208                }
 0209                $statusCodeOptions.LanguageOptions = $lo
 210            }
 211        }
 212        # Assign the configured options to the server instance
 0213        $Server.StatusCodeOptions = $statusCodeOptions
 214
 0215        if ($PassThru.IsPresent) {
 216            # if the PassThru switch is specified, return the modified server instance
 0217            return $Server
 218        }
 219    }
 220}

Methods/Properties

Enable-KrStatusCodePage()