Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional ASP.NET renderers (.NET Core compliant) & unit tests for .NET Core #54

Merged
merged 42 commits into from
Jul 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7303897
aspnet-request-cookie Renderer
s-sreenath Apr 24, 2016
5a41374
aspnet-request-referrer Renderer.
s-sreenath Apr 28, 2016
3dbbcab
aspnet-request-referrer
s-sreenath Apr 28, 2016
16979b1
aspnet-request-url and aspnet-request-referrer
s-sreenath Apr 30, 2016
11d1ad3
aspnet-useragent
s-sreenath Apr 30, 2016
52a51b4
PR Comments and improvement to documentation.
s-sreenath May 1, 2016
66fffc1
aspnet-request-method
s-sreenath May 2, 2016
eadbb51
Code improvement
s-sreenath May 4, 2016
27affc1
Code Improvement
s-sreenath May 4, 2016
e527f37
Merge branch 'master' into additional-renderers
s-sreenath May 12, 2016
1670a44
WIP - QueryString and Code Improvements
s-sreenath May 13, 2016
b1add61
Merge branch 'master' into additional-renderers
s-sreenath May 13, 2016
3cb3d2b
QueryString Renderer and Unit Test Fixes for Cookie Layout.
s-sreenath May 15, 2016
09d67bb
Asp.net 5 QueryString Change
s-sreenath May 15, 2016
e6568f1
CommaSeperatedCookieNamesTest
s-sreenath May 18, 2016
7eb65a6
testing completed with new NLog Version.
s-sreenath May 19, 2016
b25c9f4
Modified.
s-sreenath May 25, 2016
bba2b1d
Merge branch 'master' into additional-renderers
s-sreenath May 25, 2016
fc80bbe
ASPNET Core implementation.
s-sreenath May 26, 2016
434738a
asp.net core implementation
s-sreenath May 29, 2016
8a1dd50
PR Changes and Unit Test Fixes.
s-sreenath Jun 3, 2016
d1aee01
Merge branch 'master' into additional-renderers
s-sreenath Jun 4, 2016
b7603f6
unit test failure fixes.
s-sreenath Jun 4, 2016
0ac98e5
Fixes.
s-sreenath Jun 4, 2016
b2cb81d
Included testbase
304NotModified Jun 5, 2016
cb34600
fix nullref and small cleanup
304NotModified Jun 5, 2016
cff6906
Remove duplicate code
304NotModified Jun 5, 2016
ba1732d
Merge branch 'additional-renderers' of https://github.com/NLog/NLog.W…
s-sreenath Jun 5, 2016
7d2b40c
Removed the Duplicate Code and made some more improvements.
s-sreenath Jun 5, 2016
d1d87db
Remove uneended file
304NotModified Jun 8, 2016
5dc4f9d
Move testbase
304NotModified Jun 8, 2016
286acd1
WIP
304NotModified Jun 8, 2016
ee48f19
Unit Test Fixes for asp.net Core and few more code improvements.
s-sreenath Jun 12, 2016
cb4bc84
Merge branch 'master' into additional-renderers
s-sreenath Jul 4, 2016
f2225f4
Merge branch 'master' into additional-renderers+NETCore-tests
s-sreenath Jul 4, 2016
e589183
Fixed the Build Errors.
s-sreenath Jul 4, 2016
b4df306
Fixed the Build Errors.
s-sreenath Jul 4, 2016
69a622c
Update unit tests project.json for .Net Core
304NotModified Jul 8, 2016
5f350f7
run unit tests for asp.net core on appveyor
304NotModified Jul 8, 2016
becd68d
update lock
304NotModified Jul 8, 2016
0f814e2
Merge branch 'additional-renderers+NETCore-tests' into additional-ren…
304NotModified Jul 8, 2016
ee03151
Fix wrong merge
304NotModified Jul 8, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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