Kamran Pervaiz
Kamran Pervaiz

Reputation: 1931

Integration Test - This operation requires IIS integrated pipeline mode

I am using VS 2015 with.Net 4.6.1. I am trying to write an integration test for a code but it's giving me above error. I cant get my head around. This method is in 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]);
        }
    }

The error I get is on if condition where it's trying to get the specific header for the Response.Headers and the error are, any idea? why does it need IIS for Test?

This operation requires IIS integrated pipeline mode

enter image description here

Upvotes: 3

Views: 2341

Answers (1)

Pouya Samie
Pouya Samie

Reputation: 3723

Classic mode (the only mode in IIS6 and below) is a mode where IIS only works with ISAPI extensions

Integrated mode, on the other hand, is a new mode in IIS7 where IIS pipeline is tightly integrated (i.e. is just the same) as ASP.NET request pipeline

select the Web Application project node in Solution Explorer and Press F4 and change your Pipeline enter image description here

By the way, because you are using Web API there is no need to be worry about HttpModule and HttpHandlers because their section has been changed in web configure in Integrated pipeline mode.

UPDATE I will suggest Use IHttpContext and write your own wrapper with an interface for them like say IHttpContext. Then you just have your own HttpContext and delegate all the calls to it. Then in your app everyone uses the interface. This fixes your problem with interacting with Microsofts sealed classes because you can substitute mocks or stubs or whatever.

Check This link : http://haacked.com/archive/2007/09/09/ihttpcontext-and-other-interfaces-for-your-duck-typing-benefit.aspx/

Upvotes: 1

Related Questions