From 3eef952a2fd35537933073b95aa070d2e71695ab Mon Sep 17 00:00:00 2001 From: Alex Gil Date: Mon, 11 Jan 2016 21:57:46 -0500 Subject: [PATCH 1/3] initial work --- .../DefaultHttpContextAccessorTests.cs | 29 ++++++++++++ NLog.Web.Tests/NLog.Web.Tests.csproj | 2 + NLog.Web/DefaultHttpContextAccessor.cs | 23 ++++++++++ NLog.Web/IHttpContextAccessor.cs | 16 +++++++ .../AspNetApplicationValueLayoutRenderer.cs | 10 ++--- .../AspNetLayoutRendererBase.cs | 44 +++++++++++++++++++ NLog.Web/NLog.Web.csproj | 4 ++ 7 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 NLog.Web.Tests/DefaultHttpContextAccessorTests.cs create mode 100644 NLog.Web/DefaultHttpContextAccessor.cs create mode 100644 NLog.Web/IHttpContextAccessor.cs create mode 100644 NLog.Web/LayoutRenderers/AspNetLayoutRendererBase.cs diff --git a/NLog.Web.Tests/DefaultHttpContextAccessorTests.cs b/NLog.Web.Tests/DefaultHttpContextAccessorTests.cs new file mode 100644 index 00000000..32cd0595 --- /dev/null +++ b/NLog.Web.Tests/DefaultHttpContextAccessorTests.cs @@ -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); + } + } +} diff --git a/NLog.Web.Tests/NLog.Web.Tests.csproj b/NLog.Web.Tests/NLog.Web.Tests.csproj index 9c0314c7..801f0058 100644 --- a/NLog.Web.Tests/NLog.Web.Tests.csproj +++ b/NLog.Web.Tests/NLog.Web.Tests.csproj @@ -45,6 +45,7 @@ + ..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll True @@ -71,6 +72,7 @@ + diff --git a/NLog.Web/DefaultHttpContextAccessor.cs b/NLog.Web/DefaultHttpContextAccessor.cs new file mode 100644 index 00000000..ed231216 --- /dev/null +++ b/NLog.Web/DefaultHttpContextAccessor.cs @@ -0,0 +1,23 @@ +using System.Web; + +namespace NLog.Web +{ + /// + /// Provides the HttpContext associated with the current request. + /// + public class DefaultHttpContextAccessor : IHttpContextAccessor + { + /// + /// HttpContext of the current request. + /// + public HttpContextBase HttpContext + { + get + { + if (System.Web.HttpContext.Current == null) + return null; + return new HttpContextWrapper(System.Web.HttpContext.Current); + } + } + } +} \ No newline at end of file diff --git a/NLog.Web/IHttpContextAccessor.cs b/NLog.Web/IHttpContextAccessor.cs new file mode 100644 index 00000000..2611e042 --- /dev/null +++ b/NLog.Web/IHttpContextAccessor.cs @@ -0,0 +1,16 @@ +using System; +using System.Web; + +namespace NLog.Web +{ + /// + /// Provides access to the HttpContext + /// + public interface IHttpContextAccessor + { + /// + /// HttpContext associated with the current request + /// + HttpContextBase HttpContext { get; } + } +} diff --git a/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs index 2d438d70..5b121290 100644 --- a/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs @@ -34,7 +34,7 @@ namespace NLog.Web.LayoutRenderers /// /// [LayoutRenderer("aspnet-application")] - public class AspNetApplicationValueLayoutRenderer : LayoutRenderer + public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase { /// /// Gets or sets the variable name. @@ -49,18 +49,14 @@ public class AspNetApplicationValueLayoutRenderer : LayoutRenderer /// /// The to append the rendered data to. /// Logging event. - protected override void Append(StringBuilder builder, LogEventInfo logEvent) + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { if (this.Variable == null) { return; } - HttpContext context = HttpContext.Current; - if (context == null) - { - return; - } + HttpContextBase context = HttpContextAccessor.HttpContext; if (context.Application == null) { diff --git a/NLog.Web/LayoutRenderers/AspNetLayoutRendererBase.cs b/NLog.Web/LayoutRenderers/AspNetLayoutRendererBase.cs new file mode 100644 index 00000000..81710909 --- /dev/null +++ b/NLog.Web/LayoutRenderers/AspNetLayoutRendererBase.cs @@ -0,0 +1,44 @@ +using System.Text; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// Base class for ASP.NET layout renderers. + /// + public abstract class AspNetLayoutRendererBase : LayoutRenderer + { + /// + /// Initializes the with the . + /// + protected AspNetLayoutRendererBase() + { + HttpContextAccessor = new DefaultHttpContextAccessor(); + } + + /// + /// Provides access to the current request HttpContext. + /// + public IHttpContextAccessor HttpContextAccessor { get; set; } + + /// + /// Validates that the HttpContext is available and delegates append to subclasses.. + /// + /// The to append the rendered data to. + /// Logging event. + protected override void Append(StringBuilder builder, LogEventInfo logEvent) + { + if (HttpContextAccessor.HttpContext == null) + return; + + DoAppend(builder, logEvent); + } + + /// + /// Implemented by subclasses to render request information and append it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected abstract void DoAppend(StringBuilder builder, LogEventInfo logEvent); + } +} diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index 5e70efd6..6fd279e5 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -47,14 +47,18 @@ + + + + From c2825ae8dd1ac77dcea6a9ee017d449f19516be3 Mon Sep 17 00:00:00 2001 From: Alex Gil Date: Tue, 12 Jan 2016 20:59:37 -0500 Subject: [PATCH 2/3] modified existing aspnet layout renderers to inherit from AspNetLayoutRendererBase --- .../LayoutRenderers/AspNetItemValueLayoutRenderer.cs | 10 +++------- .../AspNetRequestValueLayoutRenderer.cs | 11 +++-------- .../LayoutRenderers/AspNetSessionIDLayoutRenderer.cs | 10 +++------- .../AspNetSessionValueLayoutRenderer.cs | 10 +++------- .../AspNetUserAuthTypeLayoutRenderer.cs | 10 +++------- .../AspNetUserIdentityLayoutRenderer.cs | 12 ++++-------- 6 files changed, 19 insertions(+), 44 deletions(-) diff --git a/NLog.Web/LayoutRenderers/AspNetItemValueLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetItemValueLayoutRenderer.cs index 839d522e..a1c41686 100644 --- a/NLog.Web/LayoutRenderers/AspNetItemValueLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/AspNetItemValueLayoutRenderer.cs @@ -36,7 +36,7 @@ namespace NLog.Web.LayoutRenderers /// /// [LayoutRenderer("aspnet-item")] - public class AspNetItemValueLayoutRenderer : LayoutRenderer + public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase { /// /// Gets or sets the item variable name. @@ -56,18 +56,14 @@ public class AspNetItemValueLayoutRenderer : LayoutRenderer /// /// The to append the rendered data to. /// Logging event. - protected override void Append(StringBuilder builder, LogEventInfo logEvent) + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { if (Variable == null) { return; } - HttpContext context = HttpContext.Current; - if (context == null) - { - return; - } + HttpContextBase context = HttpContextAccessor.HttpContext; var value = PropertyReader.GetValue(Variable, k => context.Items[k], EvaluateAsNestedProperties); diff --git a/NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs index cc264789..33649b50 100644 --- a/NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs @@ -23,7 +23,7 @@ namespace NLog.Web.LayoutRenderers /// /// [LayoutRenderer("aspnet-request")] - public class AspNetRequestValueLayoutRenderer : LayoutRenderer + public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase { /// /// Gets or sets the item name. The QueryString, Form, Cookies, or ServerVariables collection variables having the specified name are rendered. @@ -61,14 +61,9 @@ public class AspNetRequestValueLayoutRenderer : LayoutRenderer /// /// The to append the rendered data to. /// Logging event. - protected override void Append(StringBuilder builder, LogEventInfo logEvent) + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - HttpContext context = HttpContext.Current; - if (context == null) - { - return; - } - + HttpContextBase context = HttpContextAccessor.HttpContext; var httpRequest = context.Request; if (httpRequest == null) { diff --git a/NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs index 2e15eaf9..86413366 100644 --- a/NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs @@ -8,20 +8,16 @@ namespace NLog.Web.LayoutRenderers /// ASP.NET Session ID. /// [LayoutRenderer("aspnet-sessionid")] - public class AspNetSessionIDLayoutRenderer : LayoutRenderer + public class AspNetSessionIDLayoutRenderer : AspNetLayoutRendererBase { /// /// Renders the ASP.NET Session ID appends it to the specified . /// /// The to append the rendered data to. /// Logging event. - protected override void Append(StringBuilder builder, LogEventInfo logEvent) + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - HttpContext context = HttpContext.Current; - if (context == null) - { - return; - } + HttpContextBase context = HttpContextAccessor.HttpContext; if (context.Session == null) { diff --git a/NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index e6809845..aacae4b5 100644 --- a/NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -37,7 +37,7 @@ namespace NLog.Web.LayoutRenderers /// /// [LayoutRenderer("aspnet-session")] - public class AspNetSessionValueLayoutRenderer : LayoutRenderer + public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase { /// /// Gets or sets the session variable name. @@ -57,18 +57,14 @@ public class AspNetSessionValueLayoutRenderer : LayoutRenderer /// /// The to append the rendered data to. /// Logging event. - protected override void Append(StringBuilder builder, LogEventInfo logEvent) + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { if (this.Variable == null) { return; } - HttpContext context = HttpContext.Current; - if (context == null) - { - return; - } + HttpContextBase context = HttpContextAccessor.HttpContext; if (context.Session == null) { diff --git a/NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs index f889b23a..d52427f6 100644 --- a/NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs @@ -8,20 +8,16 @@ namespace NLog.Web.LayoutRenderers /// ASP.NET User variable. /// [LayoutRenderer("aspnet-user-authtype")] - public class AspNetUserAuthTypeLayoutRenderer : LayoutRenderer + public class AspNetUserAuthTypeLayoutRenderer : AspNetLayoutRendererBase { /// /// Renders the specified ASP.NET User.Identity.AuthenticationType variable and appends it to the specified . /// /// The to append the rendered data to. /// Logging event. - protected override void Append(StringBuilder builder, LogEventInfo logEvent) + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - HttpContext context = HttpContext.Current; - if (context == null) - { - return; - } + HttpContextBase context = HttpContextAccessor.HttpContext; if (context.User == null) { diff --git a/NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs index bd625547..9bf21b25 100644 --- a/NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs @@ -8,21 +8,17 @@ namespace NLog.Web.LayoutRenderers /// ASP.NET User variable. /// [LayoutRenderer("aspnet-user-identity")] - public class AspNetUserIdentityLayoutRenderer : LayoutRenderer + public class AspNetUserIdentityLayoutRenderer : AspNetLayoutRendererBase { /// /// Renders the specified ASP.NET User.Identity.Name variable and appends it to the specified . /// /// The to append the rendered data to. /// Logging event. - protected override void Append(StringBuilder builder, LogEventInfo logEvent) + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - var context = HttpContext.Current; - if (context == null) - { - return; - } - + HttpContextBase context = HttpContextAccessor.HttpContext; + if (context.User == null) { return; From bec447e7ff9c66586b3e5e84a1022acf0774ea9e Mon Sep 17 00:00:00 2001 From: Alex Gil Date: Thu, 21 Jan 2016 21:37:43 -0500 Subject: [PATCH 3/3] added unit tests to existing aspnet layout renderers --- NLog.Web.Tests/FakeHttpContextAccessor.cs | 17 ++ ...pNetApplicationValueLayoutRendererTests.cs | 78 ++++++ .../AspNetItemValueLayoutRendererTests.cs | 103 +++++++ .../AspNetRequestValueLayoutRendererTests.cs | 256 ++++++++++++++++++ .../AspNetSessionIDLayoutRendererTests.cs | 49 ++++ .../AspNetUserAuthTypeLayoutRendererTests.cs | 69 +++++ .../AspNetUserIdentityLayoutRendererTests.cs | 53 ++++ NLog.Web.Tests/NLog.Web.Tests.csproj | 11 + NLog.Web.Tests/packages.config | 1 + 9 files changed, 637 insertions(+) create mode 100644 NLog.Web.Tests/FakeHttpContextAccessor.cs create mode 100644 NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs create mode 100644 NLog.Web.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs create mode 100644 NLog.Web.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs create mode 100644 NLog.Web.Tests/LayoutRenderers/AspNetSessionIDLayoutRendererTests.cs create mode 100644 NLog.Web.Tests/LayoutRenderers/AspNetUserAuthTypeLayoutRendererTests.cs create mode 100644 NLog.Web.Tests/LayoutRenderers/AspNetUserIdentityLayoutRendererTests.cs diff --git a/NLog.Web.Tests/FakeHttpContextAccessor.cs b/NLog.Web.Tests/FakeHttpContextAccessor.cs new file mode 100644 index 00000000..4a0fc609 --- /dev/null +++ b/NLog.Web.Tests/FakeHttpContextAccessor.cs @@ -0,0 +1,17 @@ +using System.Web; + +namespace NLog.Web.Tests +{ + /// + /// Faked implementation of IHttpContextAccessor designed for unit testing. + /// + public class FakeHttpContextAccessor : IHttpContextAccessor + { + public HttpContextBase HttpContext { get; private set; } + + public FakeHttpContextAccessor(HttpContextBase httpContext) + { + HttpContext = httpContext; + } + } +} diff --git a/NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs b/NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs new file mode 100644 index 00000000..53cf6b79 --- /dev/null +++ b/NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs @@ -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(); + + 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(); + + 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(); + 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 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) }; + } + } + } +} diff --git a/NLog.Web.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs b/NLog.Web.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs new file mode 100644 index 00000000..f21b3e9c --- /dev/null +++ b/NLog.Web.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs @@ -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(); + + 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(); + + 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(); + 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(); + 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 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 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 }; + } + } + } +} diff --git a/NLog.Web.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs b/NLog.Web.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs new file mode 100644 index 00000000..1461e490 --- /dev/null +++ b/NLog.Web.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs @@ -0,0 +1,256 @@ +using System.Collections.Specialized; +using System.Web; +using NLog.Web.LayoutRenderers; +using NSubstitute; +using Xunit; + +namespace NLog.Web.Tests.LayoutRenderers +{ + public class AspNetRequestValueLayoutRendererTests + { + [Fact] + public void NullHttpContextRendersEmptyString() + { + var renderer = new AspNetRequestValueLayoutRenderer(); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + public class ItemTests + { + [Fact] + public void NullKeyRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Item = null; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyNotFoundRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Item = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyFoundRendersValue() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + httpContext.Request["key"].Returns(expectedResult); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Item = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } + + public class QueryStringTests + { + [Fact] + public void NullKeyRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.QueryString = null; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyNotFoundRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.QueryString = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyFoundRendersValue() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + httpContext.Request.QueryString.Returns(new NameValueCollection { {"key", expectedResult} }); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.QueryString = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } + + public class FormTests + { + [Fact] + public void NullKeyRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Form = null; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyNotFoundRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Form = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyFoundRendersValue() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + httpContext.Request.Form.Returns(new NameValueCollection { { "key", expectedResult } }); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Form = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } + + public class ServerVariablesTests + { + [Fact] + public void NullKeyRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.ServerVariable = null; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyNotFoundRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.ServerVariable = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyFoundRendersValue() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + httpContext.Request.ServerVariables.Returns(new NameValueCollection { { "key", expectedResult } }); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.ServerVariable = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } + + public class CookieTests + { + [Fact] + public void NullKeyRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Cookie = null; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyNotFoundRendersEmptyString() + { + var httpContext = Substitute.For(); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Cookie = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void KeyFoundRendersValue() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + httpContext.Request.Cookies.Returns(new HttpCookieCollection {new HttpCookie("key", expectedResult) }); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Cookie = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } + } +} diff --git a/NLog.Web.Tests/LayoutRenderers/AspNetSessionIDLayoutRendererTests.cs b/NLog.Web.Tests/LayoutRenderers/AspNetSessionIDLayoutRendererTests.cs new file mode 100644 index 00000000..fcf39859 --- /dev/null +++ b/NLog.Web.Tests/LayoutRenderers/AspNetSessionIDLayoutRendererTests.cs @@ -0,0 +1,49 @@ +using System.Web; +using NLog.Web.LayoutRenderers; +using NSubstitute; +using Xunit; + +namespace NLog.Web.Tests.LayoutRenderers +{ + public class AspNetSessionIDLayoutRendererTests + { + [Fact] + public void NullHttpContextRendersEmptyString() + { + var renderer = new AspNetSessionIDLayoutRenderer(); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void NullSessionRendersEmptyString() + { + var httpContext = Substitute.For(); + httpContext.Session.Returns(null as HttpSessionStateWrapper); + + var renderer = new AspNetSessionIDLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void AvailableSessionRendersSessionId() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + httpContext.Session.SessionID.Returns(expectedResult); + + var renderer = new AspNetSessionIDLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } +} diff --git a/NLog.Web.Tests/LayoutRenderers/AspNetUserAuthTypeLayoutRendererTests.cs b/NLog.Web.Tests/LayoutRenderers/AspNetUserAuthTypeLayoutRendererTests.cs new file mode 100644 index 00000000..be2db8fb --- /dev/null +++ b/NLog.Web.Tests/LayoutRenderers/AspNetUserAuthTypeLayoutRendererTests.cs @@ -0,0 +1,69 @@ +using System.Security.Principal; +using System.Web; +using NLog.Web.LayoutRenderers; +using NSubstitute; +using Xunit; + +namespace NLog.Web.Tests.LayoutRenderers +{ + public class AspNetUserAuthTypeLayoutRendererTests + { + [Fact] + public void NullHttpContextRendersEmptyString() + { + var renderer = new AspNetUserAuthTypeLayoutRenderer(); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void NullUserIdentityRendersEmptyString() + { + var httpContext = Substitute.For(); + httpContext.User.Identity.Returns(null as IIdentity); + + var renderer = new AspNetUserAuthTypeLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void UnauthenticatedUserRendersEmptyString() + { + var httpContext = Substitute.For(); + var identity = Substitute.For(); + identity.IsAuthenticated.Returns(false); + httpContext.User.Identity.Returns(identity); + + var renderer = new AspNetUserAuthTypeLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void AuthenticatedUserRendersAuthenticationType() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + var identity = Substitute.For(); + identity.IsAuthenticated.Returns(true); + identity.AuthenticationType.Returns(expectedResult); + httpContext.User.Identity.Returns(identity); + + var renderer = new AspNetUserAuthTypeLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } +} diff --git a/NLog.Web.Tests/LayoutRenderers/AspNetUserIdentityLayoutRendererTests.cs b/NLog.Web.Tests/LayoutRenderers/AspNetUserIdentityLayoutRendererTests.cs new file mode 100644 index 00000000..abe03660 --- /dev/null +++ b/NLog.Web.Tests/LayoutRenderers/AspNetUserIdentityLayoutRendererTests.cs @@ -0,0 +1,53 @@ +using System.Security.Principal; +using System.Web; +using NLog.Web.LayoutRenderers; +using NSubstitute; +using Xunit; + +namespace NLog.Web.Tests.LayoutRenderers +{ + public class AspNetUserIdentityLayoutRendererTests + { + [Fact] + public void NullHttpContextRendersEmptyString() + { + var renderer = new AspNetUserIdentityLayoutRenderer(); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + [Fact] + public void NullUserIdentityRendersEmptyString() + { + var httpContext = Substitute.For(); + httpContext.User.Identity.Returns(null as IIdentity); + + var renderer = new AspNetUserIdentityLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + } + + + [Fact] + public void UserIdentityNameRendersName() + { + var expectedResult = "value"; + var httpContext = Substitute.For(); + var identity = Substitute.For(); + identity.Name.Returns(expectedResult); + httpContext.User.Identity.Returns(identity); + + var renderer = new AspNetUserIdentityLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(expectedResult, result); + } + } +} diff --git a/NLog.Web.Tests/NLog.Web.Tests.csproj b/NLog.Web.Tests/NLog.Web.Tests.csproj index 801f0058..03f83cb5 100644 --- a/NLog.Web.Tests/NLog.Web.Tests.csproj +++ b/NLog.Web.Tests/NLog.Web.Tests.csproj @@ -43,6 +43,10 @@ ..\packages\NLog.4.0.1\lib\net45\NLog.dll True + + ..\packages\NSubstitute.1.9.2.0\lib\net45\NSubstitute.dll + True + @@ -73,7 +77,14 @@ + + + + + + + diff --git a/NLog.Web.Tests/packages.config b/NLog.Web.Tests/packages.config index fd4bd933..5fff404a 100644 --- a/NLog.Web.Tests/packages.config +++ b/NLog.Web.Tests/packages.config @@ -1,6 +1,7 @@  +