< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.Add-KrListener
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Add-KrListener.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
56%
Covered lines: 9
Uncovered lines: 7
Coverable lines: 16
Total lines: 100
Line coverage: 56.2%
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/Server/Add-KrListener.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 CertPath
 13        The path to the SSL certificate file. This parameter is mandatory if using HTTPS.
 14    .PARAMETER CertPassword
 15        The password for the SSL certificate, if applicable. This parameter is optional.
 16    .PARAMETER SelfSignedCert
 17        If specified, a self-signed certificate will be generated and used for HTTPS. This parameter is optional.
 18    .PARAMETER X509Certificate
 19        An X509Certificate2 object representing the SSL certificate. This parameter is mandatory if using HTTPS
 20    .PARAMETER Protocols
 21        The HTTP protocols to use (e.g., Http1, Http2). Defaults to Http1 for HTTP listeners and Http1OrHttp2 for HTTPS 
 22    .PARAMETER UseConnectionLogging
 23        If specified, enables connection logging for the listener. This is useful for debugging and monitoring purposes.
 24    .PARAMETER PassThru
 25        If specified, the cmdlet will return the modified server instance after adding the listener.
 26    .EXAMPLE
 27        New-KrServer -Name 'MyKestrunServer'
 28        Creates a new Kestrun server instance with the specified name.
 29    .NOTES
 30        This function is designed to be used after the server has been configured with routes and listeners.
 31#>
 32function Add-KrListener {
 33    [KestrunRuntimeApi('Definition')]
 34    [CmdletBinding(defaultParameterSetName = 'NoCert')]
 35    [OutputType([Kestrun.Hosting.KestrunHost])]
 36    param(
 37        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 38        [Kestrun.Hosting.KestrunHost]$Server,
 39
 40        [Parameter()]
 41        [int]$Port = 0,
 42
 43        [System.Net.IPAddress]$IPAddress = [System.Net.IPAddress]::Loopback,
 44        [Parameter(mandatory = $true, ParameterSetName = 'CertFile')]
 45        [string]$CertPath,
 46
 47        [Parameter(mandatory = $false, ParameterSetName = 'CertFile')]
 48        [SecureString]$CertPassword = $null,
 49
 50        [Parameter(ParameterSetName = 'SelfSignedCert')]
 51        [switch]$SelfSignedCert,
 52
 53        [Parameter(mandatory = $true, ParameterSetName = 'x509Certificate')]
 54        [System.Security.Cryptography.X509Certificates.X509Certificate2]$X509Certificate = $null,
 55
 56        [Parameter(ParameterSetName = 'x509Certificate')]
 57        [Parameter(ParameterSetName = 'CertFile')]
 58        [Parameter(ParameterSetName = 'SelfSignedCert')]
 59        [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]$Protocols,
 60
 61        [Parameter()]
 62        [switch]$UseConnectionLogging,
 63
 64        [Parameter()]
 65        [switch]$PassThru
 66    )
 67    begin {
 68        # Ensure the server instance is resolved
 169        $Server = Resolve-KestrunServer -Server $Server
 170        if ($null -eq $Server) {
 071            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 72        }
 73    }
 74    process {
 75
 76        # Validate parameters based on the parameter set
 177        if ($null -eq $Protocols) {
 178            if ($PSCmdlet.ParameterSetName -eq 'NoCert') {
 179                $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1
 80            } else {
 081                $Protocols = [Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols]::Http1AndHttp2
 82            }
 83        }
 184        if ($PSCmdlet.ParameterSetName -eq 'CertFile') {
 085            if (-not (Test-Path $CertPath)) {
 086                throw "Certificate file not found: $CertPath"
 87            }
 088            $X509Certificate = Import-KrCertificate -FilePath $CertPath -Password $CertPassword
 189        } elseif ($SelfSignedCert.IsPresent) {
 090            $X509Certificate = New-KrSelfSignedCertificate -DnsNames localhost, 127.0.0.1 -ValidDays 30
 91        }
 92
 293        $Server.ConfigureListener($Port, $IPAddress, $X509Certificate, $Protocols, $UseConnectionLogging.IsPresent) | Ou
 194        if ($PassThru.IsPresent) {
 95            # Return the modified server instance
 096            return $Server
 97        }
 98    }
 99}
 100

Methods/Properties

Add-KrListener()