ASP.NET + NUnit:使用.NET 4的HttpModule的良好单元测试策略 [英] ASP.NET + NUnit : Good unit testing strategy for HttpModule using .NET 4

查看:71
本文介绍了ASP.NET + NUnit:使用.NET 4的HttpModule的良好单元测试策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下我想进行单元测试的HttpModule。问题是我不允许更改访问修饰符/静态,因为它们必须保持原样。我想知道什么是测试以下模块的最佳方法。我仍然在测试方面还很陌生,主要是寻找有关测试策略的提示以及一般测试HttpModules的技巧。为了澄清起见,我只是尝试获取每个请求的URL(仅.aspx页),并检查所请求的URL是否具有许可(针对我们Intranet中的特定用户)。到目前为止,感觉好像我还不能真正测试该模块(从生产角度来看)。

I have the following HttpModule that I wanted to unit test. Problem is I am not allowed to change the access modifiers/static as they need to be as it is. I was wondering what would be the best method to test the following module. I am still pretty new in testing stuff and mainly looking for tips on testing strategy and in general testing HttpModules. Just for clarification, I am just trying to grab each requested URL(only .aspx pages) and checking if the requested url has permission (for specific users in our Intranet). So far it feels like I can't really test this module(from productive perspective).

public class PageAccessPermissionCheckerModule : IHttpModule
    {
        [Inject]
        public IIntranetSitemapProvider SitemapProvider { get; set; }
        [Inject]
        public IIntranetSitemapPermissionProvider PermissionProvider { get; set; }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += ValidatePage;
        }

        private void EnsureInjected()
        {
            if (PermissionProvider == null)
                KernelContainer.Inject(this);
        }

        private void ValidatePage(object sender, EventArgs e)
        {
            EnsureInjected();

            var context = HttpContext.Current ?? ((HttpApplication)sender).Context;

            var pageExtension = VirtualPathUtility.GetExtension(context.Request.Url.AbsolutePath);

            if (context.Session == null || pageExtension != ".aspx") return;

            if (!UserHasPermission(context))
            {
                KernelContainer.Get<UrlProvider>().RedirectToPageDenied("Access denied: " + context.Request.Url);
            }
        }

        private bool UserHasPermission(HttpContext context)
        {
            var permissionCode = FindPermissionCode(SitemapProvider.GetNodes(), context.Request.Url.PathAndQuery);

            return PermissionProvider.UserHasPermission(permissionCode);
        }

        private static string FindPermissionCode(IEnumerable<SitemapNode> nodes, string requestedUrl)
        {
            var matchingNode = nodes.FirstOrDefault(x => ComparePaths(x.SiteURL, requestedUrl));

            if (matchingNode != null)
                return matchingNode.PermissionCode;

            foreach(var node in nodes)
            {
                var code = FindPermissionCode(node.ChildNodes, requestedUrl);
                if (!string.IsNullOrEmpty(code))
                    return code;
            }

            return null;
        }  
        public void Dispose() { }
    }


推荐答案

测试HttpHandler可能很棘手。我建议您创建第二个库,并将要测试的功能放在该库中。这样也可以使您更好地分离问题。

Testing HttpHandlers can be tricky. I would recommend you create a second library and place the functionality you want to test there. This would also get you a better separation of concerns.

这篇关于ASP.NET + NUnit:使用.NET 4的HttpModule的良好单元测试策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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