< Summary - Kestrun — Combined Coverage

Information
Class: Public.Response.Add-KrCacheResponse
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Add-KrCacheResponse.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 106
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: 0% (0/17) Total lines: 102 Tag: Kestrun/Kestrun@ea635f1ee1937c260a89d1a43a3c203cd8767c7b10/13/2025 - 16:52:37 Line coverage: 0% (0/17) Total lines: 106 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Add-KrCacheResponse.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds caching headers to the HTTP response.
 4    .DESCRIPTION
 5        This cmdlet allows you to add caching headers to the HTTP response in a route script block.
 6        It provides various parameters to customize the caching behavior, such as setting max-age,
 7        no-cache, no-store, and other cache control directives.
 8    .PARAMETER NoCache
 9        If specified, the 'no-cache' directive will be added to the Cache-Control header.
 10    .PARAMETER NoStore
 11        If specified, the 'no-store' directive will be added to the Cache-Control header.
 12    .PARAMETER MaxAge
 13        If specified, sets the 'max-age' directive in seconds for the Cache-Control header.
 14    .PARAMETER SharedMaxAge
 15        If specified, sets the 's-maxage' directive in seconds for the Cache-Control header
 16        (used by shared caches).
 17    .PARAMETER MaxStale
 18        If specified, the 'max-stale' directive will be added to the Cache-Control header.
 19    .PARAMETER MaxStaleLimit
 20        If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header.
 21    .PARAMETER MinFresh
 22        If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header.
 23    .PARAMETER NoTransform
 24        If specified, the 'no-transform' directive will be added to the Cache-Control header.
 25    .PARAMETER OnlyIfCached
 26        If specified, the 'only-if-cached' directive will be added to the Cache-Control header.
 27    .PARAMETER Public
 28        If specified, the 'public' directive will be added to the Cache-Control header.
 29    .PARAMETER Private
 30        If specified, the 'private' directive will be added to the Cache-Control header.
 31    .PARAMETER MustRevalidate
 32        If specified, the 'must-revalidate' directive will be added to the Cache-Control header.
 33    .PARAMETER ProxyRevalidate
 34        If specified, the 'proxy-revalidate' directive will be added to the Cache-Control header.
 35    .EXAMPLE
 36         Add-KrCacheResponse  -NoCache -MaxAge 3600 -Public
 37        This example adds caching headers to the response, setting the 'no-cache' directive,
 38        a 'max-age' of 3600 seconds, and marking the response as 'public'.
 39    .EXAMPLE
 40        Add-KrCacheResponse -NoStore -Private -MustRevalidate
 41        This example adds caching headers to the response, setting the 'no-store' directive,
 42        marking the response as 'private', and adding the 'must-revalidate' directive.
 43    .NOTES
 44        This cmdlet is used to add caching headers to the response in a route script block,
 45        allowing you to control how responses are cached by clients and intermediate caches.
 46        It must be used within a route script block where the $Context variable is available.
 47#>
 48function Add-KrCacheResponse {
 49    [KestrunRuntimeApi('Route')]
 50    [CmdletBinding()]
 51    param(
 52        [Parameter()]
 53        [switch]$NoCache,
 54        [Parameter()]
 55        [switch]$NoStore,
 56        [Parameter()]
 57        [ValidateRange(1, [int]::MaxValue)]
 58        [int]$MaxAge,
 59        [Parameter()]
 60        [ValidateRange(1, [int]::MaxValue)]
 61        [int]$SharedMaxAge,
 62        [Parameter()]
 63        [switch]$MaxStale,
 64        [Parameter()]
 65        [ValidateRange(1, [int]::MaxValue)]
 66        [int]$MaxStaleLimit,
 67        [Parameter()]
 68        [ValidateRange(1, [int]::MaxValue)]
 69        [int]$MinFresh,
 70        [Parameter()]
 71        [switch]$NoTransform,
 72        [Parameter()]
 73        [switch]$OnlyIfCached,
 74        [Parameter()]
 75        [switch]$Public,
 76        [Parameter()]
 77        [switch]$Private,
 78        [Parameter()]
 79        [switch]$MustRevalidate,
 80        [Parameter()]
 81        [switch]$ProxyRevalidate
 82    )
 83    # Only works inside a route script block where $Context is available
 084    if ($null -ne $Context.Response) {
 85        # Define default cache control headers if not provided
 086        $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new();
 087        if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent }
 088        if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent }
 089        if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) }
 090        if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($Shar
 091        if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent }
 092        if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds($Ma
 093        if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh) }
 094        if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent }
 095        if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent }
 096        if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent }
 097        if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent }
 098        if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPresent
 099        if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.IsPres
 100
 101        # Apply the cache control headers to the response
 0102        $Context.Response.CacheControl = $cacheControl
 103    } else {
 0104        Write-KrOutsideRouteWarning
 105    }
 106}

Methods/Properties

Add-KrCacheResponse()