diff --git a/DevProxy.Integration.Tests/JsonReporterTests.cs b/DevProxy.Integration.Tests/JsonReporterTests.cs index 0fd6cb47..61c0311c 100644 --- a/DevProxy.Integration.Tests/JsonReporterTests.cs +++ b/DevProxy.Integration.Tests/JsonReporterTests.cs @@ -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; @@ -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); + } } diff --git a/DevProxy.Plugins/Reporting/UrlDiscoveryPluginReport.cs b/DevProxy.Plugins/Reporting/UrlDiscoveryPluginReport.cs index 1087dd01..ca86e40a 100644 --- a/DevProxy.Plugins/Reporting/UrlDiscoveryPluginReport.cs +++ b/DevProxy.Plugins/Reporting/UrlDiscoveryPluginReport.cs @@ -11,36 +11,12 @@ public sealed class UrlDiscoveryPluginReport : IJsonReport, IMarkdownReport, IPl { public required IEnumerable Data { get; init; } - public string FileExtension => ".jsonc"; + public string FileExtension => ".json"; - public object ToJson() + public object ToJson() => new Dictionary> { - 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() {