Skip to content

Commit

Permalink
Merge pull request #98 from NLog/all-querystrings
Browse files Browse the repository at this point in the history
support for empty 'QueryStringKeys' for ${all-querystrings{aspnet-request-querystring}
  • Loading branch information
304NotModified authored Feb 10, 2017
2 parents 9afb113 + 0b44bfa commit 92707ee
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if !NETSTANDARD_1plus
//todo nsubstitute


using System;
using System.Collections.Generic;
Expand All @@ -13,70 +12,23 @@
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
using HttpSessionState = Microsoft.AspNetCore.Http.ISession;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
#endif
using NLog.Web.LayoutRenderers;
using NSubstitute;
using NLog.Web.Enums;
using Xunit;
using System.Collections.Specialized;
using System.Reflection;
using NLog.Targets;
using NLog.Layouts;


namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetQueryStringLayoutRendererTests : TestInvolvingAspNetHttpContext
{
public AspNetQueryStringLayoutRendererTests() : base()
{
this.SetUp();
}

public void SetUp()
{
//auto load won't work yet (in DNX), so use <extensions>
SetupFakeSession();
}

protected override void CleanUp()
{
Session.Clear();
}

private HttpSessionState Session
{
get
{
#if NETSTANDARD_1plus
return HttpContext.Session;
#else
return HttpContext.Current.Session;
#endif
}
}

public void SetupFakeSession()
{
var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 10, true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);

HttpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, CallingConventions.Standard,
new[] { typeof(HttpSessionStateContainer) },
null)
.Invoke(new object[] { sessionContainer });
}

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

var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
var renderer = CreateAndMockRenderer();
renderer.QueryStringKeys = null;

string result = renderer.Render(new LogEventInfo());
Expand All @@ -87,13 +39,8 @@ public void NullKeyRendersEmptyString()
[Fact]
public void KeyNotFoundRendersEmptyString_Flat_Formatting()
{
var httpContext = Substitute.For<HttpContextBase>();
var namedClollection = new NameValueCollection();
namedClollection.Add("Id", "1");
httpContext.Request.QueryString.Returns(namedClollection);
var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"));

var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "key" };
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

Expand All @@ -105,13 +52,8 @@ public void KeyNotFoundRendersEmptyString_Flat_Formatting()
[Fact]
public void KeyNotFoundRendersEmptyString_Json_Formatting()
{
var httpContext = Substitute.For<HttpContextBase>();
var namedClollection = new NameValueCollection();
namedClollection.Add("Id", "1");
httpContext.Request.QueryString.Returns(namedClollection);
var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"));

var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "key" };
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

Expand All @@ -120,17 +62,16 @@ public void KeyNotFoundRendersEmptyString_Json_Formatting()
Assert.Empty(result);
}




[Fact]
public void KeyFoundRendersValue_QueryString_Single_Item_Flat_Formatting()
{
var expectedResult = "Id:1";
var httpContext = Substitute.For<HttpContextBase>();
var namedClollection = new NameValueCollection();
namedClollection.Add("Id", "1");
httpContext.Request.QueryString.Returns(namedClollection);

var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"));

renderer.QueryStringKeys = new List<string> { "Id" };
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

Expand All @@ -143,13 +84,9 @@ public void KeyFoundRendersValue_QueryString_Single_Item_Flat_Formatting()
public void KeyFoundRendersValue_QueryString_Single_Item_Json_Formatting()
{
var expectedResult = "[{\"Id\":\"1\"}]";
var httpContext = Substitute.For<HttpContextBase>();
var namedClollection = new NameValueCollection();
namedClollection.Add("Id", "1");
httpContext.Request.QueryString.Returns(namedClollection);

var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"));

renderer.QueryStringKeys = new List<string> { "Id" };
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

Expand All @@ -162,14 +99,9 @@ public void KeyFoundRendersValue_QueryString_Single_Item_Json_Formatting()
public void KeyFoundRendersValue_QueryString_Multiple_Item_Flat_Formatting()
{
var expectedResult = "Id:1," + Environment.NewLine + "Id2:2";
var httpContext = Substitute.For<HttpContextBase>();
var namedClollection = new NameValueCollection();
namedClollection.Add("Id", "1");
namedClollection.Add("Id2", "2");
httpContext.Request.QueryString.Returns(namedClollection);

var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"), CreateTuple("Id2", "2"));

renderer.QueryStringKeys = new List<string> { "Id", "Id2" };
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

Expand All @@ -178,25 +110,119 @@ public void KeyFoundRendersValue_QueryString_Multiple_Item_Flat_Formatting()
Assert.Equal(expectedResult, result);
}

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

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

renderer.QueryStringKeys = new List<string> { };
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

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

Assert.Equal(expectedResult, result);
}

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

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

renderer.QueryStringKeys = null;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

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

Assert.Equal(expectedResult, result);
}
[Fact]
public void MultipleValuesForOneKeyShouldWork()
{
var expectedResult = "Id:1,2,3";

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

renderer.QueryStringKeys = null;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

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 httpContext = Substitute.For<HttpContextBase>();
var namedClollection = new NameValueCollection();
namedClollection.Add("Id", "1");
namedClollection.Add("Id2", "2");
httpContext.Request.QueryString.Returns(namedClollection);

var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
var renderer = CreateAndMockRenderer(CreateTuple("Id", "1"), CreateTuple("Id2", "2"));

renderer.QueryStringKeys = new List<string> { "Id", "Id2" };
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Assert.Equal(expectedResult, result);
}
}


/// <summary>
/// Create tuple with 1 or more values (with 1 key)
/// </summary>
/// <param name="key"></param>
/// <param name="values"></param>
/// <returns></returns>
private static Tuple<string, string[]> CreateTuple(string key, params string[] values)
{
return new Tuple<string, string[]>(key, values);
}


private static AspNetQueryStringLayoutRenderer CreateAndMockRenderer(params Tuple<string, string[]>[] values)
{
var renderer = new AspNetQueryStringLayoutRenderer();

#if !NETSTANDARD_1plus

var httpContext = Substitute.For<HttpContextBase>();
var namedClollection = new NameValueCollection();
foreach (var tuple in values)
{
foreach (var value in tuple.Item2)
{
namedClollection.Add(tuple.Item1, value);
}

}

httpContext.Request.QueryString.Returns(namedClollection);

renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
#else
var httpContext = Substitute.For<HttpContextBase>();
var dict = new Dictionary<string, StringValues>();
foreach (var tuple in values)
{
dict.Add(tuple.Item1, new StringValues(tuple.Item2));

}
IQueryCollection querystringValues = new QueryCollection(dict);

httpContext.Request.Query.Returns(querystringValues);

renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
#endif

return renderer;



}


}
}
#endif
Loading

0 comments on commit 92707ee

Please sign in to comment.