public void GetOwinResponse_ReturnsResponseForOwinEnvironment()
        {
            // Arrange
            IDictionary<string, object> expectedEnvironment = new Dictionary<string, object>();

            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                request.SetOwinEnvironment(expectedEnvironment);

                // Act
                OwinResponse response = request.GetOwinResponse();

                // Assert
                Assert.Same(expectedEnvironment, response.Environment);
            }
        }
 public void GetOwinResponse_Throws_WhenOwinEnvironmentNotSet()
 {
     // Arrange
     using (HttpRequestMessage request = new HttpRequestMessage())
     {
         // Act & Assert
         Assert.Throws<InvalidOperationException>(() => { request.GetOwinResponse(); },
             "No OWIN environment is available for the request.");
     }
 }
        private static void SuppressDefaultAuthenticationChallenges(HttpRequestMessage request)
        {
            Contract.Assert(request != null);

            OwinResponse response = request.GetOwinResponse();

            AuthenticationResponseChallenge currentChallenge = response.AuthenticationResponseChallenge;

            // A null challenge or challenge.AuthenticationTypes == null or empty represents the the default behavior
            // of running all active authentication middleware challenges.
            // Provide an array with a single null item to suppress this default behavior.
            string[] suppressAuthenticationTypes = new string[] { null };

            if (currentChallenge == null)
            {
                response.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(
                    suppressAuthenticationTypes, new AuthenticationExtra());
            }
            else if (currentChallenge.AuthenticationTypes == null || currentChallenge.AuthenticationTypes.Length == 0)
            {
                response.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(
                    suppressAuthenticationTypes, currentChallenge.Extra);
            }
        }