예제 #1
0
        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));
        }
예제 #2
0
        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");
        }
예제 #3
0
        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");
        }
예제 #4
0
        public void Connect()
        {
            var client = new CachedHttpClient(this.server.HttpClient);
            var result = client.GetAsync(IndexUri).Result;

            result.StatusCode.Should().Be(HttpStatusCode.OK);
        }
예제 #5
0
        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");
        }
예제 #6
0
 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.");
     }
 }
예제 #7
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);
        }
예제 #8
0
        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.");
            }
        }
예제 #9
0
        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");
        }
예제 #10
0
        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();
            }
        }
예제 #11
0
        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.");
            }
        }
예제 #12
0
        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();
            }
        }
예제 #13
0
 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.");
     }
 }
예제 #14
0
 public void Connect()
 {
     var client = new CachedHttpClient(this.server.HttpClient);
     var result = client.GetAsync(IndexUri).Result;
     result.StatusCode.Should().Be(HttpStatusCode.OK);
 }