< 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@9d3a582b2d63930269564a7591aa77ef297cadeb
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

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    try {
 36        # Check if the Context.Response is available
 037        if ($null -ne $Context.Response) {
 38
 039            $readOnlyDictionary = [Kestrun.Utilities.ReadOnlyDictionaryAdapter]::new($Variables)
 40
 041            switch ($PSCmdlet.ParameterSetName) {
 42                'FilePath' {
 43                    # Resolve the file path relative to the Kestrun root if necessary
 044                    $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test
 045                    Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath"
 46                    # Call the C# method on the $Context.Response object
 047                    $Context.Response.WriteHtmlResponseFromFile($resolvedPath, $readOnlyDictionary, $StatusCode)
 048                    Write-Information "HTML response written for $FilePath"
 49                }
 50                'Template' {
 51                    # Call the C# method on the $Context.Response object
 052                    $Context.Response.WriteHtmlResponse($Template, $readOnlyDictionary, $StatusCode)
 053                    Write-Information 'HTML response written from template'
 54                }
 55            }
 56        }
 57    } catch {
 58        # Handle any errors that occur during the file response writing
 059        Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_
 60    }
 61}
 62

Methods/Properties

Write-KrHtmlResponse()