问题
I have an CustomHttp module in my application to remove the unwanted response headers as below.
public class RemoveServerHeadersModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
public void OnPreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Remove("X-Powered-By");
HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
HttpContext.Current.Response.Headers.Remove("Server");
}
}
I need to write unit test for this .
Tried few like below but unable to do that , getting error.
HttpRequest httpRequest = new HttpRequest("", "", "");
StringWriter stringWriter = new StringWriter();
HttpResponse httpResponse = new HttpResponse(stringWriter);
HttpContext httpContextMock = new HttpContext(httpRequest, httpResponse);
var application = new Mock<HttpApplication>();
application.Setup(ct => ct.Context).Returns(httpContextMock);
var module = new RemoveServerHeadersModule();
HttpApplication httpApplication = new HttpApplication();
module.Init(httpApplication);
module.OnPreSendRequestHeaders(httpApplication, EventArgs.Empty);
Getting error when trying to set the Context in HttpApplication. Please could you help me out
回答1:
Per guidance by Microsoft you shouldn't be using the OnPreSendRequestHeaders event
You can use the PreSendRequestHeaders and PreSendRequestContext events with native IIS modules, but do not use them with managed modules that implement IHttpModule. Setting these properties can cause issues with asynchronous requests.
If you can move to a different event, you can then use the techniques shown here
Which links to this old article
where you create a BaseHttpModule to then create events where you can pass the context into the handler
public abstract class BaseHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) => OnBeginRequest(new HttpContextWrapper(((HttpApplication) sender).Context));
context.Error += (sender, e) => OnError(new HttpContextWrapper(((HttpApplication) sender).Context));
context.EndRequest += (sender, e) => OnEndRequest(new HttpContextWrapper(((HttpApplication) sender).Context));
}
public void Dispose()
{
}
public virtual void OnBeginRequest(HttpContextBase context)
{
}
public virtual void OnError(HttpContextBase context)
{
}
public virtual void OnEndRequest(HttpContextBase context)
{
}
}
Then your class becomes
public class RemoveServerHeadersModule : BaseHttpModule
{
public override void OnBeginRequest(HttpContextBase context)
{
context.Response.Headers.Remove("X-Powered-By");
context.Response.Headers.Remove("X-AspNet-Version");
context.Response.Headers.Remove("X-AspNetMvc-Version");
context.Response.Headers.Remove("Server");
}
}
and your test is something like
var fakeContext = A.Fake<HttpContextBase>();
var fakeRequest = A.Fake<HttpRequestBase>();
var fakeResponse = A.Fake<HttpResponseBase>();
A.CallTo(() => fakeResponse.Headers).Returns( /*put your headers here*/);
A.CallTo(() => fakeContext.Response).Returns(fakeResponse);
A.CallTo(() => fakeContext.Request).Returns(fakeRequest);
var _sut = new RemoveServerHeadersModule();
_sut.OnBeginRequest(fakeContext);
//your header checks.
来源:https://stackoverflow.com/questions/46733045/unit-testing-an-event-in-httpmodule