Skip to content

Commit

Permalink
Added renders for is form content type and is https
Browse files Browse the repository at this point in the history
  • Loading branch information
Burak Akgerman committed Jun 8, 2022
1 parent 7152170 commit 3444edd
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
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');
}
}
}
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');
}
}
}
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);
}
}
}
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);
}
}
}

0 comments on commit 3444edd

Please sign in to comment.