-
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 #27 from petemounce/headers
Add ability to render Headers
- Loading branch information
Showing
5 changed files
with
134 additions
and
26 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
66 changes: 66 additions & 0 deletions
66
NLog.Web.Tests/LayoutRenderers/TestInvolvingAspNetHttpContext.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,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); | ||
} | ||
} | ||
} |
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