< 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@6135d944f8787fb570e4dfbacac6e80312799a86
Line coverage
0%
Covered lines: 0
Uncovered lines: 38
Coverable lines: 38
Total lines: 206
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 12/01/2025 - 20:55:19 Line coverage: 0% (0/40) Total lines: 220 Tag: Kestrun/Kestrun@638a27c2dd54103f693f023b6ba5f56a884caafa05/09/2026 - 21:51:36 Line coverage: 0% (0/38) Total lines: 206 Tag: Kestrun/Kestrun@6b24c7512a1bad61723a28d32446de0aa658293e

Coverage delta

Coverage delta 1 -1

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 LocationFormat
 9    The location URL for Redirect mode (e.g. "/error/{0}").
 10.PARAMETER PathFormat
 11    The path to re-execute for ReExecute mode (e.g. "/error/{0}").
 12.PARAMETER QueryFormat
 13    The query string to append for ReExecute mode (e.g. "?code={0}").
 14.PARAMETER ContentType
 15    The content type for Template mode (e.g. "text/plain" or "application/json").
 16.PARAMETER BodyFormat
 17    The body format string for Template mode (e.g. "Oops {0}").
 18.PARAMETER LanguageOptions
 19    A pre-configured LanguageOptions object for custom scripted handling.
 20.PARAMETER ScriptBlock
 21    A PowerShell script block for custom scripted handling. The script block
 22    receives a single parameter: the HttpContext object.
 23.PARAMETER Code
 24    A code string for custom scripted handling.
 25.PARAMETER Language
 26    The scripting language for the code string (PowerShell, CSharp, VisualBasic).
 27.PARAMETER CodeFilePath
 28    A file path to a code file for custom scripted handling (.ps1, .cs, .vb).
 29.PARAMETER ExtraRefs
 30    Additional assembly references for custom scripted handling.
 31.PARAMETER Arguments
 32    Additional arguments (key-value pairs) for custom scripted handling.
 33.EXAMPLE
 34    Enable-KrStatusCodePage -Mode Default
 35.EXAMPLE
 36    Enable-KrStatusCodePage -Mode Redirect -Location "/error/{0}"
 37.EXAMPLE
 38    Enable-KrStatusCodePage -Mode ReExecute -Path "/error/{0}" -Query "?code={0}"
 39.EXAMPLE
 40    Enable-KrStatusCodePage -Mode Template -ContentType "text/plain" -BodyFormat "Oops {0}"
 41.EXAMPLE
 42    $opts = [Microsoft.AspNetCore.Diagnostics.StatusCodePagesOptions]::new()
 43    Enable-KrStatusCodePage -Mode Options -Options $opts
 44.EXAMPLE
 45    $lo = [Kestrun.Hosting.Options.LanguageOptions]::new()
 46    $lo.ExtraImports = @('System.Net')
 47    $lo.Code = {
 48        param($Context)
 49        $statusCode = $Context.Response.StatusCode
 50        $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString()
 51        $message = "Custom handler: Status code $statusCode - $reasonPhrase"
 52        $Context.Response.ContentType = 'text/plain'
 53        $Context.Response.WriteAsync($message) | Out-Null
 54    }
 55    Enable-KrStatusCodePage -Mode LanguageOptions -LanguageOptions $lo
 56.EXAMPLE
 57    Enable-KrStatusCodePage -Mode ScriptBlock -ScriptBlock {
 58        param($Context)
 59        $statusCode = $Context.Response.StatusCode
 60        $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString()
 61        $message = "Custom handler: Status code $statusCode - $reasonPhrase"
 62        $Context.Response.ContentType = 'text/plain'
 63        $Context.Response.WriteAsync($message) | Out-Null
 64    }
 65.EXAMPLE
 66    Enable-KrStatusCodePage -Mode Code -Language PowerShell -Code {
 67        param($Context)
 68        $statusCode = $Context.Response.StatusCode
 69        $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString()
 70        $message = "Custom handler: Status code $statusCode - $reasonPhrase"
 71        $Context.Response.ContentType = 'text/plain'
 72        $Context.Response.WriteAsync($message) | Out-Null
 73    }
 74.EXAMPLE
 75    Enable-KrStatusCodePage -Mode CodeFilePath -CodeFilePath 'C:\Scripts\StatusCodeHandler.ps1'
 76.NOTES
 77    This function is part of the Kestrun PowerShell module and is used to manage Kestrun servers
 78    and their middleware components.
 79#>
 80function Enable-KrStatusCodePage {
 81    [KestrunRuntimeApi('Definition')]
 82    [CmdletBinding(DefaultParameterSetName = 'Default')]
 83    param(
 84        # Redirect
 85        [Parameter(Mandatory = $true, ParameterSetName = 'Redirect')]
 86        [string] $LocationFormat,
 87
 88        # Re-execute
 89        [Parameter(Mandatory = $true, ParameterSetName = 'ReExecute')]
 90        [string] $PathFormat,
 91
 92        [Parameter(ParameterSetName = 'ReExecute')]
 93        [string] $QueryFormat,
 94
 95        # Template body
 96        [Parameter(Mandatory = $true, ParameterSetName = 'Template')]
 97        [string] $ContentType,
 98
 99        [Parameter(Mandatory = $true, ParameterSetName = 'Template')]
 100        [string] $BodyFormat,
 101
 102        # Custom via LanguageOptions or ScriptBlock
 103        [Parameter(ParameterSetName = 'LanguageOptions')]
 104        [Kestrun.Hosting.Options.LanguageOptions] $LanguageOptions,
 105
 106        [Parameter(ParameterSetName = 'ScriptBlock')]
 107        [scriptblock] $ScriptBlock,
 108
 109        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 110        [Alias('CodeBlock')]
 111        [string]$Code,
 112
 113        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 114        [Kestrun.Scripting.ScriptLanguage]$Language,
 115
 116        [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')]
 117        [string]$CodeFilePath,
 118
 119        [Parameter(ParameterSetName = 'ScriptBlock')]
 120        [Parameter(ParameterSetName = 'Code')]
 121        [Parameter(ParameterSetName = 'CodeFilePath')]
 122        [System.Reflection.Assembly[]]$ExtraRefs = $null,
 123
 124        [Parameter(ParameterSetName = 'ScriptBlock')]
 125        [Parameter(ParameterSetName = 'Code')]
 126        [Parameter(ParameterSetName = 'CodeFilePath')]
 127        [hashtable]$Arguments
 128    )
 129    # Ensure the server instance is resolved
 0130    $Server = Resolve-KestrunServer
 131
 0132    $statusCodeOptions = [Kestrun.Hosting.Options.StatusCodeOptions]::new($Server)
 0133    switch ($PSCmdlet.ParameterSetName) {
 134        'Default' {
 135            # Nothing to configure; use default plain text handler
 136        }
 137        'Template' {
 0138            if ([string]::IsNullOrWhiteSpace($ContentType)) { throw '-ContentType is required for Mode=Template.' }
 0139            if ([string]::IsNullOrWhiteSpace($BodyFormat)) { throw '-BodyFormat is required for Mode=Template.' }
 0140            $statusCodeOptions.ContentType = $ContentType
 0141            $statusCodeOptions.BodyFormat = $BodyFormat
 142        }
 143        'Redirect' {
 0144            if ([string]::IsNullOrWhiteSpace($LocationFormat)) { throw '-LocationFormat is required for Mode=Redirect.' 
 0145            $statusCodeOptions.LocationFormat = $LocationFormat
 146        }
 147        'ReExecute' {
 0148            if ([string]::IsNullOrWhiteSpace($PathFormat)) { throw '-Path is required for Mode=ReExecute.' }
 0149            $statusCodeOptions.PathFormat = $PathFormat
 0150            $statusCodeOptions.QueryFormat = $QueryFormat
 151        }
 152        'LanguageOptions' {
 0153            $statusCodeOptions.LanguageOptions = $LanguageOptions
 154        }
 155        default {
 0156            $lo = [Kestrun.Hosting.Options.LanguageOptions]::new()
 0157            $lo.ExtraImports = $ExtraImports
 0158            $lo.ExtraRefs = $ExtraRefs
 0159            if ($null -ne $Arguments) {
 0160                $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0161                foreach ($key in $Arguments.Keys) {
 0162                    $dict[$key] = $Arguments[$key]
 163                }
 0164                $lo.Arguments = $dict
 165            }
 166
 0167            switch ($PSCmdlet.ParameterSetName) {
 168                'ScriptBlock' {
 0169                    $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 0170                    $lo.Code = $ScriptBlock.ToString()
 171                }
 172                'Code' {
 0173                    $lo.Language = $Language
 0174                    $lo.Code = $Code
 175                }
 176                'CodeFilePath' {
 0177                    if (-not (Test-Path -Path $CodeFilePath)) {
 0178                        throw "The specified code file path does not exist: $CodeFilePath"
 179                    }
 0180                    $extension = Split-Path -Path $CodeFilePath -Extension
 0181                    switch ($extension) {
 182                        '.ps1' {
 0183                            $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 184                        }
 185                        '.cs' {
 0186                            $lo.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp
 187                        }
 188                        '.vb' {
 0189                            $lo.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic
 190                        }
 191                        default {
 0192                            throw "Unsupported '$extension' code file extension."
 193                        }
 194                    }
 0195                    $lo.Code = Get-Content -Path $CodeFilePath -Raw
 196                }
 197                default {
 0198                    throw "Unrecognized ParameterSetName: $($PSCmdlet.ParameterSetName)"
 199                }
 200            }
 0201            $statusCodeOptions.LanguageOptions = $lo
 202        }
 203    }
 204    # Assign the configured options to the server instance
 0205    $Server.StatusCodeOptions = $statusCodeOptions
 206}

Methods/Properties

Enable-KrStatusCodePage()