-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from TerribleDev/expectCt
add expect ct
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using HardHat.Builders; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace HardHat.UnitTests | ||
{ | ||
public class ExpectCtHeaderTests | ||
{ | ||
[Fact] | ||
public void TestExceptions() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => ExpectCtHeaderBuilder.Build(0, string.Empty)); | ||
} | ||
|
||
[Fact] | ||
public void TestHeader() | ||
{ | ||
var result = ExpectCtHeaderBuilder.Build(0, "/awesome"); | ||
|
||
Assert.Equal("max-age=0; report-uri=\"/awesome\"", result); | ||
} | ||
|
||
[Fact] | ||
public void TestHeaderWithEnforce() | ||
{ | ||
var result = ExpectCtHeaderBuilder.Build(0, "/awesome", true); | ||
|
||
Assert.Equal("max-age=0; report-uri=\"/awesome\"; enforce", result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace HardHat.Builders | ||
{ | ||
internal static class ExpectCtHeaderBuilder | ||
{ | ||
internal static string Build(ulong maxAge, string reportUri, bool enforce = false) | ||
{ | ||
if (string.IsNullOrWhiteSpace(reportUri)) | ||
{ | ||
throw new ArgumentNullException(nameof(reportUri), "Report URI must have a value"); | ||
} | ||
var builder = new StringBuilder($"max-age={maxAge}; report-uri=\"{reportUri}\""); | ||
if (enforce) | ||
{ | ||
builder.Append("; enforce"); | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using HardHat.Builders; | ||
using Microsoft.AspNetCore.Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HardHat.Middlewares | ||
{ | ||
public class ExpectCt | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly string headerValue; | ||
public ExpectCt(RequestDelegate next, ulong maxAge, string reportUri, bool enforce = false) | ||
{ | ||
this._next = next; | ||
if(string.IsNullOrWhiteSpace(reportUri)) | ||
{ | ||
throw new ArgumentNullException(nameof(reportUri), "Report URI must have a value"); | ||
} | ||
headerValue = ExpectCtHeaderBuilder.Build(maxAge, reportUri, enforce); | ||
} | ||
|
||
public Task Invoke(HttpContext context) | ||
{ | ||
context.Response.Headers[Constants.ExpectCt] = headerValue; | ||
return _next?.Invoke(context); | ||
} | ||
} | ||
} |