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); }
/// <inheritdoc /> 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; VaryByQueryKeys = VaryByQueryKeys ?? selectedProfile?.VaryByQueryKeys; // 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, VaryByQueryKeys = VaryByQueryKeys, })); }
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); }