diff --git a/src/ReportingApi.Tests/ReportRequestConverterTests.cs b/src/ReportingApi.Tests/ReportRequestConverterTests.cs index 55a7740..962196c 100644 --- a/src/ReportingApi.Tests/ReportRequestConverterTests.cs +++ b/src/ReportingApi.Tests/ReportRequestConverterTests.cs @@ -1,4 +1,5 @@ using ReportingApi.Models; +using System.Collections.Generic; using System.Text.Json; using Xunit; @@ -6,66 +7,75 @@ namespace ReportingApi.Tests; public class ReportRequestConverterTests { - [Fact] - public void CspViolationTest() + [Theory] + [MemberData(nameof(JsonSerializerOptionsData))] + public void CspViolationTest(JsonSerializerOptions jsonSerializerOptions) { - var sourceJson = """ - { - "age":1, - "body": { - "blockedURL":"https://csplite.com/tst/media/7_del.png", - "disposition":"enforce", - "documentURL":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", - "effectiveDirective":"img-src", - "lineNumber":9, - "originalPolicy":"default-src 'none'; report-to endpoint-csp;", - "referrer":"https://csplite.com/test229/", - "sourceFile":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", - "statusCode":0 - }, - "type":"csp-violation", - "url":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", - "user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36" - } - """; + const string sourceJson = + // language=json + """ + { + "age":1, + "body": { + "blockedURL":"https://csplite.com/tst/media/7_del.png", + "disposition":"enforce", + "documentURL":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", + "effectiveDirective":"img-src", + "lineNumber":9, + "originalPolicy":"default-src 'none'; report-to endpoint-csp;", + "referrer":"https://csplite.com/test229/", + "sourceFile":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", + "statusCode":0 + }, + "type":"csp-violation", + "url":"https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", + "user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36" + } + """; - var reportRequest = JsonSerializer.Deserialize(sourceJson); + var reportRequest = JsonSerializer.Deserialize(sourceJson, jsonSerializerOptions); Assert.NotNull(reportRequest); Assert.Equal(1, reportRequest.Age); - Assert.Equal("https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", - reportRequest.Url); + Assert.Equal( + "https://csplite.com/tst/test_frame.php?ID=229&hash=da964209653e467d337313e51876e27d", + reportRequest.Url + ); Assert.Equal( "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36", - reportRequest.UserAgent); + reportRequest.UserAgent + ); var cspReport = Assert.IsType>(reportRequest); Assert.Equal("csp-violation", cspReport.Type); Assert.NotNull(cspReport.Body); } - [Fact] - public void NetworkErrorTest() + [Theory] + [MemberData(nameof(JsonSerializerOptionsData))] + public void NetworkErrorTest(JsonSerializerOptions jsonSerializerOptions) { - var sourceJson = """ - { - "age": 2, - "type": "network-error", - "url": "https://widget.com/thing.js", - "body": { - "sampling_fraction": 1.0, - "referrer": "https://www.example.com/", - "server_ip": "", - "protocol": "", - "method": "GET", - "status_code": 0, - "elapsed_time": 143, - "type": "dns.name_not_resolved" - } - } - """; + const string sourceJson = + // language=json + """ + { + "age": 2, + "type": "network-error", + "url": "https://widget.com/thing.js", + "body": { + "sampling_fraction": 1.0, + "referrer": "https://www.example.com/", + "server_ip": "", + "protocol": "", + "method": "GET", + "status_code": 0, + "elapsed_time": 143, + "type": "dns.name_not_resolved" + } + } + """; - var reportRequest = JsonSerializer.Deserialize(sourceJson); + var reportRequest = JsonSerializer.Deserialize(sourceJson, jsonSerializerOptions); Assert.NotNull(reportRequest); Assert.Equal(2, reportRequest.Age); @@ -76,4 +86,16 @@ public void NetworkErrorTest() Assert.Equal("network-error", cspReport.Type); Assert.NotNull(cspReport.Body); } + + public static IEnumerable JsonSerializerOptionsData() + { + yield return new object?[] + { + null, + }; + yield return new object?[] + { + ReportingApiJsonSerializerContext.Default.Options, + }; + } } diff --git a/src/ReportingApi/ReportingApiJsonSerializerContext.cs b/src/ReportingApi/ReportingApiJsonSerializerContext.cs new file mode 100644 index 0000000..623019a --- /dev/null +++ b/src/ReportingApi/ReportingApiJsonSerializerContext.cs @@ -0,0 +1,12 @@ +using ReportingApi.Models; +using System.Text.Json.Serialization; + +namespace ReportingApi; + +[JsonSourceGenerationOptions( + GenerationMode = JsonSourceGenerationMode.Default +)] +[JsonSerializable(typeof(ReportRequest))] +[JsonSerializable(typeof(ReportRequest))] +[JsonSerializable(typeof(ReportRequest))] +public sealed partial class ReportingApiJsonSerializerContext : JsonSerializerContext;