我正在使用 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
最佳答案
经典模式(IIS6及以下版本的唯一模式)是IIS只与ISAPI扩展一起工作的模式
另一方面,集成模式是 IIS7 中的一种新模式,其中 IIS 管道与 ASP.NET 请求管道紧密集成(即完全相同)
在解决方案资源管理器中选择 Web 应用程序项目节点并按 F4 并更改您的管道
顺便说一句,因为您使用的是 Web API,所以无需担心 HttpModule 和 HttpHandlers,因为它们的部分已在集成管道模式下的 Web 配置中更改。
更新 我会建议使用 IHttpContext 并编写您自己的带有接口(interface)的包装器,例如 IHttpContext。然后你就拥有自己的 HttpContext 并将所有调用委托(delegate)给它。然后在您的应用程序中,每个人都使用该界面。这解决了您与 Microsoft 密封类交互的问题,因为您可以替代模拟或 stub 或其他任何内容。
关于c# - 集成测试 - 此操作需要 IIS 集成管道模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016899/