Exemplo n.º 1
0
        public void FormatFilter_IsActive(
            string format,
            FormatSource place,
            bool expected)
        {
            // Arrange
            var mockObjects            = new MockObjects(format, place);
            var resultExecutingContext = mockObjects.CreateResultExecutingContext();
            var filterAttribute        = new FormatFilterAttribute();
            var filter = new FormatFilter(mockObjects.OptionsManager, mockObjects.ActionContextAccessor);

            // Act and Assert
            Assert.Equal(expected, filter.IsActive);
        }
Exemplo n.º 2
0
        // Set up application services
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
            {
                var formatFilter = new FormatFilterAttribute();
                options.Filters.Add(formatFilter);

                var customFormatter = new CustomFormatter("application/custom");
                options.OutputFormatters.Add(customFormatter);

                options.FormatterMappings.SetMediaTypeMappingForFormat(
                    "custom",
                    MediaTypeHeaderValue.Parse("application/custom"));
            });
        }
Exemplo n.º 3
0
        public void FormatFilter_GetFormat_UsesInvariantCulture()
        {
            // Arrange
            var mockObjects = new MockObjects();
            var context = mockObjects.CreateResultExecutingContext();
            context.RouteData.Values["format"] = new DateTimeOffset(2018, 10, 31, 7, 37, 38, TimeSpan.FromHours(-7));
            var expected = "10/31/2018 07:37:38 -07:00";
            var filterAttribute = new FormatFilterAttribute();
            var filter = new FormatFilter(mockObjects.OptionsManager, NullLoggerFactory.Instance);

            // Act
            var format = filter.GetFormat(context);

            // Assert
            Assert.Equal(expected, filter.GetFormat(context));
        }
Exemplo n.º 4
0
        public void FormatFilter_GetFormat(
            string input,
            FormatSource place,
            string expected)
        {
            // Arrange
            var mockObjects     = new MockObjects(input, place);
            var context         = mockObjects.CreateResultExecutingContext();
            var filterAttribute = new FormatFilterAttribute();
            var filter          = new FormatFilter(mockObjects.OptionsManager);

            // Act
            var format = filter.GetFormat(context);

            // Assert
            Assert.Equal(expected, filter.GetFormat(context));
        }
Exemplo n.º 5
0
        // Set up application services
        public void ConfigureServices(IServiceCollection services)
        {
            var mvcBuilder = services.AddMvc(options =>
            {
                var formatFilter = new FormatFilterAttribute();
                options.Filters.Add(formatFilter);

                var customFormatter = new CustomFormatter("application/custom");
                options.OutputFormatters.Add(customFormatter);
                options.OutputFormatters.RemoveType <StringOutputFormatter>();

                options.FormatterMappings.SetMediaTypeMappingForFormat(
                    "custom",
                    MediaTypeHeaderValue.Parse("application/custom"));
            });

            mvcBuilder.AddXmlDataContractSerializerFormatters();
        }
Exemplo n.º 6
0
        // Set up application services
        public void ConfigureServices(IServiceCollection services)
        {
            var mvcBuilder = services.AddMvc(options =>
            {
                var formatFilter = new FormatFilterAttribute();
                options.Filters.Add(formatFilter);

                var customFormatter = new CustomFormatter("application/custom");
                options.OutputFormatters.Add(customFormatter);
                options.OutputFormatters.RemoveType<StringOutputFormatter>();

                options.FormatterMappings.SetMediaTypeMappingForFormat(
                    "custom",
                    MediaTypeHeaderValue.Parse("application/custom"));
            });

            mvcBuilder.AddXmlDataContractSerializerFormatters();
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Adds a type to a <see cref="FilterCollection" /> if it doesn't already exist.
        /// </summary>
        /// <typeparam name="TType">The type of the t type.</typeparam>
        /// <param name="col">The col.</param>
        /// <param name="order">Filter order</param>
        /// <returns>FilterCollection.</returns>
        public static FilterCollection TryAdd <TType>(this FilterCollection col, int?order = null)
            where TType : IFilterMetadata
        {
            var vt = typeof(TType);

            if (col.All(t =>
            {
                // Yet more lameness, really should be able to do a simple test here...
                return(t switch
                {
                    TypeFilterAttribute tf => tf.ImplementationType != vt,
                    ServiceFilterAttribute sf => sf.ServiceType != vt,
                    FormatFilterAttribute ff => ff.GetType() != vt,
                    ResultFilterAttribute rs => rs.GetType() != vt,
                    ExceptionFilterAttribute ef => ef.GetType() != vt,
                    ActionFilterAttribute af => af.GetType() != vt,
                    _ => t.GetType() != vt
                });
            }
Exemplo n.º 8
0
        public void FormatFilter_IsActive(
            string format,
            FormatSource place,
            bool expected)
        {
            // Arrange
            var mockObjects = new MockObjects(format, place);
            var resultExecutingContext = mockObjects.CreateResultExecutingContext();
            var filterAttribute = new FormatFilterAttribute();
            var filter = new FormatFilter(mockObjects.OptionsManager, mockObjects.ActionContextAccessor);

            // Act and Assert
            Assert.Equal(expected, filter.IsActive);
        }
Exemplo n.º 9
0
        public void FormatFilter_GetFormat(
            string input,
            FormatSource place,
            string expected)
        {
            // Arrange
            var mockObjects = new MockObjects(input, place);
            var context = mockObjects.CreateResultExecutingContext();
            var filterAttribute = new FormatFilterAttribute();
            var filter = new FormatFilter(mockObjects.OptionsManager);

            // Act
            var format = filter.GetFormat(context);

            // Assert
            Assert.Equal(expected, filter.GetFormat(context));
        }