/// <summary>
        /// Validates the Formatters from action result containing one.
        /// </summary>
        /// <param name="actionResult">Action result with Formatters.</param>
        /// <param name="mediaTypeFormatters">Expected enumerable of MediaTypeFormatter.</param>
        /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
        public static void ValidateMediaTypeFormatters(
            dynamic actionResult,
            IEnumerable <MediaTypeFormatter> mediaTypeFormatters,
            Action <string, string, string> failedValidationAction)
        {
            RuntimeBinderValidator.ValidateBinding(() =>
            {
                var formatters = TryGetMediaTypeFormatters(actionResult) as IEnumerable <MediaTypeFormatter>;
                var actualMediaTypeFormatters   = SortMediaTypeFormatters(formatters);
                var expectedMediaTypeFormatters = SortMediaTypeFormatters(mediaTypeFormatters);

                if (actualMediaTypeFormatters.Count != expectedMediaTypeFormatters.Count)
                {
                    failedValidationAction(
                        "Formatters",
                        string.Format("to be {0}", expectedMediaTypeFormatters.Count),
                        string.Format("instead found {0}", actualMediaTypeFormatters.Count));
                }

                for (int i = 0; i < actualMediaTypeFormatters.Count; i++)
                {
                    var actualMediaTypeFormatter   = actualMediaTypeFormatters[i];
                    var expectedMediaTypeFormatter = expectedMediaTypeFormatters[i];
                    if (actualMediaTypeFormatter != expectedMediaTypeFormatter)
                    {
                        failedValidationAction(
                            "Formatters",
                            string.Format("to have {0}", expectedMediaTypeFormatters[i]),
                            "none was found");
                    }
                }
            });
        }
예제 #2
0
 /// <summary>
 /// Validates the Uri from action result containing one.
 /// </summary>
 /// <param name="actionResult">Action 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 actionResult,
     Uri location,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         var actualLocation = (Uri)actionResult.Location;
         if (location != actualLocation)
         {
             failedValidationAction(
                 "location",
                 string.Format("to be {0}", location.OriginalString),
                 string.Format("instead received {0}", actualLocation.OriginalString));
         }
     });
 }
예제 #3
0
 /// <summary>
 /// Validates the IContentNegotiator from action result containing one.
 /// </summary>
 /// <param name="actionResult">Action result with IContentNegotiator.</param>
 /// <param name="contentNegotiator">Expected IContentNegotiator.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateContentNegotiator(
     dynamic actionResult,
     IContentNegotiator contentNegotiator,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         var actualContentNegotiator = actionResult.ContentNegotiator as IContentNegotiator;
         if (Reflection.AreDifferentTypes(contentNegotiator, actualContentNegotiator))
         {
             failedValidationAction(
                 "IContentNegotiator",
                 string.Format("to be {0}", contentNegotiator.GetName()),
                 string.Format("instead received {0}", actualContentNegotiator.GetName()));
         }
     });
 }
 /// <summary>
 /// Validates the Formatters from action result containing one.
 /// </summary>
 /// <param name="actionResult">Action result with Formatters.</param>
 /// <param name="mediaTypeFormatter">Expected MediaTypeFormatter.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateMediaTypeFormatter(
     dynamic actionResult,
     MediaTypeFormatter mediaTypeFormatter,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         var formatters = TryGetMediaTypeFormatters(actionResult) as IEnumerable <MediaTypeFormatter>;
         if (formatters == null || formatters.All(f => Reflection.AreDifferentTypes(f, mediaTypeFormatter)))
         {
             failedValidationAction(
                 "Formatters",
                 string.Format("to contain {0}", mediaTypeFormatter.GetName()),
                 "none was found");
         }
     });
 }
 /// <summary>
 /// Validates the Formatters from action result containing one.
 /// </summary>
 /// <param name="actionResult">Action result with Formatters.</param>
 /// <param name="formattersBuilder">Formatters builder.</param>
 /// <param name="failedValidationAction">Action to call in case of failed validation.</param>
 public static void ValidateMediaTypeFormattersBuilder(
     dynamic actionResult,
     Action <IFormattersBuilder> formattersBuilder,
     Action <string, string, string> failedValidationAction)
 {
     RuntimeBinderValidator.ValidateBinding(() =>
     {
         var newFormattersBuilder = new FormattersBuilder();
         formattersBuilder(newFormattersBuilder);
         var expectedFormatters = newFormattersBuilder.GetMediaTypeFormatters();
         expectedFormatters.ForEach(formatter =>
                                    ValidateMediaTypeFormatter(
                                        actionResult,
                                        formatter,
                                        failedValidationAction));
     });
 }
        private static IEnumerable <MediaTypeFormatter> TryGetMediaTypeFormatters(dynamic actionResult)
        {
            IEnumerable <MediaTypeFormatter> formatters = new List <MediaTypeFormatter>();

            try
            {
                var formatter = actionResult.Formatter as MediaTypeFormatter;
                formatters = new List <MediaTypeFormatter> {
                    formatter
                };
            }
            catch (RuntimeBinderException)
            {
                RuntimeBinderValidator.ValidateBinding(() =>
                {
                    formatters = actionResult.Formatters as IEnumerable <MediaTypeFormatter>;
                });
            }

            return(formatters);
        }
예제 #7
0
        /// <summary>
        /// Validates URI by using UriTestBuilder.
        /// </summary>
        /// <param name="actionResult">Dynamic representation of action 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 actionResult,
            Action <MockedUriTestBuilder> uriTestBuilder,
            Action <string, string, string> failedValidationAction)
        {
            RuntimeBinderValidator.ValidateBinding(() =>
            {
                var actualUri = actionResult.Location as Uri;

                var newUriTestBuilder = new MockedUriTestBuilder();
                uriTestBuilder(newUriTestBuilder);
                var expectedUri = newUriTestBuilder.GetMockedUri();

                var validations = newUriTestBuilder.GetMockedUriValidations();
                if (validations.Any(v => !v(expectedUri, actualUri)))
                {
                    failedValidationAction(
                        "URI",
                        "to equal the provided one",
                        "was in fact different");
                }
            });
        }