-
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 #54 from NLog/additional-renderers
Additional aspnet renderers & unit tests for .net core
- Loading branch information
Showing
50 changed files
with
11,320 additions
and
692 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
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
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
246 changes: 246 additions & 0 deletions
246
NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetCookieLayoutRendererTests.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,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 |
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
40 changes: 40 additions & 0 deletions
40
NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetMvcActionRendererTests.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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.