< Summary - Kestrun — Combined Coverage

Information
Class: Public.Form.Add-KrFormOption
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Form/Add-KrFormOption.ps1
Tag: Kestrun/Kestrun@eeafbe813231ed23417e7b339e170e307b2c86f9
Line coverage
0%
Covered lines: 0
Uncovered lines: 55
Coverable lines: 55
Total lines: 227
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 02/05/2026 - 00:28:18 Line coverage: 0% (0/50) Total lines: 218 Tag: Kestrun/Kestrun@d9261bd752e45afa789d10bc0c82b7d5724d958902/18/2026 - 08:33:07 Line coverage: 0% (0/55) Total lines: 227 Tag: Kestrun/Kestrun@bf8a937cfb7e8936c225b9df4608f8ddd85558b1

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Form/Add-KrFormOption.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Adds a form option to the Kestrun server.
 4.DESCRIPTION
 5    This function creates and adds a form option to the Kestrun server's form options collection.
 6    It allows you to specify various parameters for the form option, such as name, description,
 7    allowed content types, upload path, and part rules.
 8.PARAMETER Name
 9    The name of the form option.
 10    If not provided, a unique name will be generated as "FormOption_{GUID}". Also used as the key
 11    when registering the form option in the server's form options collection.
 12    If not unique, the registration will fail and $null will be returned.
 13    If not provided, the form option will still be created and returned, but not registered.
 14.PARAMETER PartRules
 15    An array of form part rules associated with the form option.
 16    Can be provided via pipeline input.
 17.PARAMETER Description
 18    A description of the form option.
 19.PARAMETER AllowUnknownRequestContentType
 20    Indicates whether to allow unknown request content types.
 21.PARAMETER AllowedRequestContentTypes
 22    An array of allowed request content types for the form option.
 23.PARAMETER DefaultUploadPath
 24    The default upload path for files.
 25.PARAMETER ComputeSha256
 26    Indicates whether to compute the SHA-256 hash of uploaded files.
 27.PARAMETER EnablePartDecompression
 28    Indicates whether to enable decompression of parts.
 29.PARAMETER AllowedPartContentEncodings
 30    An array of allowed content encodings for parts.
 31.PARAMETER MaxDecompressedBytesPerPart
 32    The maximum number of decompressed bytes per part.
 33.PARAMETER RejectUnknownContentEncoding
 34    Indicates whether to reject unknown content encodings.
 35.PARAMETER Logger
 36    The logger to use for logging.
 37.PARAMETER MaxRequestBodyBytes
 38    The maximum size in bytes for the request body.
 39.PARAMETER MaxPartBodyBytes
 40    The maximum size in bytes for each part body.
 41.PARAMETER MaxParts
 42    The maximum number of parts allowed in the form.
 43.PARAMETER MaxHeaderBytesPerPart
 44    The maximum size in bytes for headers per part.
 45.PARAMETER MaxFieldValueBytes
 46    The maximum size in bytes for field values.
 47.PARAMETER MaxNestingDepth
 48    The maximum nesting depth for multipart forms.
 49.PARAMETER PassThru
 50    If specified, the cmdlet will return the created form option.
 51    If a Name is not provided, the created form option will always be returned.
 52.EXAMPLE
 53    Add-KrFormOption -Name 'fileUpload' -DefaultUploadPath 'C:\Uploads' -ComputeSha256 -PartRules $rules
 54    This example adds a form option named 'fileUpload' with a default upload path of 'C:\Uploads',
 55    enables SHA-256 computation, and associates the specified part rules.
 56.EXAMPLE
 57    New-KrFormPartRule -Name 'file' -Required -AllowedContentTypes 'text/plain'|
 58        Add-KrFormOption -Name 'textFileUpload' -PassThru
 59    This example creates a form part rule for a required text file and adds it to a new form option
 60    named 'textFileUpload'.
 61.NOTES
 62    This function is part of the Kestrun.Forms module and is used to define form options.
 63#>
 64function Add-KrFormOption {
 65    [KestrunRuntimeApi('Definition')]
 66    [CmdletBinding()]
 67    [OutputType([Kestrun.Forms.KrFormOptions])]
 68    param(
 69        [Parameter(ValueFromPipeline = $true)]
 70        [Kestrun.Forms.KrFormPartRule[]] $PartRules,
 71
 72        [Parameter()]
 73        [string] $Name,
 74
 75        [Parameter()]
 76        [string] $Description,
 77
 78        [Parameter()]
 79        [switch] $AllowUnknownRequestContentType,
 80
 81        [Parameter()]
 082        [string[]] $AllowedRequestContentTypes = @('multipart/form-data'),
 83
 84        [Parameter()]
 85        [string] $DefaultUploadPath,
 86
 87        [Parameter()]
 88        [switch] $ComputeSha256,
 89
 90        [Parameter()]
 91        [switch] $EnablePartDecompression,
 92
 93        [Parameter()]
 94        [string[]] $AllowedPartContentEncodings,
 95
 96        [Parameter()]
 97        [long] $MaxDecompressedBytesPerPart,
 98
 99        [Parameter()]
 100        [switch] $RejectUnknownContentEncoding,
 101
 102        [Parameter()]
 103        [Serilog.ILogger] $Logger,
 104
 105        # Limits (optional)
 106        [Parameter()]
 107        [long] $MaxRequestBodyBytes,
 108
 109        [Parameter()]
 110        [long] $MaxPartBodyBytes ,
 111
 112        [Parameter()]
 113        [int] $MaxParts,
 114
 115        [Parameter()]
 116        [int] $MaxHeaderBytesPerPart,
 117
 118        [Parameter()]
 119        [long] $MaxFieldValueBytes,
 120
 121        [Parameter()]
 122        [int] $MaxNestingDepth,
 123
 124        [Parameter()]
 125        [switch] $PassThru
 126
 127    )
 128    begin {
 0129        $Server = Resolve-KestrunServer
 130
 0131        $Options = [Kestrun.Forms.KrFormOptions]::new()
 132
 0133        if ($PSBoundParameters.ContainsKey('Description')) {
 0134            $Options.Description = $Description
 135        }
 0136        if ($PSBoundParameters.ContainsKey('AllowedRequestContentTypes')) {
 137            # The underlying C# property is KrFormOptions.AllowedContentTypes.
 138            # Keep backward compatibility if an older assembly exposes AllowedRequestContentTypes.
 0139            if ($Options.PSObject.Properties.Match('AllowedContentTypes').Count -gt 0) {
 0140                $Options.AllowedContentTypes.Clear()
 0141                $Options.AllowedContentTypes.AddRange($AllowedRequestContentTypes)
 0142            } elseif ($Options.PSObject.Properties.Match('AllowedRequestContentTypes').Count -gt 0) {
 0143                $Options.AllowedRequestContentTypes.Clear()
 0144                $Options.AllowedRequestContentTypes.AddRange($AllowedRequestContentTypes)
 145            } else {
 0146                throw 'KrFormOptions does not expose AllowedContentTypes/AllowedRequestContentTypes. Ensure Kestrun modu
 147            }
 148        }
 0149        if ($PSBoundParameters.ContainsKey('AllowUnknownRequestContentType')) {
 0150            $Options.RejectUnknownRequestContentType = -not $AllowUnknownRequestContentType.IsPresent
 151        }
 0152        if ($PSBoundParameters.ContainsKey('DefaultUploadPath')) {
 0153            $Options.DefaultUploadPath = $DefaultUploadPath
 154        }
 0155        if ($PSBoundParameters.ContainsKey('ComputeSha256')) {
 0156            $Options.ComputeSha256 = $ComputeSha256.IsPresent
 157        }
 0158        if ($PSBoundParameters.ContainsKey('EnablePartDecompression')) {
 0159            $Options.EnablePartDecompression = $EnablePartDecompression.IsPresent
 160        }
 0161        if ($PSBoundParameters.ContainsKey('AllowedPartContentEncodings')) {
 0162            $Options.AllowedPartContentEncodings.Clear()
 0163            $Options.AllowedPartContentEncodings.AddRange($AllowedPartContentEncodings)
 164        }
 0165        if ($PSBoundParameters.ContainsKey('MaxDecompressedBytesPerPart')) {
 0166            $Options.MaxDecompressedBytesPerPart = $MaxDecompressedBytesPerPart
 167        }
 0168        if ($PSBoundParameters.ContainsKey('RejectUnknownContentEncoding')) {
 0169            $Options.RejectUnknownContentEncoding = $RejectUnknownContentEncoding.IsPresent
 170        }
 171
 0172        if ($PSBoundParameters.ContainsKey('Logger')) {
 0173            $Options.Logger = $Logger
 174        } else {
 0175            $Options.Logger = $Server.Logger
 176        }
 177
 178        # Limits
 0179        if ($PSBoundParameters.ContainsKey('MaxRequestBodyBytes')) {
 0180            $Options.Limits.MaxRequestBodyBytes = $MaxRequestBodyBytes
 181        }
 0182        if ($PSBoundParameters.ContainsKey('MaxPartBodyBytes')) {
 0183            $Options.Limits.MaxPartBodyBytes = $MaxPartBodyBytes
 184        }
 0185        if ($PSBoundParameters.ContainsKey('MaxParts')) {
 0186            $Options.Limits.MaxParts = $MaxParts
 187        }
 0188        if ($PSBoundParameters.ContainsKey('MaxHeaderBytesPerPart')) {
 0189            $Options.Limits.MaxHeaderBytesPerPart = $MaxHeaderBytesPerPart
 190        }
 0191        if ($PSBoundParameters.ContainsKey('MaxFieldValueBytes')) {
 0192            $Options.Limits.MaxFieldValueBytes = $MaxFieldValueBytes
 193        }
 0194        if ($PSBoundParameters.ContainsKey('MaxNestingDepth')) {
 0195            $Options.Limits.MaxNestingDepth = $MaxNestingDepth
 196        }
 197
 198        # Register the option
 0199        if ($PSBoundParameters.ContainsKey('Name')) {
 0200            $Options.Name = $Name
 201            # Register the option in the server's form options collection
 0202            if (-not $Server.AddFormOption($Options)) {
 0203                return $null
 204            }
 205            # Return the created options if PassThru is specified
 0206            if (-not $PassThru.IsPresent) {
 207                return
 208            }
 209        } else {
 210            # Generate a unique name if not provided
 0211            $Options.Name = "FormOption_$([System.Guid]::NewGuid().ToString())"
 212        }
 213    }
 214    process {
 0215        if ($PSBoundParameters.ContainsKey('PartRules')) {
 0216            $Options.Rules.Clear()
 0217            foreach ($rule in $PartRules) {
 0218                $Options.Rules.Add($rule)
 219            }
 220        }
 221    }
 222    end {
 0223        if ( $PassThru.IsPresent -or -not $PSBoundParameters.ContainsKey('Name')) {
 0224            return $Options
 225        }
 226    }
 227}

Methods/Properties

Add-KrFormOption()