Skip to content
Open
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
30 changes: 29 additions & 1 deletion DevProxy.Integration.Tests/JsonReporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// 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.Plugins;
using DevProxy.Abstractions.Proxy;
using DevProxy.Plugins.Reporters;
using DevProxy.Plugins.Reporting;
using Microsoft.Extensions.Logging.Abstractions;
using System.Text.Json;
using Xunit;

namespace DevProxy.Integration.Tests;
Expand All @@ -17,15 +19,41 @@ public sealed class JsonReporterTests
public string? Report(string key, object value) => GetReport(new(key, value));
}

private sealed class TestJsonReport : IJsonReport
{
public string FileExtension => ".jsonc";
public object ToJson() => new { Value = 1 };
}

[Fact]
public void FileExtension_IsDeterminedPerReport()
{
var reporter = new TestJsonReporter();

_ = reporter.Report("UrlDiscoveryPlugin", new UrlDiscoveryPluginReport { Data = ["https://api.contoso.com/*"] });
_ = reporter.Report("CustomPlugin", new TestJsonReport());
Assert.Equal(".jsonc", reporter.FileExtension);

_ = reporter.Report("OtherPlugin", new { Value = 1 });
Assert.Equal(".json", reporter.FileExtension);
}

[Fact]
public void UrlDiscoveryReport_IsStrictJson()
{
var reporter = new TestJsonReporter();

var json = reporter.Report("UrlDiscoveryPlugin", new UrlDiscoveryPluginReport
{
Data = ["https://api.contoso.com/orders", "https://api.contoso.com/users"]
});

Assert.Equal(".json", reporter.FileExtension);
Assert.NotNull(json);
using var doc = JsonDocument.Parse(json);
var urls = doc.RootElement.GetProperty("urlsToWatch")
.EnumerateArray()
.Select(e => e.GetString()!)
.ToArray();
Assert.Equal(["https://api.contoso.com/orders", "https://api.contoso.com/users"], urls);
}
}
32 changes: 4 additions & 28 deletions DevProxy.Plugins/Reporting/UrlDiscoveryPluginReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,12 @@ public sealed class UrlDiscoveryPluginReport : IJsonReport, IMarkdownReport, IPl
{
public required IEnumerable<string> Data { get; init; }

public string FileExtension => ".jsonc";
public string FileExtension => ".json";

public object ToJson()
public object ToJson() => new Dictionary<string, IEnumerable<string>>
{
var sb = new StringBuilder();
_ = sb.AppendLine("{")
.AppendLine(" // Wildcards")
.AppendLine(" // ")
.AppendLine(" // You can use wildcards to catch multiple URLs with the same pattern.")
.AppendLine(" // For example, you can use the following URL pattern to catch all API requests to")
.AppendLine(" // JSON Placeholder API:")
.AppendLine(" // ")
.AppendLine(" // https://jsonplaceholder.typicode.com/*")
.AppendLine(" // ")
.AppendLine(" // Excluding URLs")
.AppendLine(" // ")
.AppendLine(" // You can exclude URLs with ! to prevent them from being intercepted.")
.AppendLine(" // For example, you can exclude the URL https://jsonplaceholder.typicode.com/authors")
.AppendLine(" // by using the following URL pattern:")
.AppendLine(" // ")
.AppendLine(" // !https://jsonplaceholder.typicode.com/authors")
.AppendLine(" // https://jsonplaceholder.typicode.com/*")
.AppendLine(" \"urlsToWatch\": [")
.AppendJoin($",{Environment.NewLine}", Data.Select(u => $" \"{u}\""))
.AppendLine("")
.AppendLine(" ]")
.AppendLine("}");

return sb.ToString();
}
["urlsToWatch"] = Data
};

public string? ToMarkdown()
{
Expand Down
Loading