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