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

Fix invalid JSON format for ${aspnet-request-cookie} and ${aspnet-request-querystring}, added extra formatting options, consistent rendering of multiple values #132

Merged
merged 5 commits into from
May 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#if !NETSTANDARD_1plus
//TODO test .NET Core
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using NLog.Web.LayoutRenderers;
using NSubstitute;
using NLog.Web.Enums;
using Xunit;

using System.Reflection;

using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
Expand All @@ -18,6 +18,8 @@
using System.Collections.Specialized;
using System.Web.SessionState;
#else
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif
Expand Down Expand Up @@ -75,9 +77,34 @@ public void KeyNotFoundRendersEmptyString_Json_Formatting()
[Fact]
public void KeyFoundRendersValue_Cookie_Mulitple_Items_Flat_Formatting()
{
#if NETSTANDARD_1plus
//no multivalue keys in ASP.NET core
var expectedResult = "key=TEST,Key1=TEST1";
#else
var expectedResult = "key=TEST&Key1=TEST1";
#endif

var renderer = CreateRenderer();

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

Assert.Equal(expectedResult, result);
}


[Fact]
public void KeyFoundRendersValue_Cookie_Multiple_Items_Flat_Formatting_separators()
{
#if NETSTANDARD_1plus
//no multivalue keys in ASP.NET core
var expectedResult = "key:TEST|Key1:TEST1";
#else
var expectedResult = "key:TEST&Key1=TEST1";
#endif

var renderer = CreateRenderer();
renderer.ValueSeparator = ":";
renderer.ItemSeparator = "|";

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

Expand All @@ -97,28 +124,60 @@ public void KeyFoundRendersValue_Single_Item_Flat_Formatting()
}

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

var renderer = CreateRenderer();
var renderer = CreateRenderer(addKey: false);

renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyFoundRendersValue_Single_Item_Json_Formatting_no_array()
{
var expectedResult = "{\"key\":\"TEST\"}";

var renderer = CreateRenderer(addKey: false);

renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;
renderer.SingleAsArray = false;

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

Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void KeyFoundRendersValue_Cookie_Mulitple_Items_Json_Formatting(bool singleAsArray)
{
var expectedResult = "[{\"key\":\"TEST\"},{\"Key1\":\"TEST1\"}]";

var renderer = CreateRenderer();

renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;
renderer.SingleAsArray = singleAsArray;

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

Assert.Equal(expectedResult, result);
}

//no multivalue keys in ASP.NET core
#if !NETSTANDARD_1plus

[Fact]
public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Flat_Formatting()
{
var expectedResult = "key=TEST&Key1=TEST1,key2=Test&key3=Test456";
var renderer = CreateRenderer(addCookie2: true);



var renderer = CreateRenderer(addCookie2: true);

renderer.CookieNames = new List<string> { "key", "key2" };

Expand All @@ -130,14 +189,17 @@ public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Flat_Forma
[Fact]
public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Json_Formatting()
{
var expectedResult = "{\"key=TEST&Key1=TEST1\"},{\"key2=Test&key3=Test456\"}";
var expectedResult = "[{\"key\":\"TEST\"},{\"Key1\":\"TEST1\"},{\"key2\":\"Test\"},{\"key3\":\"Test456\"}]";
var renderer = CreateRenderer(addCookie2: true);
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Assert.Equal(expectedResult, result);
}
#endif

#if !NETSTANDARD_1plus //todo

[Fact]
public void CommaSeperatedCookieNamesTest_Mulitple_FLAT_Formatting()
Expand Down Expand Up @@ -170,7 +232,7 @@ public void CommaSeperatedCookieNamesTest_Mulitple_FLAT_Formatting()
[Fact]
public void CommaSeperatedCookieNamesTest_Mulitple_Json_Formatting()
{
var expectedResult = "{\"key=TEST&Key1=TEST1\"}";
var expectedResult = "[{\"key\":\"TEST\"},{\"Key1\":\"TEST1\"}]";

string config = @"<nlog>
<extensions>
Expand All @@ -192,6 +254,8 @@ public void CommaSeperatedCookieNamesTest_Mulitple_Json_Formatting()
Assert.Equal(expectedResult, result);
}

#endif

/// <summary>
/// Create cookie renderer with mockup http context
/// </summary>
Expand All @@ -200,22 +264,64 @@ public void CommaSeperatedCookieNamesTest_Mulitple_Json_Formatting()
/// <returns></returns>
private static AspNetRequestCookieLayoutRenderer CreateRenderer(bool addKey = true, bool addCookie2 = false)
{
var cookieNames = new List<string>();
var httpContext = Substitute.For<HttpContextBase>();


#if NETSTANDARD_1plus
IRequestCookieCollection cookies = Substitute.For<IRequestCookieCollection>();
var cookieDict = new Dictionary<string, string>();

void AddCookie(string key, string result)
{
cookieNames.Add(key);
cookies[key].Returns(result);
cookieDict.Add(key, result);
}

AddCookie("key", "TEST");

if (addKey)
{
AddCookie("Key1", "TEST1");
}

if (addCookie2)
{
AddCookie("key2", "Test");
AddCookie("key3", "Test456");
}

cookies.Count.Returns(cookieDict.Count);

cookies.TryGetValue("", out var _)
.ReturnsForAnyArgs(callInfo =>
{
var name = callInfo.Args().First()?.ToString();
var returnVal = cookieDict.TryGetValue(name, out var cookie);
callInfo[1] = cookie;
return returnVal;
});

#else

var cookie1 = new HttpCookie("key", "TEST");
cookieNames.Add("key");
if (addKey)
{
cookie1["Key1"] = "TEST1";
}

var cookies = new HttpCookieCollection { cookie1 };
var cookieNames = new List<string> { "key" };

if (addCookie2)
{
var cookie2 = new HttpCookie("key2", "Test");
cookie2["key3"] = "Test456";
cookies.Add(cookie2);
cookieNames.Add("key2");
}
#endif


httpContext.Request.Cookies.Returns(cookies);
var renderer = new AspNetRequestCookieLayoutRenderer();
Expand All @@ -230,4 +336,3 @@ private static AspNetRequestCookieLayoutRenderer CreateRenderer(bool addKey = tr
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void KeyNotFoundRendersEmptyString_Json_Formatting()
[Fact]
public void KeyFoundRendersValue_QueryString_Single_Item_Flat_Formatting()
{
var expectedResult = "Id:1";
var expectedResult = "Id=1";

var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"));

Expand Down Expand Up @@ -98,7 +98,7 @@ public void KeyFoundRendersValue_QueryString_Single_Item_Json_Formatting()
[Fact]
public void KeyFoundRendersValue_QueryString_Multiple_Item_Flat_Formatting()
{
var expectedResult = "Id:1," + Environment.NewLine + "Id2:2";
var expectedResult = "Id=1,Id2=2";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo


var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"), CreateTuple("Id2", "2"));

Expand All @@ -113,7 +113,7 @@ public void KeyFoundRendersValue_QueryString_Multiple_Item_Flat_Formatting()
[Fact]
public void EmptyProperyShouldListAll()
{
var expectedResult = "Id:1," + Environment.NewLine + "Id2:2";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo

var expectedResult = "Id=1,Id2=2";

var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"), CreateTuple("Id2", "2"));

Expand All @@ -128,7 +128,7 @@ public void EmptyProperyShouldListAll()
[Fact]
public void NullProperyShouldListAll()
{
var expectedResult = "Id:1," + Environment.NewLine + "Id2:2";
var expectedResult = "Id=1,Id2=2";

var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"), CreateTuple("Id2", "2"));

Expand All @@ -142,7 +142,7 @@ public void NullProperyShouldListAll()
[Fact]
public void MultipleValuesForOneKeyShouldWork()
{
var expectedResult = "Id:1,2,3";
var expectedResult = "Id=1,2,3";

var renderer = CreateAndMockRenderer(CreateTuple("Id", "1", "2", "3"));

Expand All @@ -154,10 +154,27 @@ public void MultipleValuesForOneKeyShouldWork()
Assert.Equal(expectedResult, result);
}

[Fact]
public void MultipleValuesJsonQuoted()
{

var expectedResult = @"{""Id"":""a'b,\""c\""""}";

var renderer = CreateAndMockRenderer(CreateTuple("Id", "a'b", "\"c\""));

renderer.QueryStringKeys = null;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;
renderer.SingleAsArray = false;

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyFoundRendersValue_QueryString_Multiple_Item_Json_Formatting()
{
var expectedResult = "[" + "{\"Id\":\"1\"}," + Environment.NewLine + "{\"Id2\":\"2\"}" + "]";
var expectedResult = "[{\"Id\":\"1\"},{\"Id2\":\"2\"}]";

var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"), CreateTuple("Id2", "2"));

Expand Down
6 changes: 5 additions & 1 deletion NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net452</TargetFrameworks>
<TargetFrameworks>net452</TargetFrameworks>
<DefineConstants>$(DefineConstants);NETSTANDARD_1plus</DefineConstants>
<AssemblyName>NLog.Web.AspNetCore.Tests</AssemblyName>
</PropertyGroup>
Expand All @@ -26,6 +26,10 @@
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>



</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace NLog.Web.Enums
{
/// <summary>
/// To control the Cookie Renderer Output formatting.
/// To control the Renderer Output formatting.
/// </summary>
public enum AspNetRequestLayoutOutputFormat
{
/// <summary>
/// Use this format for rendering the cookie renderer output value as a flat string.
/// Use this format for rendering the output value as a flat string.
/// </summary>
Flat = 0,
/// <summary>
/// Use this format for rendering the cookie renderer output value as a json formatted string.
/// Use this format for rendering the output value as a json formatted string.
/// </summary>
Json = 1,
}
Expand Down
12 changes: 0 additions & 12 deletions NLog.Web.AspNetCore/Internal/GlobalConstants.cs

This file was deleted.

Loading