public async Task <PostcodeDetail> GetPostcodeData(string postcode) { var sanitisedPostcode = postcode.Replace(" ", string.Empty); var requestUrl = string.Format($"{PostcodeEndpoint}{sanitisedPostcode}.json"); return(await _client.GetAsync <PostcodeDetail>(requestUrl)); }
public void CachedConnectMultipleUris() { CachedHttpClient.Cache.Remove(IndexPath); var mockCache = new Mock <ObjectCache>(); //cache.Set(new CacheItem(requestUri, result.Content.ToString()), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.Add(result.Headers.CacheControl.MaxAge.Value) }); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(false); mockCache.Setup(mc => mc.Contains("/HelloWorld", null)).Returns(false); mockCache.Setup(m => m.Set(It.IsAny <CacheItem>(), It.Is <CacheItemPolicy>(cip => cip.AbsoluteExpiration <DateTimeOffset.UtcNow.AddMilliseconds(1001) && cip.AbsoluteExpiration> DateTimeOffset.UtcNow.AddMilliseconds(900)))).Verifiable("Didn't add to cache"); It.Is <CacheItemPolicy>(cip => cip.AbsoluteExpiration <DateTimeOffset.UtcNow.AddMilliseconds(1001) && cip.AbsoluteExpiration> DateTimeOffset.UtcNow.AddMilliseconds(900)); var client = new CachedHttpClient(this.server.HttpClient); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); result.WasCached.Should().BeFalse("should not yet be cached."); var result2 = client.GetAsync(new Uri("/HelloWorld", UriKind.Relative)).Result; result2.StatusCode.Should().Be(HttpStatusCode.OK, "Should be ok"); result2.WasCached.Should().BeFalse("should not be cached"); CachedHttpClient.Cache.GetCount().Should().Be(2); CachedHttpClient.Cache[IndexPath].Should().NotBeNull("Should contain entry"); CachedHttpClient.Cache["/HelloWorld"].Should().NotBeNull("Should contain entry"); }
public void CachedConnect() { CachedHttpClient.Cache.Remove(IndexPath); var client = new CachedHttpClient(this.server.HttpClient); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); result.WasCached.Should().BeFalse("should not yet be cached."); CachedHttpClient.Cache.GetCount().Should().Be(1); CachedHttpClient.Cache[IndexPath].Should().NotBeNull("Should contain entry"); var result2 = client.GetAsync(IndexUri).Result; result2.StatusCode.Should().Be(HttpStatusCode.OK, "Should be ok"); result2.WasCached.Should().BeTrue("should be cached"); }
public void Connect() { var client = new CachedHttpClient(this.server.HttpClient); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); }
public void DoNotCacheMaxAgeOfZero() { using (var testServer = SingleApiServer(0)) { var mockCache = new Mock <ObjectCache>(MockBehavior.Strict); mockCache.Setup(m => m.Contains(IndexPath, null)).Returns(false); var client = new CachedHttpClient(testServer.HttpClient, mockCache.Object); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); result.WasCached.Should().BeFalse("should not be cached if time = 0."); } }
public async Task CachedHttpClientAttemptsCacheHit() { var mockedCache = new Mock <IRequestCache>(); PostcodeDetail mockOutput; mockedCache.Setup(c => c.TryGetResponse(It.IsAny <string>(), out mockOutput)) .Returns(false); var client = new CachedHttpClient(mockedCache.Object); await client.GetAsync <PostcodeDetail>("http://uk-postcodes.com/postcode/SW1A0AA.json"); mockedCache.Verify(c => c.TryGetResponse(It.IsAny <string>(), out mockOutput), Times.AtLeastOnce); }
public void CachedConnectRespectsCacheControl() { using (var testServer = SingleApiServer(1)) { var mockCache = new Mock <ObjectCache>(MockBehavior.Strict); //cache.Set(new CacheItem(requestUri, result.Content.ToString()), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.Add(result.Headers.CacheControl.MaxAge.Value) }); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(false); mockCache.Setup(m => m.Set(It.IsAny <CacheItem>(), It.Is <CacheItemPolicy>(cip => cip.AbsoluteExpiration <DateTimeOffset.UtcNow.AddMilliseconds(1001) && cip.AbsoluteExpiration> DateTimeOffset.UtcNow.AddMilliseconds(900)))).Verifiable("Didn't add to cache"); var client = new CachedHttpClient(testServer.HttpClient, mockCache.Object); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK, "Should have succeeded on first request"); result.WasCached.Should().BeFalse("should not yet be cached."); mockCache.Verify(); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(true); mockCache.Setup(mc => mc[IndexPath]).Returns(null); client.GetAsync(IndexUri).Wait(); result.StatusCode.Should().Be(HttpStatusCode.OK, "Should have succeeded on second request"); result.WasCached.Should().BeFalse("should not yet be cached."); } }
public void CachedConnectMultipleUris() { CachedHttpClient.Cache.Remove(IndexPath); var mockCache = new Mock<ObjectCache>(); //cache.Set(new CacheItem(requestUri, result.Content.ToString()), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.Add(result.Headers.CacheControl.MaxAge.Value) }); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(false); mockCache.Setup(mc => mc.Contains("/HelloWorld", null)).Returns(false); mockCache.Setup(m => m.Set(It.IsAny<CacheItem>(), It.Is<CacheItemPolicy>(cip => cip.AbsoluteExpiration < DateTimeOffset.UtcNow.AddMilliseconds(1001) && cip.AbsoluteExpiration > DateTimeOffset.UtcNow.AddMilliseconds(900)))).Verifiable("Didn't add to cache"); It.Is<CacheItemPolicy>(cip => cip.AbsoluteExpiration < DateTimeOffset.UtcNow.AddMilliseconds(1001) && cip.AbsoluteExpiration > DateTimeOffset.UtcNow.AddMilliseconds(900)); var client = new CachedHttpClient(this.server.HttpClient); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); result.WasCached.Should().BeFalse("should not yet be cached."); var result2 = client.GetAsync(new Uri("/HelloWorld", UriKind.Relative)).Result; result2.StatusCode.Should().Be(HttpStatusCode.OK, "Should be ok"); result2.WasCached.Should().BeFalse("should not be cached"); CachedHttpClient.Cache.GetCount().Should().Be(2); CachedHttpClient.Cache[IndexPath].Should().NotBeNull("Should contain entry"); CachedHttpClient.Cache["/HelloWorld"].Should().NotBeNull("Should contain entry"); }
public void PutClearsCache() { var mockCache = new Mock <ObjectCache>(MockBehavior.Strict); //cache.Set(new CacheItem(requestUri, result.Content.ToString()), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.Add(result.Headers.CacheControl.MaxAge.Value) }); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(false); mockCache.Setup(m => m.Set(It.IsAny <CacheItem>(), It.Is <CacheItemPolicy>(cip => cip.AbsoluteExpiration <DateTimeOffset.UtcNow.AddMilliseconds(1001) && cip.AbsoluteExpiration> DateTimeOffset.UtcNow.AddMilliseconds(900)))).Verifiable("Didn't add to cache"); mockCache.Setup(m => m.Remove(IndexPath, null)).Returns(null).Verifiable("didn't delete from cache"); using (var testServer = SingleApiServer(1)) { var client = new CachedHttpClient(testServer.HttpClient, mockCache.Object); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); result.WasCached.Should().BeFalse("should not yet be cached."); var result2 = client.PutAsync(IndexUri, new StringContent("HI")).Result; mockCache.Verify(); } }
public void CachedConnectRespectsCacheControl() { using (var testServer = SingleApiServer(1)) { var mockCache = new Mock<ObjectCache>(MockBehavior.Strict); //cache.Set(new CacheItem(requestUri, result.Content.ToString()), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.Add(result.Headers.CacheControl.MaxAge.Value) }); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(false); mockCache.Setup(m => m.Set(It.IsAny<CacheItem>(), It.Is<CacheItemPolicy>(cip => cip.AbsoluteExpiration < DateTimeOffset.UtcNow.AddMilliseconds(1001)&& cip.AbsoluteExpiration > DateTimeOffset.UtcNow.AddMilliseconds(900)))).Verifiable("Didn't add to cache"); var client = new CachedHttpClient(testServer.HttpClient, mockCache.Object); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK, "Should have succeeded on first request"); result.WasCached.Should().BeFalse("should not yet be cached."); mockCache.Verify(); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(true); mockCache.Setup(mc => mc[IndexPath]).Returns(null); client.GetAsync(IndexUri).Wait(); result.StatusCode.Should().Be(HttpStatusCode.OK, "Should have succeeded on second request"); result.WasCached.Should().BeFalse("should not yet be cached."); } }
public void PutClearsCache() { var mockCache = new Mock<ObjectCache>(MockBehavior.Strict); //cache.Set(new CacheItem(requestUri, result.Content.ToString()), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.Add(result.Headers.CacheControl.MaxAge.Value) }); mockCache.Setup(mc => mc.Contains(IndexPath, null)).Returns(false); mockCache.Setup(m => m.Set(It.IsAny<CacheItem>(), It.Is<CacheItemPolicy>(cip => cip.AbsoluteExpiration < DateTimeOffset.UtcNow.AddMilliseconds(1001) && cip.AbsoluteExpiration > DateTimeOffset.UtcNow.AddMilliseconds(900)))).Verifiable("Didn't add to cache"); mockCache.Setup(m => m.Remove(IndexPath, null)).Returns(null).Verifiable("didn't delete from cache"); using (var testServer = SingleApiServer(1)) { var client = new CachedHttpClient(testServer.HttpClient, mockCache.Object); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); result.WasCached.Should().BeFalse("should not yet be cached."); var result2 = client.PutAsync(IndexUri, new StringContent("HI")).Result; mockCache.Verify(); } }
public void DoNotCacheMaxAgeOfZero() { using (var testServer = SingleApiServer(0)) { var mockCache = new Mock<ObjectCache>(MockBehavior.Strict); mockCache.Setup(m => m.Contains(IndexPath, null)).Returns(false); var client = new CachedHttpClient(testServer.HttpClient, mockCache.Object); var result = client.GetAsync(IndexUri).Result; result.StatusCode.Should().Be(HttpStatusCode.OK); result.WasCached.Should().BeFalse("should not be cached if time = 0."); } }