< Summary - Kestrun — Combined Coverage

Information
Class: Public.Service.Add-KrResponseCompression
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Service/Add-KrResponseCompression.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
78%
Covered lines: 11
Uncovered lines: 3
Coverable lines: 14
Total lines: 85
Line coverage: 78.5%
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-KrResponseCompression.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds response compression to the server.
 4    .DESCRIPTION
 5        This cmdlet allows you to configure response compression for the Kestrun server.
 6        It can be used to compress responses using various algorithms like Gzip, Brotli, etc.
 7    .PARAMETER Server
 8        The Kestrun server instance to which the response compression will be added.
 9    .PARAMETER Options
 10        The ResponseCompressionOptions to configure the response compression.
 11    .PARAMETER EnableForHttps
 12        If specified, enables response compression for HTTPS requests.
 13    .PARAMETER MimeTypes
 14        An array of MIME types to compress. If not specified, defaults to common text-based MIME types.
 15    .PARAMETER ExcludedMimeTypes
 16        An array of MIME types to exclude from compression.
 17    .PARAMETER PassThru
 18        If specified, the cmdlet will return the modified server instance.
 19    .EXAMPLE
 20        $server | Add-KrResponseCompression -EnableForHttps -MimeTypes 'text/plain', 'application/json' -ExcludedMimeTyp
 21        This example adds response compression to the server, enabling it for HTTPS requests, and specifying the MIME ty
 22    .EXAMPLE
 23        $server | Add-KrResponseCompression -Options $options
 24        This example adds response compression to the server using the specified ResponseCompressionOptions.
 25    .LINK
 26        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.responsecompression.responsecompressionoptions
 27    .NOTES
 28        This cmdlet is used to configure response compression for the Kestrun server, allowing you to specify which MIME
 29        Providers is not supported yet.
 30#>
 31function Add-KrResponseCompression {
 32    [KestrunRuntimeApi('Definition')]
 33    [CmdletBinding(defaultParameterSetName = 'Items')]
 34    [OutputType([Kestrun.Hosting.KestrunHost])]
 35    param(
 36        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 37        [Kestrun.Hosting.KestrunHost]$Server,
 38        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 39        [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]$Options,
 40        [Parameter(ParameterSetName = 'Items')]
 41        [switch]$EnableForHttps,
 42        [Parameter(ParameterSetName = 'Items')]
 43        [string[]]$MimeTypes,
 44        [Parameter(ParameterSetName = 'Items')]
 45        [string[]]$ExcludedMimeTypes,
 46        [Parameter()]
 47        [switch]$PassThru
 48    )
 49    begin {
 50        # Ensure the server instance is resolved
 151        $Server = Resolve-KestrunServer -Server $Server
 152        if ($null -eq $Server) {
 053            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 54        }
 55    }
 56    process {
 157        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 158            $Options = [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]::new()
 159            if ($null -ne $MimeTypes -and $MimeTypes.Count -gt 0) {
 160                $Options.MimeTypes = $MimeTypes
 61            }
 162            if ($null -ne $ExcludedMimeTypes -and $ExcludedMimeTypes.Count -gt 0) {
 063                $Options.ExcludedMimeTypes = $ExcludedMimeTypes
 64            }
 165            if ($EnableForHttps.IsPresent) {
 166                $Options.EnableForHttps = $true
 67            }
 68            # Providers are not supported yet
 69            <# if ($null -ne $Providers -and $Providers.Count -gt 0) {
 70                foreach ($Provider in $Providers) {
 71                    $Options.Providers.Add($Provider)
 72                }
 73            }#>
 74        }
 75
 276        [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddResponseCompression($Server, $Options) | Out-Null
 77
 178        if ($PassThru.IsPresent) {
 79            # if the PassThru switch is specified, return the server instance
 80            # Return the modified server instance
 081            return $Server
 82        }
 83    }
 84}
 85

Methods/Properties

Add-KrResponseCompression()