Skip to content

Commit

Permalink
Add ReportingApiJsonSerializerContext (#24)
Browse files Browse the repository at this point in the history
* Add ReportingApiJsonSerializerContext

* Update JsonSourceGenerationOptions
  • Loading branch information
trejjam authored Nov 15, 2023
1 parent 60e0f34 commit 881b8c3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 45 deletions.
112 changes: 67 additions & 45 deletions src/ReportingApi.Tests/ReportRequestConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,81 @@
using ReportingApi.Models;
using System.Collections.Generic;
using System.Text.Json;
using Xunit;

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<ReportRequest>(sourceJson);
var reportRequest = JsonSerializer.Deserialize<ReportRequest>(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<CspReport>>(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<ReportRequest>(sourceJson);
var reportRequest = JsonSerializer.Deserialize<ReportRequest>(sourceJson, jsonSerializerOptions);

Assert.NotNull(reportRequest);
Assert.Equal(2, reportRequest.Age);
Expand All @@ -76,4 +86,16 @@ public void NetworkErrorTest()
Assert.Equal("network-error", cspReport.Type);
Assert.NotNull(cspReport.Body);
}

public static IEnumerable<object?[]> JsonSerializerOptionsData()
{
yield return new object?[]
{
null,
};
yield return new object?[]
{
ReportingApiJsonSerializerContext.Default.Options,
};
}
}
12 changes: 12 additions & 0 deletions src/ReportingApi/ReportingApiJsonSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using ReportingApi.Models;
using System.Text.Json.Serialization;

namespace ReportingApi;

[JsonSourceGenerationOptions(
GenerationMode = JsonSourceGenerationMode.Default
)]
[JsonSerializable(typeof(ReportRequest))]
[JsonSerializable(typeof(ReportRequest<CspReport>))]
[JsonSerializable(typeof(ReportRequest<NetworkErrorReport>))]
public sealed partial class ReportingApiJsonSerializerContext : JsonSerializerContext;

0 comments on commit 881b8c3

Please sign in to comment.