예제 #1
0
        public void GetClientValidationRules_WithActionControllerArea_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "Test")
            {
                HttpMethod = "POST",
            };
            var url       = "/Test/Controller/Action";
            var urlHelper = new MockUrlHelper(url, routeName: null);
            var context   = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(3, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("POST", rule.ValidationParameters["type"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType <RouteValueDictionary>(urlHelper.RouteValues);

            Assert.Equal(3, routeDictionary.Count);
            Assert.Equal("Action", routeDictionary["action"] as string);
            Assert.Equal("Controller", routeDictionary["controller"] as string);
            Assert.Equal("Test", routeDictionary["area"] as string);
        }
예제 #2
0
        public void GetClientValidationRules_WithActionController_NoController_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller");
            var context   = GetValidationContextWithNoController();

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() => attribute.GetClientValidationRules(context));

            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
예제 #3
0
        public void GetClientValidationRules_WithBadRouteName_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("nonexistentRoute");
            var context   = GetValidationContextWithArea(currentArea: null);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() => attribute.GetClientValidationRules(context));

            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
예제 #4
0
        public void GetClientValidationRules_WithActionControllerAreaInArea_FindsControllerInDifferentArea()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "AnotherArea");
            var context   = GetValidationContextWithArea(currentArea: "Test");

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/AnotherArea/Controller/Action", rule.ValidationParameters["url"]);
        }
예제 #5
0
        public void GetClientValidationRules_WithActionControllerArea_FindsControllerInRootArea(string areaName)
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", areaName);
            var context   = GetValidationContextWithArea(currentArea: null);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/UrlEncode[[Controller]]/UrlEncode[[Action]]", rule.ValidationParameters["url"]);
        }
예제 #6
0
        public void GetClientValidationRules_WithRoute_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var routeName = "RouteName";
            var attribute = new RemoteAttribute(routeName);
            var url       = "/my/URL";
            var urlHelper = new MockUrlHelper(url, routeName);
            var context   = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));

            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType <RouteValueDictionary>(urlHelper.RouteValues);

            Assert.Empty(routeDictionary);
        }
예제 #7
0
        public void GetClientValidationRules_WithActionControllerAreaInArea_FindsControllerInDifferentArea()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "AnotherArea");
            var context = GetValidationContextWithArea(currentArea: "Test");

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/AnotherArea/Controller/Action", rule.ValidationParameters["url"]);
        }
예제 #8
0
        public void GetClientValidationRules_WithActionControllerArea_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", "Test")
            {
                HttpMethod = "POST",
            };
            var url = "/Test/Controller/Action";
            var urlHelper = new MockUrlHelper(url, routeName: null);
            var context = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(3, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("POST", rule.ValidationParameters["type"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType<RouteValueDictionary>(urlHelper.RouteValues);
            Assert.Equal(3, routeDictionary.Count);
            Assert.Equal("Action", routeDictionary["action"] as string);
            Assert.Equal("Controller", routeDictionary["controller"] as string);
            Assert.Equal("Test", routeDictionary["area"] as string);
        }
예제 #9
0
        public void GetClientValidationRules_WithRoute_CallsUrlHelperWithExpectedValues()
        {
            // Arrange
            var routeName = "RouteName";
            var attribute = new RemoteAttribute(routeName);
            var url = "/my/URL";
            var urlHelper = new MockUrlHelper(url, routeName);
            var context = GetValidationContext(urlHelper);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal(url, rule.ValidationParameters["url"]);

            var routeDictionary = Assert.IsType<RouteValueDictionary>(urlHelper.RouteValues);
            Assert.Empty(routeDictionary);
        }
예제 #10
0
        public void GetClientValidationRules_WithActionController_NoController_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller");
            var context = GetValidationContextWithNoController();

            // Act & Assert
            var exception = Assert.Throws<InvalidOperationException>(() => attribute.GetClientValidationRules(context));
            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
예제 #11
0
        public void GetClientValidationRules_WithBadRouteName_Throws()
        {
            // Arrange
            var attribute = new RemoteAttribute("nonexistentRoute");
            var context = GetValidationContextWithArea(currentArea: null);

            // Act & Assert
            var exception = Assert.Throws<InvalidOperationException>(() => attribute.GetClientValidationRules(context));
            Assert.Equal("No URL for remote validation could be found.", exception.Message);
        }
예제 #12
0
        public void GetClientValidationRules_WithActionControllerArea_FindsControllerInRootArea(string areaName)
        {
            // Arrange
            var attribute = new RemoteAttribute("Action", "Controller", areaName);
            var context = GetValidationContextWithArea(currentArea: null);

            // Act & Assert
            var rule = Assert.Single(attribute.GetClientValidationRules(context));
            Assert.Equal("remote", rule.ValidationType);
            Assert.Equal("'Length' is invalid.", rule.ErrorMessage);

            Assert.Equal(2, rule.ValidationParameters.Count);
            Assert.Equal("*.Length", rule.ValidationParameters["additionalfields"]);
            Assert.Equal("/UrlEncode[[Controller]]/UrlEncode[[Action]]", rule.ValidationParameters["url"]);
        }