Skip to content

Commit

Permalink
Merge pull request #27 from petemounce/headers
Browse files Browse the repository at this point in the history
Add ability to render Headers
  • Loading branch information
304NotModified committed Feb 4, 2016
2 parents 9e7d12c + 89a28f5 commit 7a30535
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,53 @@ public void KeyFoundRendersValue()
}
}

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

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.Header = null;

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

Assert.Empty(result);
}

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

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.Header = "key";

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

Assert.Empty(result);
}

[Fact]
public void KeyFoundRendersValue()
{
var expectedResult = "value";
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Request.Headers.Returns(new NameValueCollection { { "key", expectedResult } });

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.Header = "key";

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

Assert.Equal(expectedResult, result);
}
}

public class FormTests
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.SessionState;
Expand All @@ -12,10 +9,8 @@

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetSessionValueLayoutRendererTests : IDisposable
public class AspNetSessionValueLayoutRendererTests : TestInvolvingAspNetHttpContext
{


public AspNetSessionValueLayoutRendererTests()
{
SetUp();
Expand All @@ -26,9 +21,8 @@ public void SetUp()
SetupFakeSession();
}

public void CleanUp()
protected override void CleanUp()
{

Session.Clear();
}

Expand Down Expand Up @@ -216,35 +210,19 @@ private void ExecTest(string key, object value, object expected, LayoutRenderer
/// <summary>
/// Create Fake Session http://stackoverflow.com/a/10126711/201303
/// </summary>
public static void SetupFakeSession()
public void SetupFakeSession()
{
var httpRequest = new HttpRequest("", "http://stackoverflow/", "");
var stringWriter = new StringWriter();
var httpResponse = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponse);

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

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

HttpContext.Current = httpContext;

}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
CleanUp();
}
}
}
66 changes: 66 additions & 0 deletions NLog.Web.Tests/LayoutRenderers/TestInvolvingAspNetHttpContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Web;

namespace NLog.Web.Tests.LayoutRenderers
{
public abstract class TestInvolvingAspNetHttpContext : IDisposable
{
protected HttpContext HttpContext;

protected TestInvolvingAspNetHttpContext()
{
HttpContext = SetupFakeHttpContext();
HttpContext.Current = HttpContext;
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
CleanUp();
}

protected virtual void CleanUp()
{
}

protected HttpContext SetupFakeHttpContext()
{
var httpRequest = SetUpHttpRequest();
var stringWriter = new StringWriter();
var httpResponse = new HttpResponse(stringWriter);
return new HttpContext(httpRequest, httpResponse);
}

protected virtual HttpRequest SetUpHttpRequest(string query = "")
{
return new HttpRequest("", "http://stackoverflow/", query);
}

protected void AddHeader(HttpRequest request, string headerName, string headerValue)
{
// thanks http://stackoverflow.com/a/13307238
var headers = request.Headers;
var t = headers.GetType();
var item = new ArrayList();

t.InvokeMember("MakeReadWrite", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null,
headers, null);
t.InvokeMember("InvalidateCachedArrays",
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null, headers, null);
item.Add(headerValue);
t.InvokeMember("BaseAdd", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null,
headers,
new object[] {headerName, item});
t.InvokeMember("MakeReadOnly", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null,
headers, null);
}
}
}
1 change: 1 addition & 0 deletions NLog.Web.Tests/NLog.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="LayoutRenderers\AspNetSessionIDLayoutRendererTests.cs" />
<Compile Include="LayoutRenderers\AspNetSessionValueLayoutRendererTests.cs" />
<Compile Include="LayoutRenderers\AspNetUserAuthTypeLayoutRendererTests.cs" />
<Compile Include="LayoutRenderers\TestInvolvingAspNetHttpContext.cs" />
<Compile Include="LayoutRenderers\AspNetUserIdentityLayoutRendererTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace NLog.Web.LayoutRenderers
/// ${aspnet-request:querystring=v}
/// ${aspnet-request:form=v}
/// ${aspnet-request:cookie=v}
/// ${aspnet-request:header=h}
/// ${aspnet-request:serverVariable=v}
/// </code>
/// </example>
Expand Down Expand Up @@ -56,6 +57,12 @@ public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase
/// <docgen category='Rendering Options' order='10' />
public string ServerVariable { get; set; }

/// <summary>
/// Gets or sets the Headers item to be rendered.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public string Header { get; set; }

/// <summary>
/// Renders the specified ASP.NET Request variable and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
Expand Down Expand Up @@ -91,6 +98,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append(httpRequest.ServerVariables[this.ServerVariable]);
}
else if (this.Header != null)
{
string header = httpRequest.Headers[this.Header];

if (header != null)
{
builder.Append(header);
}
}
else if (this.Item != null)
{
builder.Append(httpRequest[this.Item]);
Expand Down

0 comments on commit 7a30535

Please sign in to comment.