diff --git a/DevProxy.Integration.Tests/JsonReporterTests.cs b/DevProxy.Integration.Tests/JsonReporterTests.cs new file mode 100644 index 000000000..0fd6cb47f --- /dev/null +++ b/DevProxy.Integration.Tests/JsonReporterTests.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using DevProxy.Abstractions.Proxy; +using DevProxy.Plugins.Reporters; +using DevProxy.Plugins.Reporting; +using Microsoft.Extensions.Logging.Abstractions; +using Xunit; + +namespace DevProxy.Integration.Tests; + +public sealed class JsonReporterTests +{ + private sealed class TestJsonReporter() : JsonReporter(NullLogger.Instance, new HashSet()) + { + public string? Report(string key, object value) => GetReport(new(key, value)); + } + + [Fact] + public void FileExtension_IsDeterminedPerReport() + { + var reporter = new TestJsonReporter(); + + _ = reporter.Report("UrlDiscoveryPlugin", new UrlDiscoveryPluginReport { Data = ["https://api.contoso.com/*"] }); + Assert.Equal(".jsonc", reporter.FileExtension); + + _ = reporter.Report("OtherPlugin", new { Value = 1 }); + Assert.Equal(".json", reporter.FileExtension); + } +} diff --git a/DevProxy.Plugins/Reporters/JsonReporter.cs b/DevProxy.Plugins/Reporters/JsonReporter.cs index 716053f13..05fc0d805 100644 --- a/DevProxy.Plugins/Reporters/JsonReporter.cs +++ b/DevProxy.Plugins/Reporters/JsonReporter.cs @@ -14,7 +14,8 @@ public class JsonReporter( ILogger logger, ISet urlsToWatch) : BaseReporter(logger, urlsToWatch) { - private string _fileExtension = ".json"; + private const string DefaultFileExtension = ".json"; + private string _fileExtension = DefaultFileExtension; public override string Name => nameof(JsonReporter); public override string FileExtension => _fileExtension; @@ -23,6 +24,9 @@ protected override string GetReport(KeyValuePair report) { Logger.LogDebug("Serializing report {ReportKey}...", report.Key); + // Determine the extension per report so that one report's extension doesn't leak into the next + _fileExtension = DefaultFileExtension; + var reportData = report.Value; if (reportData is IJsonReport jsonReport) {