-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from epignosisx/aspnet_layout_renderer_base_class
Introducing ASP.NET Layout Renderer base class
- Loading branch information
Showing
21 changed files
with
777 additions
and
51 deletions.
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
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); | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.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,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
103
NLog.Web.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.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,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 }; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.