< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Hosting.KestrunHostStaticFilesExtensions
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Hosting/KestrunHostStaticFilesExtensions.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
55%
Covered lines: 53
Uncovered lines: 43
Coverable lines: 96
Total lines: 261
Line coverage: 55.2%
Branch coverage
41%
Covered branches: 14
Total branches: 34
Branch coverage: 41.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddDefaultFiles(...)75%4475%
AddDefaultFiles(...)50%22100%
AddDirectoryBrowser(...)100%210%
AddFavicon(...)100%11100%
CopyStaticFileOptions(...)0%2040%
CopyDefaultFilesOptions(...)0%4260%
AddFileServer(...)50%13642.85%
AddFileServer(...)50%4487.5%
AddStaticFiles(...)50%4487.5%
AddStaticFiles(...)75%4477.77%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Hosting/KestrunHostStaticFilesExtensions.cs

#LineLine coverage
 1
 2using Kestrun.Middleware;
 3using Serilog.Events;
 4
 5namespace Kestrun.Hosting;
 6
 7/// <summary>
 8/// Provides extension methods for configuring static file, default file, favicon, and file server middleware in Kestrun
 9/// </summary>
 10public static class KestrunHostStaticFilesExtensions
 11{
 12    /// <summary>
 13    /// Adds default files middleware to the application.
 14    /// This middleware serves default files like index.html when a directory is requested.
 15    /// </summary>
 16    /// <param name="host">The KestrunHost instance to configure.</param>
 17    /// <param name="cfg">Configuration options for the default files middleware.</param>
 18    /// <returns>The current KestrunHost instance.</returns>
 19    public static KestrunHost AddDefaultFiles(this KestrunHost host, DefaultFilesOptions? cfg)
 20    {
 221        if (host.HostLogger.IsEnabled(LogEventLevel.Debug))
 22        {
 023            host.HostLogger.Debug("Adding Default Files with configuration: {@Config}", cfg);
 24        }
 25
 226        if (cfg == null)
 27        {
 128            return host.AddDefaultFiles(); // no config, use defaults
 29        }
 30
 31        // Convert DefaultFilesOptions to an Action<DefaultFilesOptions>
 132        return host.AddDefaultFiles(options =>
 133        {
 034            CopyDefaultFilesOptions(cfg, options);
 135        });
 36    }
 37
 38    /// <summary>
 39    /// Adds default files middleware to the application.
 40    /// This middleware serves default files like index.html when a directory is requested.
 41    /// </summary>
 42    /// <param name="host">The KestrunHost instance to configure.</param>
 43    /// <param name="cfg">Configuration options for the default files middleware.</param>
 44    /// <returns>The current KestrunHost instance.</returns>
 45    public static KestrunHost AddDefaultFiles(this KestrunHost host, Action<DefaultFilesOptions>? cfg = null)
 46    {
 547        return host.Use(app =>
 548        {
 149            var options = new DefaultFilesOptions();
 150            cfg?.Invoke(options);
 151            _ = app.UseDefaultFiles(options);
 652        });
 53    }
 54
 55    /// <summary>
 56    /// Adds a directory browser middleware to the application.
 57    /// This middleware enables directory browsing for a specified request path.
 58    /// </summary>
 59    /// <param name="host">The KestrunHost instance to configure.</param>
 60    /// <param name="requestPath">The request path to enable directory browsing for.</param>
 61    /// <returns>The current KestrunHost instance.</returns>
 62    public static KestrunHost AddDirectoryBrowser(this KestrunHost host, string requestPath)
 63    {
 064        return host.Use(app =>
 065        {
 066            _ = app.UseDirectoryBrowser(requestPath);
 067        });
 68    }
 69
 70    /// <summary>
 71    /// Adds a favicon middleware to the application.
 72    /// </summary>
 73    /// <param name="host">The KestrunHost instance to configure.</param>
 74    /// <param name="iconPath">The path to the favicon file. If null, uses the default favicon.</param>
 75    /// <returns>The current KestrunHost instance.</returns>
 76    public static KestrunHost AddFavicon(this KestrunHost host, string? iconPath = null)
 77    {
 478        return host.Use(app =>
 479        {
 280            _ = app.UseFavicon(iconPath);
 681        });
 82    }
 83
 84
 85    /// <summary>
 86    /// Copies static file options from one object to another.
 87    /// </summary>
 88    /// <param name="src">The source static file options.</param>
 89    /// <param name="dest">The destination static file options.</param>
 90    /// <remarks>
 91    /// This method copies properties from the source static file options to the destination static file options.
 92    /// </remarks>
 93    private static void CopyStaticFileOptions(StaticFileOptions? src, StaticFileOptions dest)
 94    {
 95        // If no source, return a new empty options object
 096        if (src == null || dest == null)
 97        {
 098            return;
 99        }
 100        // Copy properties from source to destination
 0101        dest.ContentTypeProvider = src.ContentTypeProvider;
 0102        dest.OnPrepareResponse = src.OnPrepareResponse;
 0103        dest.ServeUnknownFileTypes = src.ServeUnknownFileTypes;
 0104        dest.DefaultContentType = src.DefaultContentType;
 0105        dest.FileProvider = src.FileProvider;
 0106        dest.RequestPath = src.RequestPath;
 0107        dest.RedirectToAppendTrailingSlash = src.RedirectToAppendTrailingSlash;
 0108        dest.HttpsCompression = src.HttpsCompression;
 0109    }
 110
 111    /// <summary>
 112    /// Copies default files options from one object to another.
 113    /// This method is used to ensure that the default files options are correctly configured.
 114    /// </summary>
 115    /// <param name="src">The source default files options.</param>
 116    /// <param name="dest">The destination default files options.</param>
 117    /// <remarks>
 118    /// This method copies properties from the source default files options to the destination default files options.
 119    /// </remarks>
 120    private static void CopyDefaultFilesOptions(DefaultFilesOptions? src, DefaultFilesOptions dest)
 121    {
 122        // If no source, return a new empty options object
 0123        if (src == null || dest == null)
 124        {
 0125            return;
 126        }
 127        // Copy properties from source to destination
 0128        dest.DefaultFileNames.Clear();
 0129        foreach (var name in src.DefaultFileNames)
 130        {
 0131            dest.DefaultFileNames.Add(name);
 132        }
 133
 0134        dest.FileProvider = src.FileProvider;
 0135        dest.RequestPath = src.RequestPath;
 0136        dest.RedirectToAppendTrailingSlash = src.RedirectToAppendTrailingSlash;
 0137    }
 138
 139    /// <summary>
 140    /// Adds a file server middleware to the application.
 141    /// </summary>
 142    /// <param name="host">The KestrunHost instance to configure.</param>
 143    /// <param name="cfg">Configuration options for the file server middleware.</param>
 144    /// <returns>The current KestrunHost instance.</returns>
 145    /// <remarks>
 146    /// This middleware serves static files and default files from a specified file provider.
 147    /// If no configuration is provided, it uses default settings.
 148    /// </remarks>
 149    public static KestrunHost AddFileServer(this KestrunHost host, FileServerOptions? cfg)
 150    {
 2151        if (host.HostLogger.IsEnabled(LogEventLevel.Debug))
 152        {
 0153            host.HostLogger.Debug("Adding File Server with configuration: {@Config}", cfg);
 154        }
 155
 2156        if (cfg == null)
 157        {
 1158            return host.AddFileServer(); // no config, use defaults
 159        }
 160
 161        // Convert FileServerOptions to an Action<FileServerOptions>
 1162        return host.AddFileServer(options =>
 1163        {
 0164            options.EnableDefaultFiles = cfg.EnableDefaultFiles;
 0165            options.EnableDirectoryBrowsing = cfg.EnableDirectoryBrowsing;
 0166            options.FileProvider = cfg.FileProvider;
 0167            options.RequestPath = cfg.RequestPath;
 0168            options.RedirectToAppendTrailingSlash = cfg.RedirectToAppendTrailingSlash;
 0169            CopyDefaultFilesOptions(cfg.DefaultFilesOptions, options.DefaultFilesOptions);
 0170            if (cfg.DirectoryBrowserOptions != null)
 1171            {
 0172                options.DirectoryBrowserOptions.FileProvider = cfg.DirectoryBrowserOptions.FileProvider;
 0173                options.DirectoryBrowserOptions.RequestPath = cfg.DirectoryBrowserOptions.RequestPath;
 0174                options.DirectoryBrowserOptions.RedirectToAppendTrailingSlash = cfg.DirectoryBrowserOptions.RedirectToAp
 1175            }
 1176
 0177            CopyStaticFileOptions(cfg.StaticFileOptions, options.StaticFileOptions);
 1178        });
 179    }
 180
 181    /// <summary>
 182    /// Adds a file server middleware to the application.
 183    /// This middleware serves static files and default files from a specified file provider.
 184    /// </summary>
 185    /// <param name="host">The KestrunHost instance to configure.</param>
 186    /// <param name="cfg">Configuration options for the file server middleware.</param>
 187    /// <returns>The current KestrunHost instance.</returns>
 188    public static KestrunHost AddFileServer(this KestrunHost host, Action<FileServerOptions>? cfg = null)
 189    {
 7190        if (host.HostLogger.IsEnabled(LogEventLevel.Debug))
 191        {
 0192            host.HostLogger.Debug("Adding File Server with configuration: {@Config}", cfg);
 193        }
 194
 7195        return host.Use(app =>
 7196        {
 2197            var options = new FileServerOptions();
 2198            cfg?.Invoke(options);
 2199            _ = app.UseFileServer(options);
 9200        });
 201    }
 202
 203
 204
 205    /// <summary>
 206    /// Adds static files to the application.
 207    /// This overload allows you to specify configuration options.
 208    /// </summary>
 209    /// <param name="host">The KestrunHost instance to configure.</param>
 210    /// <param name="cfg">The static file options to configure.</param>
 211    /// <returns>The current KestrunHost instance.</returns>
 212    public static KestrunHost AddStaticFiles(this KestrunHost host, Action<StaticFileOptions>? cfg = null)
 213    {
 8214        if (host.HostLogger.IsEnabled(LogEventLevel.Debug))
 215        {
 0216            host.HostLogger.Debug("Adding static files with configuration: {Config}", cfg);
 217        }
 218
 8219        return host.Use(app =>
 8220        {
 3221            if (cfg == null)
 8222            {
 0223                _ = app.UseStaticFiles();
 8224            }
 8225            else
 8226            {
 3227                var options = new StaticFileOptions();
 3228                cfg(options);
 8229
 3230                _ = app.UseStaticFiles(options);
 8231            }
 11232        });
 233    }
 234
 235    /// <summary>
 236    /// Adds static files to the application.
 237    /// This overload allows you to specify configuration options.
 238    /// </summary>
 239    /// <param name="host">The KestrunHost instance to configure.</param>
 240    /// <param name="options">The static file options to configure.</param>
 241    /// <returns>The current KestrunHost instance.</returns>
 242    public static KestrunHost AddStaticFiles(this KestrunHost host, StaticFileOptions options)
 243    {
 2244        if (host.HostLogger.IsEnabled(LogEventLevel.Debug))
 245        {
 0246            host.HostLogger.Debug("Adding static files with options: {@Options}", options);
 247        }
 248
 2249        if (options == null)
 250        {
 1251            return host.AddStaticFiles(); // no options, use defaults
 252        }
 253
 254        // reuse the delegate overload so the pipeline logic stays in one place
 1255        return host.AddStaticFiles(o =>
 1256        {
 1257            // copy only the properties callers are likely to set
 0258            CopyStaticFileOptions(options, o);
 1259        });
 260    }
 261}