Skip to content

Commit

Permalink
Add support for NetworkErrorReport (#1)
Browse files Browse the repository at this point in the history
* Add support for NetworkErrorReport

* Add Github actions into sln

* Add Code-Style auto fixer

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
trejjam and github-actions[bot] committed Jun 18, 2023
1 parent ec74f9b commit 8b58c5c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 7 deletions.
18 changes: 18 additions & 0 deletions .github/dotnet-format-problem-matcher.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"problemMatcher": [
{
"owner": "dotnet-format",
"pattern": [
{
"regexp": "^\\s*(.*)\\((\\d+),(\\d+)\\):\\s+(.*)\\s+(.*):\\s+(.*)\\s+\\[(.*)\\]$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"code": 5,
"message": 6
}
]
}
]
}
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
dotnet-version: '7.0.x'
- uses: actions/checkout@v3
- name: Retore
- name: Restore
run: dotnet restore --nologo
- name: Build
run: dotnet build --no-restore --nologo --configuration Release
Expand All @@ -36,7 +36,7 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
- name: Retore
- name: Restore
run: dotnet restore --nologo
- name: .Net Format
run: dotnet format --no-restore --verify-no-changes -v diag
29 changes: 27 additions & 2 deletions .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
dotnet-version: '7.0.x'
- uses: actions/checkout@v3
- name: Retore
- name: Restore
run: dotnet restore --nologo
- name: Build
run: dotnet build --no-restore --nologo --configuration Release
Expand All @@ -31,11 +31,36 @@ jobs:
DOTNET_CLI_TELEMETRY_OPTOUT: 'true'
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Setup .Net 7.0.x
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
- name: Retore
- name: Restore
run: dotnet restore --nologo
- name: Add .Net Format Problem Matcher
shell: bash
run: echo "::add-matcher::${{ github.workspace }}/.github/dotnet-format-problem-matcher.json"
- name: .Net Format
id: format
run: dotnet format --no-restore --verify-no-changes -v diag
- name: Remove .Net Format Problem Matcher
shell: bash
if: always()
run: echo "::remove-matcher owner=dotnet-format::"
- name: .Net Format
if: failure() && steps.format.outcome == 'failure'
run: dotnet format --no-restore -v diag
- name: Commit files
if: failure() && steps.format.outcome == 'failure'
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -a -m "Fix CodeStyle"
- name: Push changes
if: failure() && steps.format.outcome == 'failure'
uses: ad-m/github-push-action@master
with:
branch: ${{ github.head_ref }}
20 changes: 20 additions & 0 deletions ReportingApi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportingApi", "src\Reporti
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportingApi.Tests", "src\ReportingApi.Tests\ReportingApi.Tests.csproj", "{6001C0E3-D5B9-4537-ACE2-1187D28799BF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{392035D4-EFC8-47FB-A388-44DA18A853CD}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
README.md = README.md
.github\dependabot.yml = .github\dependabot.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{B5569CC8-011A-43C4-BAD8-2733CAC14274}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build.yml = .github\workflows\build.yml
.github\workflows\nightly-release.yml = .github\workflows\nightly-release.yml
.github\workflows\pr-build.yml = .github\workflows\pr-build.yml
.github\workflows\release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -19,4 +36,7 @@ Global
{6001C0E3-D5B9-4537-ACE2-1187D28799BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6001C0E3-D5B9-4537-ACE2-1187D28799BF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B5569CC8-011A-43C4-BAD8-2733CAC14274} = {392035D4-EFC8-47FB-A388-44DA18A853CD}
EndGlobalSection
EndGlobal
40 changes: 38 additions & 2 deletions src/ReportingApi.Tests/ReportRequestConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,47 @@ public void CspViolationTest()

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("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);
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);

var cspReport = Assert.IsType<ReportRequest<CspReport>>(reportRequest);
Assert.Equal("csp-violation", cspReport.Type);
Assert.NotNull(cspReport.Body);
}

[Fact]
public void NetworkErrorTest()
{
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"
}
}
""";

var reportRequest = JsonSerializer.Deserialize<ReportRequest>(sourceJson);

Assert.NotNull(reportRequest);
Assert.Equal(2, reportRequest.Age);
Assert.Equal("https://widget.com/thing.js", reportRequest.Url);
Assert.Null(reportRequest.UserAgent);

var cspReport = Assert.IsType<ReportRequest<NetworkErrorReport>>(reportRequest);
Assert.Equal("network-error", cspReport.Type);
Assert.NotNull(cspReport.Body);
}
}
5 changes: 5 additions & 0 deletions src/ReportingApi/JsonConverters/ReportRequestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ private Type GetBodyType(ReadOnlySpan<byte> type)
return typeof(CspReport);
}

if (type.SequenceEqual("network-error"u8))
{
return typeof(NetworkErrorReport);
}

var typeString = Encoding.UTF8.GetString(type);
throw new JsonException($"Type '{typeString}' does not have defined body type");
}
Expand Down
30 changes: 30 additions & 0 deletions src/ReportingApi/Models/NetworkErrorReport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text.Json.Serialization;

namespace ReportingApi.Models;

public sealed record NetworkErrorReport : IReportBody
{
[JsonPropertyName("sampling_fraction")]
public decimal SamplingFraction { get; set; }

[JsonPropertyName("referrer")]
public string Referrer { get; set; } = null!;

[JsonPropertyName("server_ip")]
public string ServerIp { get; set; } = null!;

[JsonPropertyName("protocol")]
public string Protocol { get; set; } = null!;

[JsonPropertyName("method")]
public string Method { get; set; } = null!;

[JsonPropertyName("status_code")]
public int StatusCode { get; set; }

[JsonPropertyName("elapsed_time")]
public int ElapsedTime { get; set; }

[JsonPropertyName("type")]
public string Type { get; set; } = null!;
}
2 changes: 1 addition & 1 deletion src/ReportingApi/Models/ReportRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public record ReportRequest
public string Url { get; set; } = null!;

[JsonPropertyName("user_agent")]
public string UserAgent { get; set; } = null!;
public string? UserAgent { get; set; }
}

public sealed record ReportRequest<TBody> : ReportRequest
Expand Down

0 comments on commit 8b58c5c

Please sign in to comment.