public void GetCacheKey_RequestIsNull_ArgumentNullExceptionIsThrown() { var cacheMock = new Mock <IHttpCache>(); var sut = new HttpStandardKeyStrategy(cacheMock.Object); Assert.Throws <ArgumentNullException>(() => sut.GetCacheKey(null)); }
public void GetCacheKey_VaryHeadersAreCachedFromPreviousRequestResponsePair_TheTwoKeysAreMatching() { var usedKey = ""; var usedVaryHeaders = ""; var cacheMock = new Mock <IHttpCache>(); cacheMock.Setup(x => x.Put(It.IsAny <string>(), It.IsAny <string>())).Callback( delegate(string key, object value) { usedKey = key; usedVaryHeaders = value as string; }); var sut = new HttpStandardKeyStrategy(cacheMock.Object, CacheKeySetting.Standard); var request = new HttpRequestMessage(HttpMethod.Get, UrlWithQueryString); request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US")); var response = new HttpResponseMessage(HttpStatusCode.Accepted) { RequestMessage = request }; response.Headers.Vary.Add("Accept-Encoding"); response.Headers.Vary.Add("Accept-Language"); var firstKey = sut.GetCacheKey(request, response); //We set it up here because usedKey and usedVaryHeaders are set in the call above cacheMock.Setup(x => x.GetString(It.Is <string>(key => key.Equals(usedKey)))).Returns(usedVaryHeaders); var secondKey = sut.GetCacheKey(request); Assert.Equal(firstKey, secondKey); }
public void GetCacheKeyForRequestAndResponse_ResponseIsNull_ArgumentNullExceptionIsThrown() { var cacheMock = new Mock <IHttpCache>(); cacheMock.Setup(x => x.GetString(It.IsAny <string>())).Returns(""); var sut = new HttpStandardKeyStrategy(cacheMock.Object, CacheKeySetting.Standard); Assert.Throws <ArgumentNullException>(() => sut.GetCacheKey(new HttpRequestMessage(HttpMethod.Get, UrlWithQueryString), null)); }
public void GetCacheKey_RequestUrlWithQueryStringAndSettingIsIgnoreQueryString_KeyIsUrlWithoutQueryString() { var cacheMock = new Mock <IHttpCache>(); cacheMock.Setup(x => x.GetString(It.IsAny <string>())).Returns(""); var sut = new HttpStandardKeyStrategy(cacheMock.Object, CacheKeySetting.IgnoreQueryString); var request = new HttpRequestMessage(HttpMethod.Get, UrlWithQueryString); var key = sut.GetCacheKey(request); Assert.Equal(UrlWithoutQueryString, key); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); var cacheConfig = new CacheManager.Core.ConfigurationBuilder().WithMicrosoftMemoryCacheHandle().Build(); var cache = new BaseCacheManager <object>(cacheConfig); var cacheAdapter = new CacheManagerAdapter(cache); var keyStrategy = new HttpStandardKeyStrategy(cacheAdapter); var handler = new HttpCachingHandler(cacheAdapter, keyStrategy); var api = new MyAPI(handler); api.BaseUri = new Uri("http://localhost:3670"); services.AddSingleton <IMyAPI>(api); }
public void GetCacheKeyForRequestAndResponse_NoVaryHeadersInResponseAndStandardSettings_KeyIsUrlWithQueryString() { var cacheMock = new Mock <IHttpCache>(); cacheMock.Setup(x => x.GetString(It.IsAny <string>())).Returns(""); var sut = new HttpStandardKeyStrategy(cacheMock.Object, CacheKeySetting.Standard); var request = new HttpRequestMessage(HttpMethod.Get, UrlWithQueryString); var response = new HttpResponseMessage(HttpStatusCode.Accepted) { RequestMessage = request, }; var key = sut.GetCacheKey(request, response); Assert.Equal(UrlWithQueryString, key); }
public void GetCacheKeyForRequestAndResponse_VaryHeaderInResponseAndStandardSettings_KeyIsUrlWithQueryStringAndVaryHeader() { var cacheMock = new Mock <IHttpCache>(); cacheMock.Setup(x => x.GetString(It.IsAny <string>())).Returns(""); var sut = new HttpStandardKeyStrategy(cacheMock.Object, CacheKeySetting.Standard); var request = new HttpRequestMessage(HttpMethod.Get, UrlWithQueryString); request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); var response = new HttpResponseMessage(HttpStatusCode.Accepted) { RequestMessage = request }; response.Headers.Vary.Add("Accept-Encoding"); var key = sut.GetCacheKey(request, response); Assert.Equal(UrlWithQueryString + "_gzip", key); }
public HttpCachingHandlerTests() { var cache = new SimpleCache(); var keyStrategy = new HttpStandardKeyStrategy(cache); var fakeResponse = ResponseBuilder.Response(HttpStatusCode.OK) .WithContent(new StringContent(ResponseContent, Encoding.UTF8, "application/json")) .Created(_testDate.Subtract(TimeSpan.FromMinutes(15))) .WithMaxAge(3600) .Build(); _cachingHandler = new HttpCachingHandler(cache, keyStrategy) { SystemClock = new FakeClock { UtcNow = _testDate }, InnerHandler = new FakeMessageHandler { Response = fakeResponse } }; _client = new HttpClient(_cachingHandler); }