【问题标题】:Integration Test - This operation requires IIS integrated pipeline mode集成测试 - 此操作需要 IIS 集成管道模式
【发布时间】:2016-12-07 12:00:24
【问题描述】:

我正在使用 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 的特定标头的情况下,并且错误是,知道吗?为什么需要 IIS 进行测试?

此操作需要IIS集成管道模式

【问题讨论】:

  • 你的管道是什么?
  • @PouyaSamie 我正在使用 IIsExpress,但我可以在完整的 II 上部署它并检查它是否修复了错误。

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


【解决方案1】:

经典模式(IIS6及以下的唯一模式)是IIS仅适用于ISAPI扩展的模式

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

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

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

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

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

【讨论】:

  • 我正在使用 IISExpress,我需要使用完整的 IIS 吗?好吧,一切都是本地主机,所以我可以尝试在 IIS 中创建一个网站。
  • 不只是 express iis 会做......改变管道,你应该没问题
  • 其实我只是验证了我确实在属性中集成了管道,匿名被禁用,windows被启用。
  • 我需要在 app.config 中写一些东西吗?在 integrationTest 项目中?
  • 我不想写这么多代码只是为了测试响应头......我想我可以摆脱它。 HttpRequest 和 HttpReponse 只是类,不应该依赖 IIS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多