forked from NLog/NLog.Web
-
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 renders for is form content type and is https
- Loading branch information
Burak Akgerman
committed
Jun 8, 2022
1 parent
7152170
commit 3444edd
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRenderer.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,27 @@ | ||
using NLog.LayoutRenderers; | ||
using NLog.Web.Internal; | ||
using System.Text; | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Is Request Form Content Type | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-is-form-content-type} | ||
/// </remarks> | ||
[LayoutRenderer("aspnet-request-is-form-content-type")] | ||
public class AspNetRequestIsFormContentTypeLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Renders the specified ASP.NET Core HttpContext.Request.HasFormContentType variable and appends it to the specified <see cref="StringBuilder" />. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="StringBuilder" /> to append the rendered data to.</param> | ||
/// <param name="logEvent">Logging event.</param> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var request = HttpContextAccessor.HttpContext?.TryGetRequest(); | ||
builder.Append(request?.HasFormContentType == true ? '1' : '0'); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsHttpsLayoutRenderer.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,27 @@ | ||
using NLog.LayoutRenderers; | ||
using NLog.Web.Internal; | ||
using System.Text; | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Is Request HTTPS | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-is-https} | ||
/// </remarks> | ||
[LayoutRenderer("aspnet-request-is-https")] | ||
public class AspNetRequestIsHttpsLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Renders the specified ASP.NET Core HttpContext.Request.IsHttps variable and appends it to the specified <see cref="StringBuilder" />. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="StringBuilder" /> to append the rendered data to.</param> | ||
/// <param name="logEvent">Logging event.</param> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var request = HttpContextAccessor.HttpContext?.TryGetRequest(); | ||
builder.Append(request?.IsHttps == true ? '1' : '0'); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRendererTests.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,64 @@ | ||
using NLog.Web.LayoutRenderers; | ||
using NSubstitute; | ||
using NSubstitute.ReturnsExtensions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace NLog.Web.Tests.LayoutRenderers | ||
{ | ||
public class AspNetRequestIsFormContentTypeLayoutRendererTests : LayoutRenderersTestBase<AspNetRequestIsFormContentTypeLayoutRenderer> | ||
{ | ||
[Fact] | ||
public void TrueTest() | ||
{ | ||
// Arrange | ||
var (renderer, httpContext) = CreateWithHttpContext(); | ||
|
||
httpContext.Request.HasFormContentType.Returns(true); | ||
// Act | ||
string result = renderer.Render(new LogEventInfo()); | ||
// Assert | ||
Assert.Equal("1", result); | ||
} | ||
|
||
[Fact] | ||
public void FalseTest() | ||
{ | ||
// Arrange | ||
var (renderer, httpContext) = CreateWithHttpContext(); | ||
|
||
httpContext.Request.HasFormContentType.Returns(false); | ||
// Act | ||
string result = renderer.Render(new LogEventInfo()); | ||
// Assert | ||
Assert.Equal("0", result); | ||
} | ||
|
||
[Fact] | ||
public void NullTest() | ||
{ | ||
// Arrange | ||
var (renderer, httpContext) = CreateWithHttpContext(); | ||
|
||
httpContext.Request.ReturnsNull(); | ||
// Act | ||
string result = renderer.Render(new LogEventInfo()); | ||
// Assert | ||
Assert.Equal("0", result); | ||
} | ||
|
||
protected override void NullRendersEmptyString() | ||
{ | ||
// Arrange | ||
var (renderer, _) = CreateWithHttpContext(); | ||
|
||
// Act | ||
string result = renderer.Render(LogEventInfo.CreateNullEvent()); | ||
|
||
// Assert | ||
Assert.Equal("0", result); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsHttpsLayoutRendererTests.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,64 @@ | ||
using NLog.Web.LayoutRenderers; | ||
using NSubstitute; | ||
using NSubstitute.ReturnsExtensions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace NLog.Web.Tests.LayoutRenderers | ||
{ | ||
public class AspNetRequestIsHttpsLayoutRendererTests : LayoutRenderersTestBase<AspNetRequestIsHttpsLayoutRenderer> | ||
{ | ||
[Fact] | ||
public void TrueTest() | ||
{ | ||
// Arrange | ||
var (renderer, httpContext) = CreateWithHttpContext(); | ||
|
||
httpContext.Request.IsHttps.Returns(true); | ||
// Act | ||
string result = renderer.Render(new LogEventInfo()); | ||
// Assert | ||
Assert.Equal("1", result); | ||
} | ||
|
||
[Fact] | ||
public void FalseTest() | ||
{ | ||
// Arrange | ||
var (renderer, httpContext) = CreateWithHttpContext(); | ||
|
||
httpContext.Request.IsHttps.Returns(false); | ||
// Act | ||
string result = renderer.Render(new LogEventInfo()); | ||
// Assert | ||
Assert.Equal("0", result); | ||
} | ||
|
||
[Fact] | ||
public void NullTest() | ||
{ | ||
// Arrange | ||
var (renderer, httpContext) = CreateWithHttpContext(); | ||
|
||
httpContext.Request.ReturnsNull(); | ||
// Act | ||
string result = renderer.Render(new LogEventInfo()); | ||
// Assert | ||
Assert.Equal("0", result); | ||
} | ||
|
||
protected override void NullRendersEmptyString() | ||
{ | ||
// Arrange | ||
var (renderer, _) = CreateWithHttpContext(); | ||
|
||
// Act | ||
string result = renderer.Render(LogEventInfo.CreateNullEvent()); | ||
|
||
// Assert | ||
Assert.Equal("0", result); | ||
} | ||
} | ||
} |