Skip to content
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

AspNet Response HttpStatusCode LayoutRenderer as Enum.ToString #921

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/Shared/LayoutRenderers/AspNetResponseStatusCodeRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text;
using System;
using System.Net;
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
Expand All @@ -9,12 +11,31 @@ namespace NLog.Web.LayoutRenderers
/// ASP.NET Response Status Code.
/// </summary>
/// <remarks>
/// <code>${aspnet-response-statuscode}</code>
/// <code>${aspnet-response-statuscode} emits the http status code as integer</code>
/// <code>${aspnet-response-statuscode:Format=D} emits the http status code as integer</code>
/// <code>${aspnet-response-statuscode:Format=F} emits the http status code as enum-string-value</code>
/// <code>${aspnet-response-statuscode:Format=G} emits the http status code as enum-string-value</code>
/// <code>${aspnet-response-statuscode:Format=X} emits the http status code as hexadecimal</code>
/// </remarks>
/// <seealso href="https://github.com/NLog/NLog/wiki/AspNetResponse-StatusCode-Layout-Renderer">Documentation on NLog Wiki</seealso>
[LayoutRenderer("aspnet-response-statuscode")]
public class AspNetResponseStatusCodeRenderer : AspNetLayoutRendererBase
{
/// <summary>
/// A valid enumeration format string, defaults to integer format
/// </summary>
/// <remarks>
/// Supported Values, Case Insensitive
/// D: outputs the HttpStatusCode enum as a integer
/// F: outputs the HttpStatusCode enum as a string if possible, otherwise an integer
/// G: outputs the HttpStatusCode enum as a string if possible, otherwise an integer
/// X: outputs the HttpStatusCode enum as a hexadecimal
/// </remarks>
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/standard/base-types/enumeration-format-strings">Documentation on Enum Format Strings</seealso>

[DefaultParameter]
public string Format { get; set; } = "d";

/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
Expand All @@ -24,11 +45,22 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

int statusCode = httpResponse.StatusCode;
var statusCode = httpResponse.StatusCode;
if (statusCode < 100 || statusCode > 599)
{
// Only output valid HTTP status codes
return;
}

if (statusCode >= 100 && statusCode <= 599)
try
{
builder.Append(((HttpStatusCode)statusCode).ToString(Format));
}
catch (Exception ex)
{
builder.Append(statusCode);
NLog.Common.InternalLogger.Error(ex,
"Error occurred outputting HttpStatusCode enum ToString() with format specifier of {0} and value of {1}",
Format, statusCode);
}
}
}
Expand Down
239 changes: 236 additions & 3 deletions tests/Shared/LayoutRenderers/AspNetResponseStatusCodeRendererTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog.Web.LayoutRenderers;
using System.Net;
using NLog.Web.LayoutRenderers;
using NSubstitute;
using Xunit;

Expand All @@ -7,7 +8,7 @@ namespace NLog.Web.Tests.LayoutRenderers
public class AspNetResponseStatusCodeRendererTests : LayoutRenderersTestBase<AspNetResponseStatusCodeRenderer>
{
[Fact]
public void StatusCode_Set_Renderer()
public void StatusCode_Set_Renderer_DefaultFormat()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
Expand All @@ -20,13 +21,109 @@ public void StatusCode_Set_Renderer()
Assert.Equal("200", result);
}

[Fact]
public void StatusCode_Set_Renderer_EnumFormatF()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "f";

httpContext.Response.StatusCode.Returns(200);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal(((HttpStatusCode)200).ToString(), result);
}

[Fact]
public void StatusCode_Set_Renderer_EnumFormatG()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "g";

httpContext.Response.StatusCode.Returns(200);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal(((HttpStatusCode)200).ToString(), result);
}

[Fact]
public void StatusCode_Set_Renderer_IntegerFormat()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "d";

httpContext.Response.StatusCode.Returns(200);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal("200", result);
}

[Fact]
public void StatusCode_Set_Renderer_HexFormat()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "x";

httpContext.Response.StatusCode.Returns(200);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal(((HttpStatusCode)200).ToString("x"), result);
}

[Fact]
public void StatusCode_Set_Renderer_InvalidFormat()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "invalid";

httpContext.Response.StatusCode.Returns(200);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal("", result);
}

[Fact]
public void StatusCode_Set_Renderer_UpperCaseIntegerFormat()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "D";

httpContext.Response.StatusCode.Returns(200);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal("200", result);
}

[Theory]
[InlineData(0, false)]
[InlineData(99, false)]
[InlineData(100, true)]
[InlineData(599, true)]
[InlineData(600, false)]
public void Only_Render_Valid_StatusCodes(int statusCode, bool shouldBeRendered)
public void Only_Render_Valid_StatusCodes_DefaultFormat(int statusCode, bool shouldBeRendered)
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
Expand All @@ -45,5 +142,141 @@ public void Only_Render_Valid_StatusCodes(int statusCode, bool shouldBeRendered)
Assert.Empty(result);
}
}

[Theory]
[InlineData(0, false)]
[InlineData(99, false)]
[InlineData(100, true)]
[InlineData(599, true)]
[InlineData(600, false)]
public void Only_Render_Valid_StatusCodes_EnumFormatF(int statusCode, bool shouldBeRendered)
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "f";
httpContext.Response.StatusCode.Returns(statusCode);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
if (shouldBeRendered)
{
Assert.Equal(((HttpStatusCode)statusCode).ToString(), result);
}
else
{
Assert.Empty(result);
}
}


[Theory]
[InlineData(0, false)]
[InlineData(99, false)]
[InlineData(100, true)]
[InlineData(599, true)]
[InlineData(600, false)]
public void Only_Render_Valid_StatusCodes_EnumFormatG(int statusCode, bool shouldBeRendered)
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "g";
httpContext.Response.StatusCode.Returns(statusCode);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
if (shouldBeRendered)
{
Assert.Equal(((HttpStatusCode)statusCode).ToString(), result);
}
else
{
Assert.Empty(result);
}
}

[Theory]
[InlineData(0, false)]
[InlineData(99, false)]
[InlineData(100, true)]
[InlineData(599, true)]
[InlineData(600, false)]
public void Only_Render_Valid_StatusCodes_IntegerFormat(int statusCode, bool shouldBeRendered)
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "d";
httpContext.Response.StatusCode.Returns(statusCode);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
if (shouldBeRendered)
{
Assert.Equal($"{statusCode}", result);
}
else
{
Assert.Empty(result);
}
}

[Theory]
[InlineData(0, false)]
[InlineData(99, false)]
[InlineData(100, true)]
[InlineData(599, true)]
[InlineData(600, false)]
public void Only_Render_Valid_StatusCodes_UpperCaseIntegerFormat(int statusCode, bool shouldBeRendered)
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "D";
httpContext.Response.StatusCode.Returns(statusCode);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
if (shouldBeRendered)
{
Assert.Equal($"{statusCode}", result);
}
else
{
Assert.Empty(result);
}
}

[Theory]
[InlineData(0, false)]
[InlineData(99, false)]
[InlineData(100, true)]
[InlineData(599, true)]
[InlineData(600, false)]
public void Only_Render_Valid_StatusCodes_HexFormat(int statusCode, bool shouldBeRendered)
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.Format = "x";
httpContext.Response.StatusCode.Returns(statusCode);

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
if (shouldBeRendered)
{
Assert.Equal(((HttpStatusCode)statusCode).ToString("x"), result);
}
else
{
Assert.Empty(result);
}
}
}
}