Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,31 @@ public void Custom_options_are_registered_and_used()
Assert.Equal("***", options.RedactionText);
Assert.NotNull(store.Environment);
}
[Fact]
public void MaxEntries_zero_throws_InvalidOperationException()
{
var services = new ServiceCollection();

var exception = Assert.Throws<InvalidOperationException>(() =>
services.AddDebugProbe(options =>
{
options.MaxEntries = 0;
}));

Assert.Contains("MaxEntries", exception.Message);
}

[Fact]
public void MaxEntries_negative_throws_InvalidOperationException()
{
var services = new ServiceCollection();

var exception = Assert.Throws<InvalidOperationException>(() =>
services.AddDebugProbe(options =>
{
options.MaxEntries = -1;
}));

Assert.Contains("MaxEntries", exception.Message);
}
}
5 changes: 5 additions & 0 deletions DebugProbe.AspNetCore/Extensions/DebugProbeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public static IServiceCollection AddDebugProbe(this IServiceCollection services,

configure?.Invoke(options);

var validator = new DebugProbeOptionsValidator();
var result = validator.Validate(null, options);
if (result.Failed)
throw new InvalidOperationException(result.FailureMessage);

services.AddSingleton(options);

services.AddSingleton<DebugEntryStore>();
Expand Down
22 changes: 22 additions & 0 deletions DebugProbe.AspNetCore/Options/DebugProbeOptionsValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.Options;

namespace DebugProbe.AspNetCore.Options;

internal sealed class DebugProbeOptionsValidator
: IValidateOptions<DebugProbeOptions>
{
public ValidateOptionsResult Validate(
string? name,
DebugProbeOptions options)
{
if (options.MaxEntries < 1)
{
return ValidateOptionsResult.Fail(
$"DebugProbe configuration is invalid. " +
$"MaxEntries must be greater than or equal to 1. " +
$"Provided value: {options.MaxEntries}.");
}

return ValidateOptionsResult.Success;
}
}
Loading