Skip to content

Commit

Permalink
Merge pull request #54 from NLog/additional-renderers
Browse files Browse the repository at this point in the history
Additional aspnet renderers & unit tests for .net core
  • Loading branch information
304NotModified authored Jul 8, 2016
2 parents 18aa78d + ee03151 commit eb9ea06
Show file tree
Hide file tree
Showing 50 changed files with 11,320 additions and 692 deletions.
7 changes: 5 additions & 2 deletions NLog.Web.AspNetCore.Tests/DefaultHttpContextAccessorTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.IO;
#if !NETSTANDARD_1plus
using System.IO;
using System.Text;
using System.Web;
using NLog.Web.Tests.LayoutRenderers;
using Xunit;

namespace NLog.Web.Tests
{
public class DefaultHttpContextAccessorTests
public class DefaultHttpContextAccessorTests: TestBase
{
[Fact]
public void UnavailableHttpContextReturnsNull()
Expand All @@ -27,3 +29,4 @@ public void AvailableHttpContextIsReturned()
}
}
}
#endif
12 changes: 10 additions & 2 deletions NLog.Web.AspNetCore.Tests/FakeHttpContextAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using System.Web;
#if !NETSTANDARD_1plus
using System.Web;
using System.Collections.Specialized;
using System.Web.SessionState;
#else
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif

namespace NLog.Web.Tests
{
Expand All @@ -7,7 +15,7 @@ namespace NLog.Web.Tests
/// </summary>
public class FakeHttpContextAccessor : IHttpContextAccessor
{
public HttpContextBase HttpContext { get; private set; }
public HttpContextBase HttpContext { get; set; }

public FakeHttpContextAccessor(HttpContextBase httpContext)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
#if !NETSTANDARD_1plus
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
public class AspNetApplicationValueLayoutRendererTests : TestBase
{
[Fact]
public void NullHttpContextRendersEmptyString()
Expand Down Expand Up @@ -76,3 +78,4 @@ public static IEnumerable<object[]> VariableFoundData
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@

#if !NETSTANDARD_1plus
//TODO test .NET Core
using System;
using System.Collections.Generic;
using System.Globalization;
using NLog.Web.LayoutRenderers;
using NSubstitute;
using NLog.Web.Enums;
using Xunit;

using System.Reflection;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;

#if !NETSTANDARD_1plus
using System.Web;
using System.Collections.Specialized;
using System.Web.SessionState;
#else
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif



namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetCookieLayoutRendererTests : TestInvolvingAspNetHttpContext
{
public AspNetCookieLayoutRendererTests() : base()
{

}

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

var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = null;

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

Assert.Empty(result);
}

[Fact]
public void KeyNotFoundRendersEmptyString_Flat_Formatting()
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Request.Cookies.Returns(new HttpCookieCollection { new HttpCookie("key1", "TEST") });
var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Flat;

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

Assert.Empty(result);
}

[Fact]
public void KeyNotFoundRendersEmptyString_Json_Formatting()
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Request.Cookies.Returns(new HttpCookieCollection { new HttpCookie("key1", "TEST") });
var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;

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

Assert.Empty(result);
}

[Fact]
public void KeyFoundRendersValue_Cookie_Mulitple_Items_Flat_Formatting()
{
var expectedResult = "key=TEST&Key1=TEST1";
var httpContext = Substitute.For<HttpContextBase>();
var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";
var cookies = new HttpCookieCollection();
cookies.Add(cookie);
httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyFoundRendersValue_Single_Item_Flat_Formatting()
{
var expectedResult = "key=TEST";
var httpContext = Substitute.For<HttpContextBase>();
var cookie = new HttpCookie("key", "TEST");
var cookies = new HttpCookieCollection();
cookies.Add(cookie);
httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyFoundRendersValue_Cookie_Mulitple_Items_Json_Formatting()
{
var expectedResult = "{\"key=TEST&Key1=TEST1\"}";
var httpContext = Substitute.For<HttpContextBase>();
var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";
var cookies = new HttpCookieCollection();
cookies.Add(cookie);
httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Flat_Formatting()
{
var expectedResult = "key=TEST&Key1=TEST1,key2=Test&key3=Test456";
var httpContext = Substitute.For<HttpContextBase>();
var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";
var cookies = new HttpCookieCollection();
cookies.Add(cookie);

cookie = new HttpCookie("key2", "Test");
cookie["key3"] = "Test456";
cookies.Add(cookie);

httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key", "key2" };

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Json_Formatting()
{
var expectedResult = "{\"key=TEST&Key1=TEST1\"},{\"key2=Test&key3=Test456\"}";
var httpContext = Substitute.For<HttpContextBase>();
var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";
var cookies = new HttpCookieCollection();
cookies.Add(cookie);

cookie = new HttpCookie("key2", "Test");
cookie["key3"] = "Test456";
cookies.Add(cookie);

httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key", "key2" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void CommaSeperatedCookieNamesTest_Mulitple_FLAT_Formatting()
{
var expectedResult = "key=TEST&Key1=TEST1";

string config = @"<nlog>
<extensions>
<add assembly='NLog.Web' />
</extensions>
<targets><target name='debug' type='Debug' layout='${aspnet-request-cookie:CookieNames=key,key1}' /></targets>
<rules>
<logger name='*' minlevel='Debug' writeTo='debug' />
</rules>
</nlog>";
LogManager.Configuration = CreateConfigurationFromString(config);

var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";

this.HttpContext.Request.Cookies.Add(cookie);
var t = (DebugTarget)LogManager.Configuration.AllTargets[0];
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetCookieLayoutRenderer;

var result = renderer.Render(LogEventInfo.CreateNullEvent());

Assert.Equal(expectedResult, result);
}

[Fact]
public void CommaSeperatedCookieNamesTest_Mulitple_Json_Formatting()
{
var expectedResult = "{\"key=TEST&Key1=TEST1\"}";

string config = @"<nlog>
<extensions>
<add assembly='NLog.Web' />
</extensions>
<targets><target name='debug' type='Debug' layout='${aspnet-request-cookie:CookieNames=key,key1:OutputFormat=Json}' /></targets>
</nlog>";
LogManager.Configuration = CreateConfigurationFromString(config);

var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";

this.HttpContext.Request.Cookies.Add(cookie);
var t = (DebugTarget)LogManager.Configuration.AllTargets[0];
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetCookieLayoutRenderer;

var result = renderer.Render(LogEventInfo.CreateNullEvent());

Assert.Equal(expectedResult, result);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using System;
using System.Collections.Generic;
using System.Globalization;
#if !NETSTANDARD_1plus
using System.Web;
using System.Collections.Specialized;
using System.Web.SessionState;
#else
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif
using NLog.Web.LayoutRenderers;
using NSubstitute;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetItemValueLayoutRendererTests
public class AspNetItemValueLayoutRendererTests : TestBase
{
[Fact]
public void NullHttpContextRendersEmptyString()
Expand Down Expand Up @@ -52,8 +59,13 @@ public void VariableNotFoundRendersEmptyString()
public void VariableFoundRendersValue(object expectedValue)
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Items["key"].Returns(expectedValue);

#if NETSTANDARD_1plus
httpContext.Items = new Dictionary<object, object>();
httpContext.Items.Add("key", expectedValue);
#else
httpContext.Items["key"].Returns(expectedValue);
#endif

var renderer = new AspNetItemValueLayoutRenderer();
renderer.Variable = "key";
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Globalization;
#if !NETSTANDARD_1plus
using System.Web;
using System.Collections.Specialized;
using System.Web.SessionState;
#else
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Http.Features;
#endif
using NLog.Web.LayoutRenderers;
using NSubstitute;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetMvcActionRendererTests : TestBase
{
[Fact]
public void NullRoutesRenderersEmptyString()
{
var httpContext = Substitute.For<HttpContextBase>();
#if NETSTANDARD_1plus
var routingFeature = Substitute.For<IRoutingFeature>();
var collection = new FeatureCollection();
collection.Set<IRoutingFeature>(routingFeature);
httpContext.Features.Returns(collection);
#endif
var renderer = new AspNetMvcActionRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);

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

Assert.Empty(result);
}
}
}
Loading

0 comments on commit eb9ea06

Please sign in to comment.