Skip to content

Commit

Permalink
Merge pull request #22 from epignosisx/aspnet_layout_renderer_base_class
Browse files Browse the repository at this point in the history
Introducing ASP.NET Layout Renderer base class
  • Loading branch information
304NotModified committed Feb 3, 2016
2 parents 1bfaa80 + bec447e commit a1e2541
Show file tree
Hide file tree
Showing 21 changed files with 777 additions and 51 deletions.
29 changes: 29 additions & 0 deletions NLog.Web.Tests/DefaultHttpContextAccessorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.IO;
using System.Text;
using System.Web;
using Xunit;

namespace NLog.Web.Tests
{
public class DefaultHttpContextAccessorTests
{
[Fact]
public void UnavailableHttpContextReturnsNull()
{
var httpContextAccessor = new DefaultHttpContextAccessor();
Assert.Null(httpContextAccessor.HttpContext);
}

[Fact]
public void AvailableHttpContextIsReturned()
{
var httpContextAccessor = new DefaultHttpContextAccessor();
HttpContext.Current = new HttpContext(
new HttpRequest(null, "http://nlog-project.org", ""),
new HttpResponse(new StringWriter(new StringBuilder()))
);

Assert.NotNull(httpContextAccessor.HttpContext);
}
}
}
17 changes: 17 additions & 0 deletions NLog.Web.Tests/FakeHttpContextAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Web;

namespace NLog.Web.Tests
{
/// <summary>
/// Faked implementation of IHttpContextAccessor designed for unit testing.
/// </summary>
public class FakeHttpContextAccessor : IHttpContextAccessor
{
public HttpContextBase HttpContext { get; private set; }

public FakeHttpContextAccessor(HttpContextBase httpContext)
{
HttpContext = httpContext;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using NLog.Web.LayoutRenderers;
using NSubstitute;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetApplicationValueLayoutRendererTests
{
[Fact]
public void NullHttpContextRendersEmptyString()
{
var renderer = new AspNetApplicationValueLayoutRenderer();

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

Assert.Empty(result);
}

[Fact]
public void NullVariableRendersEmptyString()
{
var httpContext = Substitute.For<HttpContextBase>();

var renderer = new AspNetApplicationValueLayoutRenderer();
renderer.Variable = null;
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Empty(result);
}

[Fact]
public void VariableNotFoundRendersEmptyString()
{
var httpContext = Substitute.For<HttpContextBase>();

var renderer = new AspNetApplicationValueLayoutRenderer();
renderer.Variable = "key";
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Empty(result);
}

[Theory, MemberData("VariableFoundData")]
public void VariableFoundRendersValue(object expectedValue)
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Application["key"].Returns(expectedValue);

var renderer = new AspNetApplicationValueLayoutRenderer();
renderer.Variable = "key";
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result);
}

public static IEnumerable<object[]> VariableFoundData
{
get
{
yield return new object[] { "string"};
yield return new object[] { 1 };
yield return new object[] { 1.5 };
yield return new object[] { DateTime.Now };
yield return new object[] { Tuple.Create("a", 1) };
}
}
}
}
103 changes: 103 additions & 0 deletions NLog.Web.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using NLog.Web.LayoutRenderers;
using NSubstitute;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetItemValueLayoutRendererTests
{
[Fact]
public void NullHttpContextRendersEmptyString()
{
var renderer = new AspNetItemValueLayoutRenderer();

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

Assert.Empty(result);
}

[Fact]
public void NullVariableRendersEmptyString()
{
var httpContext = Substitute.For<HttpContextBase>();

var renderer = new AspNetItemValueLayoutRenderer();
renderer.Variable = null;
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Empty(result);
}

[Fact]
public void VariableNotFoundRendersEmptyString()
{
var httpContext = Substitute.For<HttpContextBase>();

var renderer = new AspNetItemValueLayoutRenderer();
renderer.Variable = "key";
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Empty(result);
}

[Theory, MemberData("VariableFoundData")]
public void VariableFoundRendersValue(object expectedValue)
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Items["key"].Returns(expectedValue);

var renderer = new AspNetItemValueLayoutRenderer();
renderer.Variable = "key";
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result);
}

[Theory, MemberData("NestedPropertyData")]
public void NestedPropertyRendersValue(string itemKey, string variable, object data, object expectedValue)
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Items[itemKey].Returns(data);

var renderer = new AspNetItemValueLayoutRenderer();
renderer.Variable = variable;
renderer.EvaluateAsNestedProperties = true;
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result);
}

public static IEnumerable<object[]> VariableFoundData
{
get
{
yield return new object[] { "string" };
yield return new object[] { 1 };
yield return new object[] { 1.5 };
yield return new object[] { DateTime.Now };
yield return new object[] { Tuple.Create("a", 1) };
}
}

public static IEnumerable<object[]> NestedPropertyData
{
get
{
yield return new object[] { "key", "key.Item1", Tuple.Create("value"), "value" };
yield return new object[] { "key", "key.Item1.Item1", Tuple.Create(Tuple.Create(1)), 1 };
}
}
}
}
Loading

0 comments on commit a1e2541

Please sign in to comment.