【问题标题】:Unit testing an event in HttpModule对 HttpModule 中的事件进行单元测试
【发布时间】:2018-03-25 18:35:37
【问题描述】:

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

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

我需要为此编写单元测试。

尝试了以下几个但无法做到这一点,出现错误。

 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 中设置上下文时出错。 请你帮帮我

【问题讨论】:

    标签: asp.net-mvc unit-testing c#-4.0


    【解决方案1】:

    根据 Microsoft 的 guidance,您不应该使用 OnPreSendRequestHeaders 事件

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

    如果您可以转移到不同的活动,则可以使用here 显示的技术

    哪个链接到这个旧的article

    您创建一个 BaseHttpModule 以创建事件,您可以在其中将上下文传递给处理程序

    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.
    

    【讨论】:

    • OP 使用的是HttpModule,而不是Controller
    • 非常感谢您的回复。有一个查询..我是否需要附加除 OnBeginRequest 之外的其他事件(OnError,OnEndRequest),因为我仅在 RemoveServerHeadersModule 中使用 OnBeginRequest。请告诉我
    • 除非你打算使用它们,否则你不应该实现其他偶数处理程序
    • 在将事件从 OnPreSendRequestHeaders 修改为 OnBeginRequest 后,仅从标头中删除了“Server”键。在调试 Web api 响应时,我只能看到响应中存在“Server”键。请建议如何修改以获取 Web api 响应中的所有服务器标头。
    • 如果您真正需要做的就是删除那些 asp.net 标头,您可以使用 web.config 来完成,而无需编写自定义处理程序。 stackoverflow.com/questions/4078756/…
    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多