public void Should_return_GatewayTimeout_when_no_cache_and_request_sent_OnlyIfCached_control() { // Arrange var cacheControl = new CacheControlHeaderValue {OnlyIfCached = true}; var svc = new CacheResponseBuilder(); // Action var result = svc.GetResponse(cacheControl, null, new HttpRequestMessage()); // Assert Assert.AreEqual(HttpStatusCode.GatewayTimeout, result.StatusCode); Assert.AreEqual("no cache available", result.Headers.GetValues("X-Flatwhite-Message").Single()); }
public void Should_return_304_if_etag_matched() { // Arrange var cacheControl = new CacheControlHeaderValue { MaxStale = true, MaxStaleLimit = TimeSpan.FromSeconds(15), MinFresh = TimeSpan.FromSeconds(20) }; var cacheItem = new WebApiCacheItem { CreatedTime = DateTime.UtcNow.AddSeconds(-11), MaxAge = 10, StaleWhileRevalidate = 5, IgnoreRevalidationRequest = true, ResponseCharSet = "UTF8", ResponseMediaType = "text/json", Content = new byte[0], Key = "CacheKey" + Guid.NewGuid() }; Global.Cache.PhoenixFireCage[cacheItem.Key] = new Phoenix(NSubstitute.Substitute.For<_IInvocation>(), new CacheInfo()); var request = new HttpRequestMessage(); request.Properties[WebApiExtensions.__webApi_etag_matched] = true; var svc = new CacheResponseBuilder { }; // Action var response = svc.GetResponse(cacheControl, cacheItem, request); // Assert Assert.AreEqual(HttpStatusCode.NotModified, response.StatusCode); Assert.AreEqual("Cache freshness lifetime not qualified", response.Headers.GetValues("X-Flatwhite-Warning").First()); Assert.AreEqual("Response is Stale", response.Headers.GetValues("X-Flatwhite-Warning").Last()); Assert.AreEqual($"110 - \"Response is Stale\"", response.Headers.GetValues("Warning").Last()); }
public async Task Should_return_new_etag_if_cache_item_found_but_doesnt_match_checksum(string cacheChecksum, HttpStatusCode resultCode) { // Arrange var cacheControl = new CacheControlHeaderValue { MaxStale = true, MaxStaleLimit = TimeSpan.FromSeconds(15), MinFresh = TimeSpan.FromSeconds(20) }; var oldCacheItem = new WebApiCacheItem { CreatedTime = DateTime.UtcNow.AddSeconds(-11), MaxAge = 10, StaleWhileRevalidate = 5, IgnoreRevalidationRequest = true, ResponseCharSet = "UTF8", ResponseMediaType = "text/json", Content = new byte[0], Key = "fw-0-HASHEDKEY", Checksum = cacheChecksum }; var request = new HttpRequestMessage { Method = new HttpMethod("GET"), RequestUri = new Uri("http://localhost") }; request.Headers.Add("If-None-Match", "\"fw-0-HASHEDKEY-OLDCHECKSUM\""); var builder = new CacheResponseBuilder(); var handler = new EtagHeaderHandler(builder); await Global.CacheStoreProvider.GetAsyncCacheStore().SetAsync("fw-0-HASHEDKEY", oldCacheItem, DateTimeOffset.Now.AddDays(1)).ConfigureAwait(false); // Action Global.Cache.PhoenixFireCage["fw-0-HASHEDKEY"] = new WebApiPhoenix(NSubstitute.Substitute.For<_IInvocation>(), new CacheInfo(), oldCacheItem, request); var response = await handler.HandleAsync(cacheControl, request, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(resultCode, response.StatusCode); if (resultCode == HttpStatusCode.OK) { Assert.AreEqual($"\"fw-0-HASHEDKEY-{cacheChecksum}\"", response.Headers.ETag.Tag); } else { Assert.IsNull(response.Headers.ETag); } }
/// <summary> /// Initialize default flatwhite webapi config /// </summary> public FlatwhiteWebApiConfiguration() { EnableStatusController = true; ResponseBuilder = new CacheResponseBuilder(); IgnoreVaryCustomKeys = false; }
public void Should_return_null_if_cache_not_mature_as_min_fresh_request() { // Arrange var cacheControl = new CacheControlHeaderValue { MinFresh = TimeSpan.FromSeconds(100) }; var cacheItem = new WebApiCacheItem { CreatedTime = DateTime.UtcNow.AddSeconds(-20), MaxAge = 1000, StaleWhileRevalidate = 5, IgnoreRevalidationRequest = false }; var request = new HttpRequestMessage(); var svc = new CacheResponseBuilder { }; // Action var response = svc.GetResponse(cacheControl, cacheItem, request); // Assert Assert.IsNull(response); }
public void Should_return_null_when_no_cache_found() { // Arrange var svc = new CacheResponseBuilder(); // Action var result = svc.GetResponse(new CacheControlHeaderValue { }, null, new HttpRequestMessage()); // Assert Assert.IsNull(result); }
public void Should_return_null_if_stale() { // Arrange var cacheControl = new CacheControlHeaderValue { MaxStale = true, MaxStaleLimit = TimeSpan.FromSeconds(15) }; var cacheItem = new WebApiCacheItem { CreatedTime = DateTime.UtcNow.AddSeconds(-11), MaxAge = 10, StaleWhileRevalidate = 5, IgnoreRevalidationRequest = false, ResponseCharSet = "UTF8", ResponseMediaType = "text/json", Content = new byte[0], Key = "CacheKey" + Guid.NewGuid() }; Global.Cache.PhoenixFireCage[cacheItem.Key] = new Phoenix(NSubstitute.Substitute.For<_IInvocation>(), new CacheInfo()); var request = new HttpRequestMessage(); var svc = new CacheResponseBuilder { }; // Action var response = svc.GetResponse(cacheControl, cacheItem, request); // Assert Assert.IsNull(response); }