< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.Add-KrEndpoint
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Add-KrEndpoint.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 31
Coverable lines: 31
Total lines: 138
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/31) Total lines: 138 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Add-KrEndpoint.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Creates a new Kestrun server instance with specified options and listeners.
 4    .DESCRIPTION
 5        This function initializes a new Kestrun server instance, allowing configuration of various options and listeners
 6    .PARAMETER Server
 7        The Kestrun server instance to configure. This parameter is Mandatory and must be a valid server object.
 8    .PARAMETER Port
 9        The port on which the server will listen for incoming requests. The default is 0, which means a random available
 10    .PARAMETER IPAddress
 11        The IP address on which the server will listen. Defaults to [System.Net.IPAddress]::Any, which means it will lis
 12    .PARAMETER HostName
 13        The hostname for the listener. This parameter is Mandatory if using the 'HostName' parameter set.
 14    .PARAMETER Uri
 15        The full URI for the listener. This parameter is Mandatory if using the 'Uri' parameter set.
 16    .PARAMETER AddressFamily
 17        An array of address families to filter resolved addresses (e.g., IPv4-only). This parameter is optional.
 18    .PARAMETER CertPath
 19        The path to the SSL certificate file. This parameter is Mandatory if using HTTPS.
 20    .PARAMETER CertPassword
 21        The password for the SSL certificate, if applicable. This parameter is optional.
 22    .PARAMETER SelfSignedCert
 23        If specified, a self-signed certificate will be generated and used for HTTPS. This parameter is optional.
 24    .PARAMETER X509Certificate
 25        An X509Certificate2 object representing the SSL certificate. This parameter is Mandatory if using HTTPS
 26    .PARAMETER Protocols
 27        The HTTP protocols to use (e.g., Http1, Http2). Defaults to Http1 for HTTP listeners and Http1OrHttp2 for HTTPS 
 28    .PARAMETER UseConnectionLogging
 29        If specified, enables connection logging for the listener. This is useful for debugging and monitoring purposes.
 30    .PARAMETER PassThru
 31        If specified, the cmdlet will return the modified server instance after adding the listener.
 32    .EXAMPLE
 33        New-KrServer -Name 'MyKestrunServer'
 34        Creates a new Kestrun server instance with the specified name.
 35    .NOTES
 36        This function is designed to be used after the server has been configured with routes and listeners.
 37#>
 38function Add-KrEndpoint {
 39    [KestrunRuntimeApi('Definition')]
 40    [CmdletBinding(defaultParameterSetName = 'NoCert')]
 41    [OutputType([Kestrun.Hosting.KestrunHost])]
 42    param(
 43        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 44        [Kestrun.Hosting.KestrunHost]$Server,
 45        [Parameter()]
 46        [int]$Port = 0,
 47        [Parameter()]
 48        [System.Net.IPAddress]$IPAddress,
 49        [Parameter()]
 50        [string]$HostName,
 51        [Parameter()]
 52        [System.Uri]$Uri,
 53        [Parameter()]
 54        [System.Net.Sockets.AddressFamily[]]$AddressFamily,
 55
 56        [Parameter(mandatory = $true, ParameterSetName = 'CertFile')]
 57        [string]$CertPath,
 58
 59        [Parameter(mandatory = $false, ParameterSetName = 'CertFile')]
 60        [SecureString]$CertPassword = $null,
 61
 62        [Parameter(ParameterSetName = 'SelfSignedCert')]
 63        [switch]$SelfSignedCert,
 64
 65        [Parameter(Mandatory = $true, ParameterSetName = 'x509Certificate')]
 66        [System.Security.Cryptography.X509Certificates.X509Certificate2]$X509Certificate = $null,
 67
 68        [Parameter(ParameterSetName = 'x509Certificate')]
 69        [Parameter(ParameterSetName = 'CertFile')]
 70        [Parameter(ParameterSetName = 'SelfSignedCert')]
 71        [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]$Protocols,
 72
 73        [Parameter()]
 74        [switch]$UseConnectionLogging,
 75
 76        [Parameter()]
 77        [switch]$PassThru
 78    )
 79    begin {
 80        # Ensure the server instance is resolved
 081        $Server = Resolve-KestrunServer -Server $Server
 082        if ($null -ne $IPAddress) {
 083            if (-not [string]::IsNullOrEmpty($HostName)) {
 084                throw "Cannot specify both IPAddress and HostName. Please choose one."
 85            }
 086            if ($null -ne $Uri) {
 087                throw "Cannot specify both IPAddress and Uri. Please choose one."
 88            }
 089            if ($AddressFamily -and -not ($AddressFamily -contains $IPAddress.AddressFamily)) {
 090                throw "The specified IPAddress does not match the provided AddressFamily filter."
 91            }
 92        } else {
 93
 094            if ($null -ne $Uri -and (-not ([string]::IsNullOrEmpty($HostName)))) {
 095                throw "Cannot specify both HostName and Uri. Please choose one."
 96            }
 097            if ($null -eq $Uri -and [string]::IsNullOrEmpty($HostName)) {
 098                $IPAddress = [System.Net.IPAddress]::Loopback
 99            }
 100        }
 101    }
 102    process {
 103
 104        # Validate parameters based on the parameter set
 0105        if ($null -eq $Protocols) {
 0106            if ($PSCmdlet.ParameterSetName -eq 'NoCert') {
 0107                $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1
 108            } else {
 0109                $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1AndHttp2
 110            }
 111        }
 0112        if ($PSCmdlet.ParameterSetName -eq 'CertFile') {
 0113            if (-not (Test-Path $CertPath)) {
 0114                throw "Certificate file not found: $CertPath"
 115            }
 0116            $X509Certificate = Import-KrCertificate -FilePath $CertPath -Password $CertPassword
 0117        } elseif ($SelfSignedCert.IsPresent) {
 0118            $X509Certificate = New-KrSelfSignedCertificate -DnsNames localhost, 127.0.0.1 -ValidDays 30
 119        }
 120
 121
 0122        if (-not ([string]::IsNullOrEmpty($HostName))) {
 0123            $Server.ConfigureListener($HostName, $Port, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent, $
 0124        } elseif ($null -ne $Uri) {
 0125            $Server.ConfigureListener($Uri, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent, $AddressFamil
 0126        } elseif ($null -ne $IPAddress) {
 0127            $Server.ConfigureListener($Port, $IPAddress, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent) 
 128        } else {
 0129            throw "Invalid parameter set: $($PSCmdlet.ParameterSetName). Please specify either HostName, Uri, or IPAddre
 130        }
 131
 0132        if ($PassThru.IsPresent) {
 133            # Return the modified server instance
 0134            return $Server
 135        }
 136    }
 137}
 138

Methods/Properties

Add-KrEndpoint()