我正在使用带有.Net 4.6.1的VS 2015。 我试图为一段代码编写集成测试,但是它给了我以上错误。 我无法理解。此方法位于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]);
}
}
我收到的错误是在 if 条件语句中,它试图获取 Response.Headers 的特定标头,错误是什么?任何想法吗?为什么 Test 需要 IIS?
此操作需要 IIS 集成管道模式