c# - 集成测试 - 此操作需要 IIS 集成管道模式

标签 c# asp.net http iis integration-testing

我正在使用 VS 2015 和 .Net 4.6.1。我正在尝试为代码编写集成测试,但它给了我上述错误。我无法理解。这个方法在global.asax中

public void Application_EndRequest(object sender, EventArgs e)
        {
            AddCorsResponseHeadersForUnauthorizedRequests(Response, Request);
        }

        public static void AddCorsResponseHeadersForUnauthorizedRequests(HttpResponse response, HttpRequest request)
        {
            var origin = request.Params[AppConstant.RequestHttpOrigin];
            if (response.StatusCode == (int) HttpStatusCode.Unauthorized &&
                string.IsNullOrEmpty(response.Headers[AppConstant.AccessControlAllowOrigin]) &&
                !string.IsNullOrEmpty(origin))
            {
                response.AddHeader(AppConstant.AccessControlAllowOrigin, WebApiConfig.GetCorsAllowedOrigin());
                response.AddHeader(AppConstant.AccessControlAllowCredentials, "true");
            }
        }

IntegrationTest.cs

[TestFixture]
    public class AuthenticationResponseHeadersTests
    {
        private WebApiApplication systemUnderTest;
        private HttpRequest httpRequest;
        private HttpResponse httpResponse;

        [SetUp]
        public void Setup()
        {
            systemUnderTest = new WebApiApplication();
            httpRequest = new HttpRequest(string.Empty, "http://localhost:5001/", string.Empty);
            httpResponse = new HttpResponse(TextWriter.Null);
            httpResponse.AddHeader("Connection", "keep-alive");
        }

        [Test]
        public void ShouldAddCorsResponseHeaders()
        {
            httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
            WebApiApplication.AddCorsResponseHeadersForUnauthorizedRequests(httpResponse, httpRequest);

            Assert.AreEqual("http://localhost:5001", httpResponse.Headers[AppConstant.AccessControlAllowOrigin]);
            Assert.AreEqual("true", httpResponse.Headers[AppConstant.AccessControlAllowCredentials]);
        }
    }

我得到的错误是在它试图获取 Response.Headers 的特定 header 和错误的情况下,知道吗?为什么需要IIS来测试?

This operation requires IIS integrated pipeline mode

enter image description here

最佳答案

经典模式(IIS6及以下版本的唯一模式)是IIS只与ISAPI扩展一起工作的模式

另一方面,集成模式是 IIS7 中的一种新模式,其中 IIS 管道与 ASP.NET 请求管道紧密集成(即完全相同)

在解决方案资源管理器中选择 Web 应用程序项目节点并按 F4 并更改您的管道 enter image description here

顺便说一句,因为您使用的是 Web API,所以无需担心 HttpModule 和 HttpHandlers,因为它们的部分已在集成管道模式下的 Web 配置中更改。

更新 我会建议使用 IHttpContext 并编写您自己的带有接口(interface)的包装器,例如 IHttpContext。然后你就拥有自己的 HttpContext 并将所有调用委托(delegate)给它。然后在您的应用程序中,每个人都使用该界面。这解决了您与 Microsoft 密封类交互的问题,因为您可以替代模拟或 stub 或其他任何内容。

检查此链接: http://haacked.com/archive/2007/09/09/ihttpcontext-and-other-interfaces-for-your-duck-typing-benefit.aspx/

关于c# - 集成测试 - 此操作需要 IIS 集成管道模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016899/

相关文章:

asp.net - 如何在 ASP 中制作可扩展的图像按钮

php - 使用 HTTP 身份验证以编程方式下载文件

php - 无法模拟 HTTP 302 响应

c# - 求一个ASP.Net的代码分析工具

c# - 如何从 Entity Framework 中的多个数据库/表创建单个对象

c# - 我应该在每个类还是在基类中声明一次 log4net 记录器?

c# - 创建具有定义的文件大小的 SQL Server 数据库

c# - MySql gridview删除命令

javascript - 确定哪些控件在 ASP.NET 网页上失败

java - 响应200正常,然后转到错误行