public void EvaluateSucceedsWhenNotEnabled()
        {
            var mockActionDescriptor = new Mock <HttpActionDescriptor>();
            var httpConfiguration    = new HttpConfiguration();
            var routeData            = new HttpRouteData(new HttpRoute());
            var request = new HttpRequestMessage();
            var controllerDescriptor = new HttpControllerDescriptor {
                Configuration = httpConfiguration, ControllerName = "generic"
            };
            var controllerContext = new HttpControllerContext(httpConfiguration, routeData, request)
            {
                ControllerDescriptor = controllerDescriptor
            };
            var actionContext = new HttpActionContext(controllerContext, mockActionDescriptor.Object);
            var config        = new RequireSslAuthorizationPolicyConfiguration {
                Enabled = false
            };
            var policy = new RequireSslAuthorizationPolicy(config);

            mockActionDescriptor.SetupGet(descriptor => descriptor.ActionName).Returns("someAction");

            request.RequestUri = new Uri("http://www.someServer.com");
            request.SetConfiguration(httpConfiguration);
            request.SetRouteData(routeData);

            policy.Evaluate(actionContext).Should().BeNull("because the policy should always be satisfied if not enabled");
        }
        public void EvaluateSucceedsForSecureLocalRequestWhenTheExceptionIsDisallowed()
        {
            var mockActionDescriptor = new Mock <HttpActionDescriptor>();
            var httpConfiguration    = new HttpConfiguration();
            var routeData            = new HttpRouteData(new HttpRoute());
            var request = new HttpRequestMessage();
            var controllerDescriptor = new HttpControllerDescriptor {
                Configuration = httpConfiguration, ControllerName = "generic"
            };
            var controllerContext = new HttpControllerContext(httpConfiguration, routeData, request)
            {
                ControllerDescriptor = controllerDescriptor
            };
            var actionContext = new HttpActionContext(controllerContext, mockActionDescriptor.Object);
            var config        = new RequireSslAuthorizationPolicyConfiguration {
                Enabled = true, AllowLoopbackException = false
            };
            var policy = new RequireSslAuthorizationPolicy(config);

            mockActionDescriptor.SetupGet(descriptor => descriptor.ActionName).Returns("someAction");

            request.RequestUri = new Uri("https://localhost/some-endpoint");
            request.SetConfiguration(httpConfiguration);
            request.SetRouteData(routeData);

            policy.Evaluate(actionContext).Should().BeNull("because the policy should be satisfied for a secure local request when the exception is disallowed");
        }
        public void EvaluateFailsForUnsecuredRequest()
        {
            var mockActionDescriptor = new Mock <HttpActionDescriptor>();
            var httpConfiguration    = new HttpConfiguration();
            var routeData            = new HttpRouteData(new HttpRoute());
            var request = new HttpRequestMessage();
            var controllerDescriptor = new HttpControllerDescriptor {
                Configuration = httpConfiguration, ControllerName = "generic"
            };
            var controllerContext = new HttpControllerContext(httpConfiguration, routeData, request)
            {
                ControllerDescriptor = controllerDescriptor
            };
            var actionContext = new HttpActionContext(controllerContext, mockActionDescriptor.Object);
            var config        = new RequireSslAuthorizationPolicyConfiguration {
                Enabled = true, AllowLoopbackException = true
            };
            var policy = new RequireSslAuthorizationPolicy(config);

            mockActionDescriptor.SetupGet(descriptor => descriptor.ActionName).Returns("someAction");

            request.RequestUri = new Uri("http://api.someserver.com/some-endpoint");
            request.SetConfiguration(httpConfiguration);
            request.SetRouteData(routeData);

            policy.Evaluate(actionContext).Should().Be(HttpStatusCode.Forbidden, "because the policy should fail for a request that is not local or over SSL.");
        }
        public void EnabledPropertyIsConfigured()
        {
            var config = new RequireSslAuthorizationPolicyConfiguration {
                Enabled = true
            };
            var policy = new RequireSslAuthorizationPolicy(config);

            policy.Enabled.Should().Be(config.Enabled, "because the Enabled property should be driven by configuration");
        }
Exemplo n.º 5
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="RequireSslAuthorizationPolicy"/> class.
 /// </summary>
 ///
 /// <param name="configuration">The configuration to use for the policy.</param>
 ///
 public RequireSslAuthorizationPolicy(RequireSslAuthorizationPolicyConfiguration configuration)
 {
     this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
 }