< Summary - Kestrun — Combined Coverage

Information
Class: Public.Response.Write-KrHtmlResponse
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrHtmlResponse.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 10
Coverable lines: 10
Total lines: 62
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: 0% (0/10) Total lines: 61 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 22:37:32 Line coverage: 0% (0/10) Total lines: 62 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec609/12/2025 - 03:43:11 Line coverage: 0% (0/11) Total lines: 62 Tag: Kestrun/Kestrun@d160286e3020330b1eb862d66a37db2e26fc904210/13/2025 - 16:52:37 Line coverage: 0% (0/10) Total lines: 62 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrHtmlResponse.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Writes an HTML response to the HTTP response body.
 4    .DESCRIPTION
 5        Serializes the provided HTML template with variables and writes it to the HTTP response.
 6    .PARAMETER FilePath
 7        The path to the HTML file to read and write to the response. This can be a relative or absolute path.
 8    .PARAMETER Template
 9        The HTML template string to write to the response. If provided, this will override the FilePath parameter.
 10    .PARAMETER StatusCode
 11        The HTTP status code to set for the response. Defaults to 200 (OK).
 12    .PARAMETER Variables
 13        A hashtable of variables to use for template placeholders. These will be merged into the HTML template.
 14    .EXAMPLE
 15        Write-KrHtmlResponse -FilePath "C:\path\to\template.html" -StatusCode 200 -Variables @{ Title = "My Page"; Conte
 16        Reads the HTML file at "C:\path\to\template.html", merges in the variables, and writes the resulting HTML to the
 17    .NOTES
 18        This function is designed to be used in the context of a Kestrun server response.
 19#>
 20function Write-KrHtmlResponse {
 21    [KestrunRuntimeApi('Route')]
 22    [CmdletBinding(defaultParameterSetName = 'FilePath')]
 23    [KestrunRuntimeApi('Route')]
 24    [CmdletBinding()]
 25    param(
 26        [Parameter(Mandatory = $true, ParameterSetName = 'FilePath')]
 27        [string]$FilePath,
 28        [Parameter(Mandatory = $true, ParameterSetName = 'Template')]
 29        [string]$Template,
 30        [Parameter()]
 31        [int]$StatusCode = 200,
 32        [Parameter()]
 33        [hashtable]$Variables
 34    )
 35    # Only works inside a route script block where $Context is available
 036    if ($null -ne $Context.Response) {
 37        try {
 038            if ($null -eq $Variables) { $Variables = @{} }
 39            # Wrap the hashtable in a read-only dictionary for safety
 040            $readOnlyDictionary = [Kestrun.Utilities.ReadOnlyDictionaryAdapter]::new($Variables)
 41
 042            switch ($PSCmdlet.ParameterSetName) {
 43                'FilePath' {
 44                    # Resolve the file path relative to the Kestrun root if necessary
 045                    $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test
 046                    Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath"
 47                    # Call the C# method on the $Context.Response object
 048                    $Context.Response.WriteHtmlResponseFromFile($resolvedPath, $readOnlyDictionary, $StatusCode)
 49                }
 50                'Template' {
 51                    # Call the C# method on the $Context.Response object
 052                    $Context.Response.WriteHtmlResponse($Template, $readOnlyDictionary, $StatusCode)
 53                }
 54            }
 55        } catch {
 56            # Handle any errors that occur during the file response writing
 057            Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_
 58        }
 59    } else {
 060        Write-KrOutsideRouteWarning
 61    }
 62}

Methods/Properties

Write-KrHtmlResponse()