< Summary - Kestrun — Combined Coverage

Information
Class: Public.Service.Add-KrCorsPolicy
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Service/Add-KrCorsPolicy.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 115
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/Service/Add-KrCorsPolicy.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds a CORS policy to the server.
 4    .DESCRIPTION
 5        This cmdlet allows you to configure a CORS policy for the Kestrun server.
 6        It can be used to specify allowed origins, methods, headers, and other CORS settings.
 7    .PARAMETER Server
 8        The Kestrun server instance to which the CORS policy will be added.
 9    .PARAMETER Name
 10        The name of the CORS policy.
 11    .PARAMETER Builder
 12        The CORS policy builder to configure the CORS policy.
 13    .PARAMETER AllowAnyOrigin
 14        If specified, allows any origin to access the resources.
 15    .PARAMETER AllowAnyMethod
 16        If specified, allows any HTTP method to be used in requests.
 17    .PARAMETER AllowAnyHeader
 18        If specified, allows any header to be included in requests.
 19        If not specified, only headers explicitly allowed will be included.
 20    .PARAMETER AllowCredentials
 21        If specified, allows credentials (cookies, authorization headers, etc.) to be included in requests.
 22    .PARAMETER DisallowCredentials
 23        If specified, disallows credentials in requests.
 24        If not specified, credentials will be allowed.
 25    .PARAMETER PassThru
 26        If specified, returns the modified server instance after adding the CORS policy.
 27    .EXAMPLE
 28        $server | Add-KrCorsPolicy -Name 'AllowAll' -AllowAnyOrigin -AllowAnyMethod -AllowAnyHeader
 29        This example adds a CORS policy named 'AllowAll' to the server, allowing any origin, method, and header.
 30    .EXAMPLE
 31        $server | Add-KrCorsPolicy -Name 'CustomPolicy' -Builder $builder
 32        This example adds a CORS policy named 'CustomPolicy' to the server using the specified CORS policy builder.
 33    .EXAMPLE
 34        $server | Add-KrCorsPolicy -Server $server -Name 'CustomPolicy' -AllowAnyOrigin -AllowAnyMethod -AllowAnyHeader
 35        This example adds a CORS policy named 'CustomPolicy' to the server, allowing any origin, method, and header.
 36    .NOTES
 37        This cmdlet is used to configure CORS policies for the Kestrun server, allowing you to control cross-origin requ
 38    .LINK
 39        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.cors.infrastructure.corspolicybuilder?view=asp
 40#>
 41function Add-KrCorsPolicy {
 42    [KestrunRuntimeApi('Definition')]
 43    [CmdletBinding(defaultParameterSetName = 'Items')]
 44    [OutputType([Kestrun.Hosting.KestrunHost])]
 45    param(
 46        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 47        [Kestrun.Hosting.KestrunHost]$Server,
 48
 49        [Parameter(Mandatory = $true)]
 50        [string]$Name,
 51
 52        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 53        [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]$Builder,
 54
 55        [Parameter(ParameterSetName = 'Items')]
 56        [switch]$AllowAnyOrigin,
 57
 58        [Parameter(ParameterSetName = 'Items')]
 59        [switch]$AllowAnyMethod,
 60
 61        [Parameter(ParameterSetName = 'Items')]
 62        [switch]$AllowAnyHeader,
 63
 64        [Parameter(ParameterSetName = 'Items')]
 65        [switch]$AllowCredentials,
 66
 67        [Parameter(ParameterSetName = 'Items')]
 68        [switch]$DisallowCredentials,
 69
 70        [Parameter()]
 71        [switch]$PassThru
 72    )
 73    begin {
 74        # Ensure the server instance is resolved
 075        $Server = Resolve-KestrunServer -Server $Server
 076        if ($null -eq $Server) {
 077            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 78        }
 79    }
 80    process {
 081        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 82
 083            if ($AllowCredentials.IsPresent -and $DisallowCredentials.IsPresent) {
 084                throw 'Cannot specify both AllowCredentials and DisallowCredentials.'
 85            }
 86
 087            $Builder = [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]::new()
 088            if ($AllowAnyOrigin.IsPresent) {
 089                $Builder.AllowAnyOrigin() | Out-Null
 90            }
 091            if ($AllowAnyMethod.IsPresent) {
 092                $Builder.AllowAnyMethod() | Out-Null
 93            }
 094            if ($AllowAnyHeader.IsPresent) {
 095                $Builder.AllowAnyHeader() | Out-Null
 96            }
 097            if ($AllowCredentials.IsPresent) {
 098                $Builder.AllowCredentials() | Out-Null
 99            }
 0100            if ($DisallowCredentials.IsPresent) {
 0101                $Builder.DisallowCredentials() | Out-Null
 102            }
 103        }
 104
 0105        [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddCors($Server, $Name, $Builder) | Out-Null
 106        # Add the CORS policy to the server
 107
 0108        if ($PassThru.IsPresent) {
 109            # if the PassThru switch is specified, return the server instance
 110            # Return the modified server instance
 0111            return $Server
 112        }
 113    }
 114}
 115

Methods/Properties

Add-KrCorsPolicy()