在HttpModule中对事件进行单元测试 [英] Unit testing an event in HttpModule

查看:83
本文介绍了在HttpModule中对事件进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个CustomHttp模块,用于删除不需要的响应标头,如下所示。

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);

尝试在HttpApplication中设置上下文时出错。
请您帮帮我

Getting error when trying to set the Context in HttpApplication. Please could you help me out

推荐答案

由Microsoft提供的指南,您不应使用OnPreSendRequestHeaders事件

Per guidance by Microsoft you shouldn't be using the OnPreSendRequestHeaders event


您可以使用PreSendRequestHeaders和PreSendRequestContext事件
与本机IIS模块一起使用,但不要与
实现IHttpModule的托管模块一起使用。设置这些属性可能会导致
异步请求出现问题。

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

与此旧文章

在其中创建BaseHttpModule,然后创建事件将上下文传递到处理程序中

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)
    {
    }
}

然后您的课程变为

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");
    }
}

您的测试类似于

        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.

这篇关于在HttpModule中对事件进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆