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
31 changes: 31 additions & 0 deletions DevProxy.Integration.Tests/JsonReporterTests.cs
Original file line number Diff line number Diff line change
@@ -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<JsonReporter>.Instance, new HashSet<UrlToWatch>())
{
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);
}
}
6 changes: 5 additions & 1 deletion DevProxy.Plugins/Reporters/JsonReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class JsonReporter(
ILogger<JsonReporter> logger,
ISet<UrlToWatch> 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;
Expand All @@ -23,6 +24,9 @@ protected override string GetReport(KeyValuePair<string, object> 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)
{
Expand Down
Loading