generated from LBHackney-IT/lbh-base-api
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MTTL-376 Setup boiler plate code for logging the correlation ID #2
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c6e061f
Added middleware + unit tests
evak6979 0b518ed
Added middleware to startup, ignored unecessary DB tests
evak6979 615b218
Added basecontroller action to retrieve correlationId + unit tests
evak6979 7a3bc8d
Removed dry run for circle-ci
evak6979 8630f16
Added constant file for correlationId
evak6979 61ded65
Merged from master
evak6979 052538c
Fixed merge issues
evak6979 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:52488/", | ||
"sslPort": 44338 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"BaseApi.Tests": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000" | ||
} | ||
} | ||
} |
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,52 @@ | ||
using System.Collections.Generic; | ||
using BaseApi.V1.Controllers; | ||
using BaseApi.V1.Infrastructure; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Routing; | ||
using NUnit.Framework; | ||
|
||
namespace BaseApi.Tests.V1.Controllers | ||
{ | ||
[TestFixture] | ||
public class BaseControllerTests | ||
{ | ||
private BaseController _sut; | ||
private ControllerContext _controllerContext; | ||
private HttpContext _stubHttpContext; | ||
|
||
[SetUp] | ||
public void Init() | ||
{ | ||
_stubHttpContext = new DefaultHttpContext(); | ||
_controllerContext = new ControllerContext(new ActionContext(_stubHttpContext, new RouteData(), new ControllerActionDescriptor())); | ||
_sut = new BaseController(); | ||
|
||
_sut.ControllerContext = _controllerContext; | ||
} | ||
|
||
[Test] | ||
public void GetCorrelationShouldThrowExceptionIfCorrelationHeaderUnavailable() | ||
{ | ||
// Arrange + Act + Assert | ||
_sut.Invoking(x => x.GetCorrelationId()) | ||
.Should().Throw<KeyNotFoundException>() | ||
.WithMessage("Request is missing a correlationId"); | ||
} | ||
|
||
[Test] | ||
public void GetCorrelationShouldReturnCorrelationIdWhenExists() | ||
{ | ||
// Arrange | ||
_stubHttpContext.Request.Headers.Add(Constants.CorrelationId, "123"); | ||
|
||
// Act | ||
var result = _sut.GetCorrelationId(); | ||
|
||
// Assert | ||
result.Should().BeEquivalentTo("123"); | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
BaseApi.Tests/V1/Infrastructure/CorrelationMiddlewareTest.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,49 @@ | ||
using System.Threading.Tasks; | ||
using BaseApi.V1.Infrastructure; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Http; | ||
using NUnit.Framework; | ||
|
||
namespace BaseApi.Tests.V1.Infrastructure | ||
{ | ||
[TestFixture] | ||
public class CorrelationMiddlewareTest | ||
{ | ||
private CorrelationMiddleware _sut; | ||
|
||
[SetUp] | ||
public void Init() | ||
{ | ||
_sut = new CorrelationMiddleware(null); | ||
} | ||
|
||
[Test] | ||
public async Task DoesNotReplaceCorrelationIdIfOneExists() | ||
{ | ||
// Arrange | ||
var httpContext = new DefaultHttpContext(); | ||
var headerValue = "123"; | ||
|
||
httpContext.HttpContext.Request.Headers.Add(Constants.CorrelationId, headerValue); | ||
|
||
// Act | ||
await _sut.InvokeAsync(httpContext).ConfigureAwait(false); | ||
|
||
// Assert | ||
httpContext.HttpContext.Request.Headers[Constants.CorrelationId].Should().BeEquivalentTo(headerValue); | ||
} | ||
|
||
[Test] | ||
public async Task AddsCorrelationIdIfOneDoesNotExist() | ||
{ | ||
// Arrange | ||
var httpContext = new DefaultHttpContext(); | ||
|
||
// Act | ||
await _sut.InvokeAsync(httpContext).ConfigureAwait(false); | ||
|
||
// Assert | ||
httpContext.HttpContext.Request.Headers[Constants.CorrelationId].Should().HaveCountGreaterThan(0); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
|
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,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace BaseApi.V1.Infrastructure | ||
{ | ||
public static class Constants | ||
{ | ||
public const string CorrelationId = "x-correlation-id"; | ||
} | ||
} |
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,37 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace BaseApi.V1.Infrastructure | ||
{ | ||
public class CorrelationMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public CorrelationMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (context.Request.Headers[Constants.CorrelationId].Count == 0) | ||
{ | ||
context.Request.Headers[Constants.CorrelationId] = Guid.NewGuid().ToString(); | ||
} | ||
|
||
if (_next != null) | ||
await _next(context).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
public static class CorrelationMiddlewareExtensions | ||
{ | ||
public static IApplicationBuilder UseCorrelation( | ||
this IApplicationBuilder builder) | ||
{ | ||
return builder.UseMiddleware<CorrelationMiddleware>(); | ||
} | ||
} | ||
} |
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,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:51761/", | ||
"sslPort": 44398 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"LBH_search_api.Tests": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000" | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The folder names need to be changed (both
BaseAPI
andBaseAPI.Tests
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to do so, but if possible we should merge the initial renaming PR. And then I can merge master from there to this branch which should resolve it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot the renaming PR hasn't been merged, ignore this comment please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries at all :) With 2 approvals, I took the liberty of merging the renaming PR to master and then into this branch. Renaming issues should be resolved by now.