-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Tests for CorrelationId in the Http package, Added HttpContext …
…Customization in the Testing package. (#description: Added Tests for CorrelationId in the Http package, Added HttpContext Customization in the Testing package)
- Loading branch information
Farshad DASHTI
authored and
Farshad DASHTI
committed
Oct 8, 2024
1 parent
0691c13
commit 5725a92
Showing
7 changed files
with
175 additions
and
18 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
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 was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
src/DfE.CoreLibs.Testing/AutoFixture/Customizations/HttpContextCustomization.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,13 @@ | ||
using AutoFixture; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace DfE.CoreLibs.Testing.AutoFixture.Customizations | ||
{ | ||
public class HttpContextCustomization : ICustomization | ||
{ | ||
public void Customize(IFixture fixture) | ||
{ | ||
fixture.Register(() => new DefaultHttpContext()); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Tests/DfE.CoreLibs.Http.Tests/DfE.CoreLibs.Http.Tests.csproj
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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="FluentAssertions" Version="6.12.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NSubstitute" Version="5.1.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\DfE.CoreLibs.Http\DfE.CoreLibs.Http.csproj" /> | ||
<ProjectReference Include="..\..\DfE.CoreLibs.Testing\DfE.CoreLibs.Testing.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
119 changes: 119 additions & 0 deletions
119
src/Tests/DfE.CoreLibs.Http.Tests/Middlewares/CorrelationIdMiddlewareTests.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,119 @@ | ||
using AutoFixture; | ||
using AutoFixture.Xunit2; | ||
using DfE.CoreLibs.Http.Interfaces; | ||
using DfE.CoreLibs.Http.Middlewares.CorrelationId; | ||
using DfE.CoreLibs.Testing.AutoFixture.Attributes; | ||
using DfE.CoreLibs.Testing.AutoFixture.Customizations; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
using System.Net; | ||
|
||
namespace DfE.CoreLibs.Http.Tests.Middlewares | ||
{ | ||
public class CorrelationIdMiddlewareTests | ||
{ | ||
private readonly RequestDelegate _nextDelegate; | ||
private readonly ILogger<CorrelationIdMiddleware> _logger; | ||
private readonly ICorrelationContext _correlationContext; | ||
private readonly CorrelationIdMiddleware _middleware; | ||
|
||
public CorrelationIdMiddlewareTests() | ||
{ | ||
_nextDelegate = Substitute.For<RequestDelegate>(); | ||
_logger = Substitute.For<ILogger<CorrelationIdMiddleware>>(); | ||
_correlationContext = Substitute.For<ICorrelationContext>(); | ||
_middleware = new CorrelationIdMiddleware(_nextDelegate, _logger); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData(typeof(HttpContextCustomization))] | ||
public async Task Invoke_ShouldSetNewCorrelationId_WhenHeaderNotPresent( | ||
HttpContext context) | ||
{ | ||
// Arrange | ||
context.Request.Headers.Remove(Keys.HeaderKey); | ||
context.Response.Body = new System.IO.MemoryStream(); | ||
|
||
_nextDelegate.Invoke(context).Returns(Task.CompletedTask); | ||
|
||
// Act | ||
await _middleware.Invoke(context, _correlationContext); | ||
|
||
// Assert | ||
Assert.True(Guid.TryParse(context.Response.Headers[Keys.HeaderKey], out var correlationId)); | ||
Assert.NotEqual(Guid.Empty, correlationId); | ||
_correlationContext.Received(1).SetContext(Arg.Any<Guid>()); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData(typeof(HttpContextCustomization))] | ||
public async Task Invoke_ShouldRetainExistingCorrelationId_WhenHeaderPresent([Frozen] IFixture fixture, | ||
DefaultHttpContext context) | ||
{ | ||
// Arrange | ||
var existingCorrelationId = fixture.Create<Guid>(); | ||
context.Request.Headers[Keys.HeaderKey] = existingCorrelationId.ToString(); | ||
context.Response.Body = new System.IO.MemoryStream(); | ||
|
||
_nextDelegate.Invoke(context).Returns(Task.CompletedTask); | ||
|
||
// Act | ||
await _middleware.Invoke(context, _correlationContext); | ||
|
||
// Assert | ||
Assert.Equal(existingCorrelationId.ToString(), context.Response.Headers[Keys.HeaderKey]); | ||
_correlationContext.Received(1).SetContext(existingCorrelationId); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData(typeof(HttpContextCustomization))] | ||
public async Task Invoke_ShouldReturnBadRequest_WhenCorrelationIdIsEmpty(DefaultHttpContext context) | ||
{ | ||
// Arrange | ||
context.Request.Headers[Keys.HeaderKey] = Guid.Empty.ToString(); | ||
context.Response.Body = new System.IO.MemoryStream(); | ||
|
||
_nextDelegate.Invoke(context).Returns(Task.CompletedTask); | ||
|
||
// Act | ||
await _middleware.Invoke(context, _correlationContext); | ||
|
||
// Assert | ||
Assert.Equal((int)HttpStatusCode.BadRequest, context.Response.StatusCode); | ||
Assert.Contains("Bad Request", ReadResponseBody(context)); | ||
_correlationContext.DidNotReceive().SetContext(Arg.Any<Guid>()); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData(typeof(HttpContextCustomization))] | ||
public async Task Invoke_ShouldLogNewGuidWarning_WhenHeaderCannotBeParsed(DefaultHttpContext context) | ||
{ | ||
// Arrange | ||
context.Request.Headers[Keys.HeaderKey] = "invalid-guid"; | ||
context.Response.Body = new System.IO.MemoryStream(); | ||
|
||
_nextDelegate.Invoke(context).Returns(Task.CompletedTask); | ||
|
||
// Act | ||
await _middleware.Invoke(context, _correlationContext); | ||
|
||
// Assert | ||
_logger.Received(1).Log( | ||
LogLevel.Warning, | ||
Arg.Any<EventId>(), | ||
Arg.Is<object>(v => v.ToString().Contains("Detected header x-correlationId, but value cannot be parsed to a GUID")), | ||
Arg.Any<Exception>(), | ||
Arg.Any<Func<object, Exception, string>>()!); | ||
|
||
_correlationContext.Received(1).SetContext(Arg.Any<Guid>()); | ||
} | ||
|
||
private static string ReadResponseBody(HttpContext context) | ||
{ | ||
context.Response.Body.Seek(0, System.IO.SeekOrigin.Begin); | ||
using var reader = new System.IO.StreamReader(context.Response.Body); | ||
return reader.ReadToEnd(); | ||
} | ||
} | ||
} |