< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrCompressionMiddleware
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCompressionMiddleware.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 93
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 09/13/2025 - 17:19:56 Line coverage: 78.5% (11/14) Total lines: 85 Tag: Kestrun/Kestrun@ea635f1ee1937c260a89d1a43a3c203cd8767c7b10/13/2025 - 16:52:37 Line coverage: 0% (0/20) Total lines: 93 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCompressionMiddleware.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 DisableGzip
 18        If specified, disables Gzip compression.
 19    .PARAMETER DisableBrotli
 20        If specified, disables Brotli compression.
 21    .PARAMETER PassThru
 22        If specified, the cmdlet will return the modified server instance.
 23    .EXAMPLE
 24        $server | Add-KrCompressionMiddleware -EnableForHttps -MimeTypes 'text/plain', 'application/json' -ExcludedMimeT
 25        This example adds response compression to the server, enabling it for HTTPS requests, and specifying the MIME ty
 26    .EXAMPLE
 27        $server | Add-KrCompressionMiddleware -Options $options
 28        This example adds response compression to the server using the specified ResponseCompressionOptions.
 29    .LINK
 30        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.responsecompression.responsecompressionoptions
 31    .NOTES
 32        This cmdlet is used to configure response compression for the Kestrun server, allowing you to specify which MIME
 33        Providers is not supported yet.
 34#>
 35function Add-KrCompressionMiddleware {
 36    [KestrunRuntimeApi('Definition')]
 37    [CmdletBinding(defaultParameterSetName = 'Items')]
 38    [OutputType([Kestrun.Hosting.KestrunHost])]
 39    param(
 40        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 41        [Kestrun.Hosting.KestrunHost]$Server,
 42        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 43        [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]$Options,
 44        [Parameter(ParameterSetName = 'Items')]
 45        [switch]$EnableForHttps,
 46        [Parameter(ParameterSetName = 'Items')]
 47        [string[]]$MimeTypes,
 48        [Parameter(ParameterSetName = 'Items')]
 49        [string[]]$ExcludedMimeTypes,
 50        [Parameter(ParameterSetName = 'Items')]
 51        [switch]$DisableGzip,
 52        [Parameter(ParameterSetName = 'Items')]
 53        [switch]$DisableBrotli,
 54        [Parameter()]
 55        [switch]$PassThru
 56    )
 57    begin {
 58        # Ensure the server instance is resolved
 059        $Server = Resolve-KestrunServer -Server $Server
 60    }
 61    process {
 062        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 063            $Options = [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]::new()
 064            if ($null -ne $MimeTypes -and $MimeTypes.Count -gt 0) {
 065                $Options.MimeTypes = $MimeTypes
 66            }
 067            if ($null -ne $ExcludedMimeTypes -and $ExcludedMimeTypes.Count -gt 0) {
 068                $Options.ExcludedMimeTypes = $ExcludedMimeTypes
 69            }
 070            if ($EnableForHttps.IsPresent) {
 071                $Options.EnableForHttps = $true
 72            }
 073            if (-not $DisableGzip.IsPresent) {
 074                $gzipCompressionProviderOptions = [Microsoft.AspNetCore.ResponseCompression.GzipCompressionProviderOptio
 075                $gzipOptionsWrapper = [Microsoft.Extensions.Options.Options]::Create($gzipCompressionProviderOptions)
 076                $Options.Providers.Add([Microsoft.AspNetCore.ResponseCompression.GzipCompressionProvider]::new($gzipOpti
 077                if (-not $DisableBrotli.IsPresent) {
 078                    $brotliCompressionProviderOptions = [Microsoft.AspNetCore.ResponseCompression.BrotliCompressionProvi
 079                    $brotliOptionsWrapper = [Microsoft.Extensions.Options.Options]::Create($brotliCompressionProviderOpt
 080                    $Options.Providers.Add([Microsoft.AspNetCore.ResponseCompression.BrotliCompressionProvider]::new($br
 81                }
 82            }
 83        }
 84
 085        [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddResponseCompression($Server, $Options) | Out-Null
 86
 087        if ($PassThru.IsPresent) {
 88            # if the PassThru switch is specified, return the modified server instance
 089            return $Server
 90        }
 91    }
 92}
 93

Methods/Properties

Add-KrCompressionMiddleware()