< 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@d9261bd752e45afa789d10bc0c82b7d5724d9589
Line coverage
0%
Covered lines: 0
Uncovered lines: 50
Coverable lines: 50
Total lines: 218
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@d9261bd752e45afa789d10bc0c82b7d5724d9589

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')) {
 0137            $Options.AllowedRequestContentTypes.Clear()
 0138            $Options.AllowedRequestContentTypes.AddRange($AllowedRequestContentTypes)
 139        }
 0140        if ($PSBoundParameters.ContainsKey('AllowUnknownRequestContentType')) {
 0141            $Options.RejectUnknownRequestContentType = -not $AllowUnknownRequestContentType.IsPresent
 142        }
 0143        if ($PSBoundParameters.ContainsKey('DefaultUploadPath')) {
 0144            $Options.DefaultUploadPath = $DefaultUploadPath
 145        }
 0146        if ($PSBoundParameters.ContainsKey('ComputeSha256')) {
 0147            $Options.ComputeSha256 = $ComputeSha256.IsPresent
 148        }
 0149        if ($PSBoundParameters.ContainsKey('EnablePartDecompression')) {
 0150            $Options.EnablePartDecompression = $EnablePartDecompression.IsPresent
 151        }
 0152        if ($PSBoundParameters.ContainsKey('AllowedPartContentEncodings')) {
 0153            $Options.AllowedPartContentEncodings.Clear()
 0154            $Options.AllowedPartContentEncodings.AddRange($AllowedPartContentEncodings)
 155        }
 0156        if ($PSBoundParameters.ContainsKey('MaxDecompressedBytesPerPart')) {
 0157            $Options.MaxDecompressedBytesPerPart = $MaxDecompressedBytesPerPart
 158        }
 0159        if ($PSBoundParameters.ContainsKey('RejectUnknownContentEncoding')) {
 0160            $Options.RejectUnknownContentEncoding = $RejectUnknownContentEncoding.IsPresent
 161        }
 162
 0163        if ($PSBoundParameters.ContainsKey('Logger')) {
 0164            $Options.Logger = $Logger
 165        } else {
 0166            $Options.Logger = $Server.Logger
 167        }
 168
 169        # Limits
 0170        if ($PSBoundParameters.ContainsKey('MaxRequestBodyBytes')) {
 0171            $Options.Limits.MaxRequestBodyBytes = $MaxRequestBodyBytes
 172        }
 0173        if ($PSBoundParameters.ContainsKey('MaxPartBodyBytes')) {
 0174            $Options.Limits.MaxPartBodyBytes = $MaxPartBodyBytes
 175        }
 0176        if ($PSBoundParameters.ContainsKey('MaxParts')) {
 0177            $Options.Limits.MaxParts = $MaxParts
 178        }
 0179        if ($PSBoundParameters.ContainsKey('MaxHeaderBytesPerPart')) {
 0180            $Options.Limits.MaxHeaderBytesPerPart = $MaxHeaderBytesPerPart
 181        }
 0182        if ($PSBoundParameters.ContainsKey('MaxFieldValueBytes')) {
 0183            $Options.Limits.MaxFieldValueBytes = $MaxFieldValueBytes
 184        }
 0185        if ($PSBoundParameters.ContainsKey('MaxNestingDepth')) {
 0186            $Options.Limits.MaxNestingDepth = $MaxNestingDepth
 187        }
 188
 189        # Register the option
 0190        if ($PSBoundParameters.ContainsKey('Name')) {
 0191            $Options.Name = $Name
 192            # Register the option in the server's form options collection
 0193            if (-not $Server.AddFormOption($Options)) {
 0194                return $null
 195            }
 196            # Return the created options if PassThru is specified
 0197            if (-not $PassThru.IsPresent) {
 198                return
 199            }
 200        } else {
 201            # Generate a unique name if not provided
 0202            $Options.Name = "FormOption_$([System.Guid]::NewGuid().ToString())"
 203        }
 204    }
 205    process {
 0206        if ($PSBoundParameters.ContainsKey('PartRules')) {
 0207            $Options.Rules.Clear()
 0208            foreach ($rule in $PartRules) {
 0209                $Options.Rules.Add($rule)
 210            }
 211        }
 212    }
 213    end {
 0214        if ( $PassThru.IsPresent -or -not $PSBoundParameters.ContainsKey('Name')) {
 0215            return $Options
 216        }
 217    }
 218}

Methods/Properties

Add-KrFormOption()