< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Cors.Add-KrCorsPolicy
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Cors/Add-KrCorsPolicy.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 96
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/14/2025 - 20:04:52 Line coverage: 0% (0/12) Total lines: 96 Tag: Kestrun/Kestrun@a05ac8de57c6207e227b92ba360e9d58869ac80a

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Cors/Add-KrCorsPolicy.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Adds a CORS policy to the Kestrun server.
 4.DESCRIPTION
 5    This function adds a CORS policy to the Kestrun server. It can be used to configure CORS policies for the Kestrun se
 6.PARAMETER Builder
 7    The CORS policy builder to be used to create the CORS policy.
 8.PARAMETER Server
 9    The Kestrun server to which the CORS policy will be added.
 10.PARAMETER Name
 11    The name of the CORS policy to be added.
 12.PARAMETER Default
 13    Specifies that the CORS policy is the default policy.
 14.PARAMETER AllowAll
 15    Specifies that the CORS policy should allow all origins, methods, and headers.
 16.PARAMETER PassThru
 17    Specifies that the CORS policy builder should be passed through the pipeline.
 18.EXAMPLE
 19    New-KrCorsPolicyBuilder | Set-KrCorsMethod -Any | Set-KrCorsHeader -Any | Add-KrCorsPolicy -Server $server -Name 'My
 20.EXAMPLE
 21    New-KrCorsPolicyBuilder | Set-KrCorsMethod -Any | Set-KrCorsHeader -Any | Add-KrCorsPolicy -Server $server -Default
 22.EXAMPLE
 23    Add-KrCorsPolicy -Server $server -Default -AllowAll
 24.EXAMPLE
 25     New-KrCorsPolicyBuilder | Set-KrCorsExposedHeader -Headers 'X-Total-Count','X-Page-Number'|
 26        Add-KrCorsPolicy -Server $server -Name 'MyCORSPolicy'
 27.NOTES
 28    This function is part of the Kestrun runtime API and is used to configure CORS policies for the Kestrun server.
 29#>
 30function Add-KrCorsPolicy {
 31    [KestrunRuntimeApi('Definition')]
 32    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
 33    [CmdletBinding(DefaultParameterSetName = 'Named')]
 34    [OutputType([Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder])]
 35    param(
 36        # Builder path (pipeline-friendly)
 37        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Named')]
 38        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Default')]
 39        [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]$Builder,
 40
 41        # Server
 42        [Parameter()]
 43        [Kestrun.Hosting.KestrunHost]$Server,
 44
 45        # Named policy
 46        [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Named')]
 47        [Parameter(Mandatory = $true, ParameterSetName = 'Named')]
 48        [ValidateNotNullOrEmpty()]
 49        [string]$Name,
 50
 51        # Default policy
 52        [Parameter(Mandatory = $true, ParameterSetName = 'Default')]
 53        [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Default')]
 54        [switch]$Default,
 55
 56        # Convenience: allow everything (no builder required)
 57        [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Named')]
 58        [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Default')]
 59        [switch]$AllowAll,
 60
 61        # Pipeline continuation
 62        [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Named')]
 63        [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Default')]
 64        [switch]$PassThru
 65    )
 66
 67    begin {
 068        $Server = Resolve-KestrunServer -Server $Server
 69    }
 70
 71    process {
 072        if ($AllowAll.IsPresent -and $PSBoundParameters.ContainsKey('Builder')) {
 073            throw 'Do not pipe a builder when using -AllowAll. Use -AllowAll without a builder.'
 74        }
 75
 076        if ($AllowAll.IsPresent) {
 77            # Allow all origins, methods, and headers
 078            if ([string]::IsNullOrEmpty($Name)) {
 079                [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsDefaultPolicyAllowAll($Server) | Out-Null
 80            } else {
 081                [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsPolicyAllowAll($Server, $Name) | Out-Null
 82            }
 83        } else {
 84            # Use the provided builder to add the CORS policy
 085            if ([string]::IsNullOrEmpty($Name)) {
 086                [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsDefaultPolicy($Server, $Builder) | Out-Nul
 87            } else {
 088                [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsPolicy($Server, $Name, $Builder) | Out-Nul
 89            }
 90        }
 91        # Pass the builder through the pipeline if requested
 092        if ($PassThru.IsPresent) {
 093            return $Builder
 94        }
 95    }
 96}

Methods/Properties

Add-KrCorsPolicy()