-
Notifications
You must be signed in to change notification settings - Fork 3
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 #31 from sia-digital/update-packages
Update packages (minor + major)
- Loading branch information
Showing
5 changed files
with
153 additions
and
48 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
26 changes: 26 additions & 0 deletions
26
...Abstractions/src/PiBox.Hosting.Abstractions/Middlewares/EnrichRequestMetricsMiddleware.cs
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,26 @@ | ||
using Chronos.Abstractions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using PiBox.Hosting.Abstractions.Attributes; | ||
|
||
namespace PiBox.Hosting.Abstractions.Middlewares | ||
{ | ||
[Middleware] | ||
public sealed class EnrichRequestMetricsMiddleware : ApiMiddleware | ||
{ | ||
public EnrichRequestMetricsMiddleware(RequestDelegate next, IDateTimeProvider dateTimeProvider) : base(next, | ||
dateTimeProvider) | ||
{ | ||
} | ||
|
||
public override Task Invoke(HttpContext context) | ||
{ | ||
var tagsFeature = context.Features.Get<IHttpMetricsTagsFeature>(); | ||
if (tagsFeature == null) return Next.Invoke(context); | ||
var authorizedParty = context.User.Claims.SingleOrDefault(x => x.Type == "azp")?.Value; | ||
tagsFeature.Tags.Add(new KeyValuePair<string, object>("azp", authorizedParty ?? "")); | ||
|
||
return Next.Invoke(context); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
.../test/PiBox.Hosting.Abstractions.Tests/Middlewares/EnrichRequestMetricsMiddlewareTests.cs
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,86 @@ | ||
using System.Security.Claims; | ||
using Chronos.Abstractions; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
using PiBox.Hosting.Abstractions.Middlewares; | ||
|
||
namespace PiBox.Hosting.Abstractions.Tests.Middlewares | ||
{ | ||
public class EnrichRequestMetricsMiddlewareTests | ||
{ | ||
private readonly IDateTimeProvider _dateTimeProvider = Substitute.For<IDateTimeProvider>(); | ||
|
||
private static HttpContext GetContext() | ||
{ | ||
return new DefaultHttpContext { Response = { Body = new MemoryStream() } }; | ||
} | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_dateTimeProvider.UtcNow.Returns(new DateTime(2020, 1, 1)); | ||
} | ||
|
||
[Test] | ||
public async Task ExistingAzpClaimShouldAddTagWithUserIdToMetrics() | ||
{ | ||
var httpMetricsTagsFeature = Substitute.For<IHttpMetricsTagsFeature>(); | ||
httpMetricsTagsFeature.Tags.Returns(new List<KeyValuePair<string, object>>()); | ||
var middleware = new EnrichRequestMetricsMiddleware(x => | ||
{ | ||
x.Response.StatusCode = 200; | ||
return Task.CompletedTask; | ||
}, _dateTimeProvider); | ||
var context = GetContext(); | ||
|
||
context.User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() { new Claim("azp", "userid1") })); | ||
context.Features.Set(httpMetricsTagsFeature); | ||
|
||
await middleware.Invoke(context); | ||
var metrics = context.Features.Get<IHttpMetricsTagsFeature>(); | ||
metrics.Tags.Should().Contain(x => x.Key == "azp" && x.Value.ToString() == "userid1"); | ||
} | ||
|
||
[Test] | ||
public async Task NonExistingAzpClaimShouldAddTagWithEmptyValueToMetrics() | ||
{ | ||
var httpMetricsTagsFeature = Substitute.For<IHttpMetricsTagsFeature>(); | ||
httpMetricsTagsFeature.Tags.Returns(new List<KeyValuePair<string, object>>()); | ||
var middleware = new EnrichRequestMetricsMiddleware(x => | ||
{ | ||
x.Response.StatusCode = 200; | ||
return Task.CompletedTask; | ||
}, _dateTimeProvider); | ||
var context = GetContext(); | ||
|
||
context.User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() { new Claim("azp1", "userid1") })); | ||
context.Features.Set(httpMetricsTagsFeature); | ||
|
||
await middleware.Invoke(context); | ||
var metrics = context.Features.Get<IHttpMetricsTagsFeature>(); | ||
metrics.Tags.Should().Contain(x => x.Key == "azp" && x.Value.ToString() == ""); | ||
} | ||
|
||
[Test] | ||
public async Task NoHttpMetricsTagsFeatureShouldNotAddMetricTagsToResponse() | ||
{ | ||
var httpMetricsTagsFeature = Substitute.For<IHttpMetricsTagsFeature>(); | ||
httpMetricsTagsFeature.Tags.Returns(new List<KeyValuePair<string, object>>()); | ||
var middleware = new EnrichRequestMetricsMiddleware(x => | ||
{ | ||
x.Response.StatusCode = 200; | ||
return Task.CompletedTask; | ||
}, _dateTimeProvider); | ||
var context = GetContext(); | ||
|
||
context.User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() { new Claim("azp1", "userid1") })); | ||
|
||
await middleware.Invoke(context); | ||
var metrics = context.Features.Get<IHttpMetricsTagsFeature>(); | ||
metrics.Should().BeNull(); | ||
} | ||
} | ||
} |
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