< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Forms.KrFormEndpoints
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Forms/KrFormEndpoints.cs
Tag: Kestrun/Kestrun@d9261bd752e45afa789d10bc0c82b7d5724d9589
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 77
Line coverage: 100%
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: 100% (47/47) Total lines: 77 Tag: Kestrun/Kestrun@d9261bd752e45afa789d10bc0c82b7d5724d9589

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MapKestrunFormRoute(...)100%11100%
<MapKestrunFormRoute()100%66100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Forms/KrFormEndpoints.cs

#LineLine coverage
 1using Kestrun.Hosting;
 2using Kestrun.Models;
 3
 4namespace Kestrun.Forms;
 5
 6/// <summary>
 7/// Provides endpoint mapping helpers for form routes.
 8/// </summary>
 9public static class KrFormEndpoints
 10{
 11    /// <summary>
 12    /// Represents a delegate that handles a parsed form context.
 13    /// </summary>
 14    /// <param name="context">The form context.</param>
 15    /// <returns>A task representing the asynchronous operation.</returns>
 16    public delegate Task KrFormHandler(KrFormContext context);
 17
 18    /// <summary>
 19    /// Maps a form route that parses multipart and urlencoded payloads.
 20    /// </summary>
 21    /// <param name="endpoints">The endpoint route builder.</param>
 22    /// <param name="pattern">The route pattern.</param>
 23    /// <param name="options">The form parsing options.</param>
 24    /// <param name="handler">The handler to execute after parsing.</param>
 25    /// <returns>The endpoint convention builder.</returns>
 26    public static IEndpointConventionBuilder MapKestrunFormRoute(this IEndpointRouteBuilder endpoints, string pattern, K
 27    {
 428        ArgumentNullException.ThrowIfNull(endpoints);
 429        ArgumentNullException.ThrowIfNull(pattern);
 430        ArgumentNullException.ThrowIfNull(options);
 431        ArgumentNullException.ThrowIfNull(handler);
 32
 433        var map = endpoints.MapPost(pattern, async httpContext =>
 434        {
 435            var host = httpContext.RequestServices.GetRequiredService<KestrunHost>();
 436            var kestrunContext = new KestrunContext(host, httpContext);
 437            options.Logger ??= host.Logger;
 438
 439            try
 440            {
 441                var payload = await KrFormParser.ParseAsync(httpContext, options, httpContext.RequestAborted).ConfigureA
 342                var formContext = new KrFormContext(kestrunContext, options, payload);
 443
 344                httpContext.Items["KrFormContext"] = formContext;
 345                httpContext.Items["KrFormPayload"] = payload;
 446
 347                if (options.OnCompleted != null)
 448                {
 149                    var result = await options.OnCompleted(formContext).ConfigureAwait(false);
 150                    if (result != null)
 451                    {
 152                        await Results.Ok(result).ExecuteAsync(httpContext).ConfigureAwait(false);
 153                        return;
 454                    }
 455                }
 456
 257                await handler(formContext).ConfigureAwait(false);
 158                await kestrunContext.Response.ApplyTo(httpContext.Response).ConfigureAwait(false);
 159            }
 160            catch (KrFormException ex)
 461            {
 162                host.Logger.Error(ex, "Form parsing failed for {Pattern}", pattern);
 163                httpContext.Response.StatusCode = ex.StatusCode;
 164                await httpContext.Response.WriteAsync("Invalid form data.", httpContext.RequestAborted).ConfigureAwait(f
 165            }
 166            catch (Exception ex)
 467            {
 168                host.Logger.Error(ex, "Unhandled error in form route for {Pattern}", pattern);
 169                httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
 170                await httpContext.Response.WriteAsync("Internal server error.", httpContext.RequestAborted).ConfigureAwa
 471            }
 472        });
 73
 474        _ = map.DisableAntiforgery();
 475        return map;
 476    }
 77}