Validator class containing dynamic property calls validation logic.
        private static AuthenticationProperties TryGetAuthenticationProperties(dynamic actionResult)
        {
            AuthenticationProperties authenticationProperties = null;

            RuntimeBinderValidator.ValidateBinding(() =>
            {
                authenticationProperties = (AuthenticationProperties)actionResult.Properties;
            });

            return(authenticationProperties);
        }
        private static IEnumerable <string> TryGetContentTypesCollection(dynamic actionResult)
        {
            MediaTypeCollection contentTypes = null;

            RuntimeBinderValidator.ValidateBinding(() =>
            {
                contentTypes = (MediaTypeCollection)actionResult.ContentTypes;
            });

            return(contentTypes);
        }
        private static IList <string> TryGetAuthenticationSchemes(dynamic actionResult)
        {
            IList <string> authenticationSchemes = new List <string>();

            RuntimeBinderValidator.ValidateBinding(() =>
            {
                authenticationSchemes = (IList <string>)actionResult.AuthenticationSchemes;
            });

            return(authenticationSchemes);
        }
 /// <summary>
 /// Validates whether StatusCode is the same as the provided one from action result containing such property.
 /// </summary>
 /// <param name="result">Component result with StatusCode.</param>
 /// <param name="expectedHttpStatusCode">Expected HTTP status code.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateHttpStatusCode(
     dynamic result,
     HttpStatusCode expectedHttpStatusCode,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         ValidateHttpStatusCode(
             expectedHttpStatusCode,
             (int?)result.StatusCode,
             failedValidationAction);
     });
 }
 /// <summary>
 /// Validates whether ContentType is the same as the provided one from action result containing such property.
 /// </summary>
 /// <param name="result">Component result with ContentType.</param>
 /// <param name="expectedContentType">Expected content type.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateContentType(
     dynamic result,
     string expectedContentType,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         ValidateContentType(
             expectedContentType,
             (string)result.ContentType,
             failedValidationAction);
     });
 }
        public static void ValidateRouteValueOfType <TRouteValue>(
            dynamic actionResult,
            Action <string, string, string> failedValidationAction)
        {
            RuntimeBinderValidator.ValidateBinding(() =>
            {
                var routeValues = (IDictionary <string, object>)actionResult.RouteValues;

                DictionaryValidator.ValidateValueOfType <TRouteValue>(
                    RouteValuesName,
                    routeValues,
                    failedValidationAction);
            });
        }
 /// <summary>
 /// Validates whether UrlHelper contains the same value as the provided one from action result containing such property.
 /// </summary>
 /// <param name="actionResult">Action result with UrlHelper.</param>
 /// <param name="urlHelper">Expected URL helper.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateUrlHelper(
     dynamic actionResult,
     IUrlHelper urlHelper,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         if (urlHelper != (IUrlHelper)actionResult.UrlHelper)
         {
             failedValidationAction(
                 "UrlHelper",
                 "to be the same as the provided one",
                 "instead received different result");
         }
     });
 }
 /// <summary>
 /// Validates whether ControllerName is the same as the provided one from action result containing such property.
 /// </summary>
 /// <param name="actionResult">Action result with ControllerName.</param>
 /// <param name="controllerName">Expected controller name.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateControllerName(
     dynamic actionResult,
     string controllerName,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         var actualControllerName = (string)actionResult.ControllerName;
         if (controllerName != actualControllerName)
         {
             failedValidationAction(
                 "to have",
                 $"'{controllerName}' controller name",
                 $"instead received {actualControllerName.GetErrorMessageName()}");
         }
     });
 }
Esempio n. 9
0
 /// <summary>
 /// Validates the Uri from action result containing one.
 /// </summary>
 /// <param name="result">Component result with Uri.</param>
 /// <param name="location">Expected Uri.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateUri(
     dynamic result,
     string location,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         var actualLocation = (string)GetUrlFromDynamic(result);
         if (location != actualLocation)
         {
             failedValidationAction(
                 "location",
                 $"to be '{location}'",
                 $"instead received '{actualLocation}'");
         }
     });
 }
        /// <summary>
        /// Validates whether RouteName is the same as the provided one from action result containing such property.
        /// </summary>
        /// <param name="actionResult">Action result with RouteName.</param>
        /// <param name="routeName">Expected route name.</param>
        /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
        public static void ValidateRouteName(
            dynamic actionResult,
            string routeName,
            Action <string, string, string> failedValidationAction)
        {
            RuntimeBinderValidator.ValidateBinding(() =>
            {
                var actualRouteName = (string)actionResult.RouteName;

                if (routeName != actualRouteName)
                {
                    failedValidationAction(
                        "to have",
                        $"'{routeName}' route name",
                        $"instead received {actualRouteName.GetErrorMessageName()}");
                }
            });
        }
 /// <summary>
 /// Validates whether UrlHelper contains the same type as the provided one from action result containing such property.
 /// </summary>
 /// <typeparam name="TUrlHelper">Type of IUrlHelper.</typeparam>
 /// <param name="actionResult">Action result with UrlHelper.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateUrlHelperOfType <TUrlHelper>(
     dynamic actionResult,
     Action <string, string, string> failedValidationAction)
     where TUrlHelper : IUrlHelper
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         var actualUrlHelper = (IUrlHelper)actionResult.UrlHelper;
         if (actualUrlHelper == null ||
             Reflection.AreDifferentTypes(typeof(TUrlHelper), actualUrlHelper.GetType()))
         {
             failedValidationAction(
                 "UrlHelper",
                 $"to be of {typeof(TUrlHelper).Name} type",
                 $"instead received {actualUrlHelper.GetName()}");
         }
     });
 }
        /// <summary>
        /// Validates whether RouteValues contains the same route entries as the provided ones from action result containing such property.
        /// </summary>
        /// <param name="actionResult">Action result with RouteValues.</param>
        /// <param name="routeValues">Expected route value dictionary.</param>
        /// <param name="includeCountCheck">Specifies whether the total number of route values should be validated.</param>
        /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
        public static void ValidateRouteValues(
            dynamic actionResult,
            IDictionary <string, object> routeValues,
            bool includeCountCheck,
            Action <string, string, string> failedValidationAction)
        {
            RuntimeBinderValidator.ValidateBinding(() =>
            {
                var actualRouteValues = (IDictionary <string, object>)actionResult.RouteValues;

                DictionaryValidator.ValidateValues(
                    RouteValuesName,
                    actualRouteValues,
                    routeValues,
                    failedValidationAction,
                    includeCountCheck);
            });
        }
        /// <summary>
        /// Validates whether RouteValues contains route entries with key and value as the provided ones from action result containing such property.
        /// </summary>
        /// <param name="actionResult">Action result with RouteValues.</param>
        /// <param name="key">Expected route entry key.</param>
        /// <param name="value">Expected route entry value.</param>
        /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
        public static void ValidateRouteValue(
            dynamic actionResult,
            string key,
            object value,
            Action <string, string, string> failedValidationAction)
        {
            RuntimeBinderValidator.ValidateBinding(() =>
            {
                var routeValues = (IDictionary <string, object>)actionResult.RouteValues;

                DictionaryValidator.ValidateStringKeyAndValue(
                    RouteValuesName,
                    routeValues,
                    key,
                    value,
                    failedValidationAction);
            });
        }
Esempio n. 14
0
        /// <summary>
        /// Validates URI by using UriTestBuilder.
        /// </summary>
        /// <param name="result">Dynamic representation of component result.</param>
        /// <param name="uriTestBuilder">UriTestBuilder for validation.</param>
        /// <param name="failedValidationAction">Action to execute, if the validation fails.</param>
        public static void ValidateLocation(
            dynamic result,
            Action <UriMockTestBuilder> uriTestBuilder,
            Action <string, string, string> failedValidationAction)
        {
            RuntimeBinderValidator.ValidateBinding(() =>
            {
                var actualUri = (string)GetUrlFromDynamic(result);

                var newUriTestBuilder = new UriMockTestBuilder();
                uriTestBuilder(newUriTestBuilder);
                var expectedUri = newUriTestBuilder.GetUriMock();

                var validations = newUriTestBuilder.GetUriMockValidations();
                var actualUriWithUnknownKind = new Uri(actualUri, UriKind.RelativeOrAbsolute);
                if (validations.Any(v => !v(expectedUri, actualUriWithUnknownKind)))
                {
                    failedValidationAction(
                        "URI",
                        $"to be '{expectedUri}'",
                        $"was in fact '{actualUriWithUnknownKind.OriginalString}'");
                }
            });
        }