public void CreateInstance_HonorsOverrides(
            ResponseCacheAttribute responseCache,
            Dictionary <string, CacheProfile> cacheProfiles,
            CacheProfile expectedProfile)
        {
            // Arrange & Act
            var createdFilter = responseCache.CreateInstance(GetServiceProvider(cacheProfiles));

            // Assert
            var responseCacheFilter = Assert.IsType <ResponseCacheFilter>(createdFilter);

            Assert.Equal(expectedProfile.Duration, responseCacheFilter.Duration);
            Assert.Equal(expectedProfile.Location, responseCacheFilter.Location);
            Assert.Equal(expectedProfile.NoStore, responseCacheFilter.NoStore);
            Assert.Equal(expectedProfile.VaryByHeader, responseCacheFilter.VaryByHeader);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new instance of <see cref="ResponseCacheFilter"/>
        /// </summary>
        /// <param name="cacheProfile">The profile which contains the settings for
        /// <see cref="ResponseCacheFilter"/>.</param>
        public ResponseCacheFilter(CacheProfile cacheProfile)
        {
            if (!(cacheProfile.NoStore ?? false))
            {
                // Duration MUST be set (either in the cache profile or in the attribute) unless NoStore is true.
                if (cacheProfile.Duration == null)
                {
                    throw new InvalidOperationException(
                              Resources.FormatResponseCache_SpecifyDuration(nameof(NoStore), nameof(Duration)));
                }
            }

            Duration     = cacheProfile.Duration ?? 0;
            Location     = cacheProfile.Location ?? ResponseCacheLocation.Any;
            NoStore      = cacheProfile.NoStore ?? false;
            VaryByHeader = cacheProfile.VaryByHeader;
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new instance of <see cref="ResponseCacheFilter"/>
        /// </summary>
        /// <param name="cacheProfile">The profile which contains the settings for
        /// <see cref="ResponseCacheFilter"/>.</param>
        public ResponseCacheFilter(CacheProfile cacheProfile)
        {
            if (!(cacheProfile.NoStore ?? false))
            {
                // Duration MUST be set (either in the cache profile or in the attribute) unless NoStore is true.
                if (cacheProfile.Duration == null)
                {
                    throw new InvalidOperationException(
                            Resources.FormatResponseCache_SpecifyDuration(nameof(NoStore), nameof(Duration)));
                }
            }

            Duration = cacheProfile.Duration ?? 0;
            Location = cacheProfile.Location ?? ResponseCacheLocation.Any;
            NoStore = cacheProfile.NoStore ?? false;
            VaryByHeader = cacheProfile.VaryByHeader;
        }
Esempio n. 4
0
        public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var optionsAccessor = serviceProvider.GetRequiredService <IOptions <MvcOptions> >();

            CacheProfile selectedProfile = null;

            if (CacheProfileName != null)
            {
                optionsAccessor.Value.CacheProfiles.TryGetValue(CacheProfileName, out selectedProfile);
                if (selectedProfile == null)
                {
                    throw new InvalidOperationException(Resources.FormatCacheProfileNotFound(CacheProfileName));
                }
            }

            // If the ResponseCacheAttribute parameters are set,
            // then it must override the values from the Cache Profile.
            // The below expression first checks if the duration is set by the attribute's parameter.
            // If absent, it checks the selected cache profile (Note: There can be no cache profile as well)
            // The same is the case for other properties.
            _duration    = _duration ?? selectedProfile?.Duration;
            _noStore     = _noStore ?? selectedProfile?.NoStore;
            _location    = _location ?? selectedProfile?.Location;
            VaryByHeader = VaryByHeader ?? selectedProfile?.VaryByHeader;

            // ResponseCacheFilter cannot take any null values. Hence, if there are any null values,
            // the properties convert them to their defaults and are passed on.
            return(new ResponseCacheFilter(
                       new CacheProfile
            {
                Duration = _duration,
                Location = _location,
                NoStore = _noStore,
                VaryByHeader = VaryByHeader
            }));
        }
 /// <summary>
 /// Creates a new instance of <see cref="ResponseCacheFilter"/>
 /// </summary>
 /// <param name="cacheProfile">The profile which contains the settings for
 /// <see cref="ResponseCacheFilter"/>.</param>
 public ResponseCacheFilter(CacheProfile cacheProfile)
 {
     _cacheProfile = cacheProfile;
 }
Esempio n. 6
0
 /// <summary>
 /// Creates a new instance of <see cref="ResponseCacheFilter"/>
 /// </summary>
 /// <param name="cacheProfile">The profile which contains the settings for
 /// <see cref="ResponseCacheFilter"/>.</param>
 public ResponseCacheFilter(CacheProfile cacheProfile)
 {
     _cacheProfile = cacheProfile;
 }
        public void CreateInstance_HonorsOverrides(
            ResponseCacheAttribute responseCache,
            Dictionary<string, CacheProfile> cacheProfiles,
            CacheProfile expectedProfile)
        {
            // Arrange & Act
            var createdFilter = responseCache.CreateInstance(GetServiceProvider(cacheProfiles));

            // Assert
            var responseCacheFilter = Assert.IsType<ResponseCacheFilter>(createdFilter);
            Assert.Equal(expectedProfile.Duration, responseCacheFilter.Duration);
            Assert.Equal(expectedProfile.Location, responseCacheFilter.Location);
            Assert.Equal(expectedProfile.NoStore, responseCacheFilter.NoStore);
            Assert.Equal(expectedProfile.VaryByHeader, responseCacheFilter.VaryByHeader);
        }