Exemple #1
0
        public async Task SendAsync_ExecutesInnerHandler_WhenRequest_Contains_NoCache_CacheControlHeader()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Headers.CacheControl = new CacheControlHeaderValue()
            {
                NoCache = true
            };
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("My Example Reponse")
            };

            testHandler.HttpResponseToReturn = testResponse;

            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.True(testHandler.SendAsyncCalled);
        }
Exemple #2
0
        public async Task SendAsync_PassesResponseEtag_ToCache_WhenResult_HasEtagHeader_AndCanBeCached()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("My Example Reponse")
            };

            testResponse.Headers.CacheControl = new CacheControlHeaderValue {
                Private = true, MaxAge = TimeSpan.FromSeconds(60)
            };
            testResponse.Headers.ETag = new EntityTagHeaderValue("\"123456789\"");

            testHandler.HttpResponseToReturn = testResponse;

            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            _mockResponseCache.Verify(c => c.Add(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TimeSpan>(), It.Is <string>(s => s == "\"123456789\"")), Times.Once);
        }
Exemple #3
0
        public async Task SendAsync_CallsAdd_OnResponseCache_WhenPreviouslyCachedResponseHasExpired_AndNewResponseHas_NoEtag()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("My Example Reponse")
            };

            testResponse.Headers.CacheControl = new CacheControlHeaderValue {
                Private = true, MaxAge = TimeSpan.FromSeconds(60)
            };

            testHandler.HttpResponseToReturn = testResponse;

            _mockResponseCache.Setup(c => c.Exists(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.IsExpired(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            _mockResponseCache.Verify(c => c.Add(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TimeSpan>(), null), Times.Once);
        }
Exemple #4
0
        public async Task SendAsync_Passes_ResponseContent_AsBase64String_ToCache_WhenResult_CanBeCached()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("My Example Reponse")
            };

            testResponse.Headers.CacheControl = new CacheControlHeaderValue {
                Private = true, MaxAge = TimeSpan.FromSeconds(60)
            };

            testHandler.HttpResponseToReturn = testResponse;

            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            var bytes           = Encoding.UTF8.GetBytes("My Example Reponse");
            var expectedContent = System.Convert.ToBase64String(bytes);

            _mockResponseCache.Verify(c => c.Add(It.IsAny <string>(), It.Is <string>(s => s == expectedContent), It.IsAny <TimeSpan>(), null), Times.Once);
        }
Exemple #5
0
        public async Task SendAsync_ExecutesInnerHandler_WhenResponseCache_IsNull()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(null, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("My Example Reponse")
            };

            testHandler.HttpResponseToReturn = testResponse;

            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.True(testHandler.SendAsyncCalled);
        }
Exemple #6
0
        public async Task SendAsync_SetsResultContent_UsingCache_WhenReponseCode_Is304()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            var cachedContent             = "My Cached Response";
            var bytes                     = Encoding.UTF8.GetBytes(cachedContent);
            var base64EncodedCacheContent = Convert.ToBase64String(bytes);

            _mockResponseCache.Setup(c => c.Get(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(base64EncodedCacheContent);

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.NotModified
            };

            testResponse.Headers.CacheControl = new CacheControlHeaderValue {
                Private = true, MaxAge = TimeSpan.FromSeconds(60)
            };

            testHandler.HttpResponseToReturn = testResponse;

            //Act
            var result = await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.Equal("My Cached Response", await result.Content.ReadAsStringAsync());
        }
Exemple #7
0
        public async Task SendAsync_DoesNotCallAdd_OnReponseCache_WhenHttpResponseCode_IsNot200()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent("My Example Reponse")
            };

            testResponse.Headers.CacheControl = new CacheControlHeaderValue {
                Private = true
            };

            testHandler.HttpResponseToReturn = testResponse;

            //Act
            var result = await sut.SendAsync(request, new CancellationToken());

            //Assert
            _mockResponseCache.Verify(c => c.Add(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TimeSpan>(), null), Times.Never);
        }
Exemple #8
0
        public async Task SendAsync_ReturnsResult_WithCorrectReponseContent_WhenCacheHasFreshContent()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            _mockResponseCache.Setup(c => c.Exists(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.IsExpired(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(false);

            var bytes        = Encoding.UTF8.GetBytes("My cached response content");
            var cacheContent = Convert.ToBase64String(bytes);

            _mockResponseCache.Setup(c => c.Get(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(cacheContent);

            //Act
            var result = await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.Equal("My cached response content", await result.Content.ReadAsStringAsync());
        }
Exemple #9
0
        public async Task SendAsync_DoesNotAddIfNoneMatchedHeader_ToRequest_WhenCachedReponse_HasNoEtag()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            _mockResponseCache.Setup(c => c.Exists(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.IsExpired(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.GetETag(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(() => null);

            testHandler.HttpResponseToReturn = new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK
            };

            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.Empty(testHandler.PreviousRequestHeaders.IfNoneMatch);
        }
Exemple #10
0
        public async Task SendAsync_AddsIfNonMatchedHeader_ToRequest_WhenCacheContainsStaleContent_AndAnEtagIsPresent()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            _mockResponseCache.Setup(c => c.Exists(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.IsExpired(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.GetETag(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns("\"123\"");

            testHandler.HttpResponseToReturn = new HttpResponseMessage {
                StatusCode = HttpStatusCode.NotModified
            };


            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.Single(testHandler.PreviousRequestHeaders.IfNoneMatch);
            Assert.Equal("\"123\"", testHandler.PreviousRequestHeaders.IfNoneMatch.First().Tag);
        }
Exemple #11
0
        public async Task SendAsync_ExecutesInnerHandler_GivenGetRequest_WhenCacheContainsStaleContent()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            _mockResponseCache.Setup(c => c.Exists(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.IsExpired(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            testHandler.HttpResponseToReturn = new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK
            };

            //Act
            await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.True(testHandler.SendAsyncCalled);
        }
Exemple #12
0
        public async Task SendAsync_ReturnsResponse_WithStatusCode200_WhenIfNoneMatchedRequest_ReturnsReponseCode_WithStatusCode304()
        {
            //Arrange
            var testHandler = new FakeDelegatingHttpHandler();
            var sut         = new HttpCachingHandlerTestWrapper(_mockResponseCache.Object, testHandler);

            var request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri("http://www.tempuri.org/myresource");

            _mockResponseCache.Setup(c => c.Get(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns("My Cached Response");

            _mockResponseCache.Setup(c => c.Exists(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.IsExpired(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns(true);

            _mockResponseCache.Setup(c => c.GetETag(It.Is <string>(s => s == request.RequestUri.AbsoluteUri)))
            .Returns("\"123\"");

            var testResponse = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.NotModified
            };

            testResponse.Headers.CacheControl = new CacheControlHeaderValue {
                Private = true, MaxAge = TimeSpan.FromSeconds(60)
            };

            testHandler.HttpResponseToReturn = testResponse;

            //Act
            var result = await sut.SendAsync(request, new CancellationToken());

            //Assert
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
        }