Esempio n. 1
0
        public async Task ClientRequest_CachedValueForUserAndShared_ReturnsUserValue()
        {
            // arrange
            var state          = new TestState();
            var sharedResponse = state.AddToCache(DateTime.UtcNow.AddDays(1), addResponseContent: 1);
            var userResponse   = state.AddToCache(DateTime.UtcNow.AddDays(1), addResponseContent: 2, user: "******");
            var serverResponse = state.AddHttpRequest(3);

            // act
            var response = await state.ExecuteRequest(user : "******");

            // assert
            await CustomAssert.AssertResponse(2, response);
        }
Esempio n. 2
0
        public async Task ClientRequest_WithPreviouslyCachedValue_ReturnsCachedValue()
        {
            // arrange
            var state          = new TestState();
            var cachedResponse = state.AddToCache(DateTime.UtcNow.AddDays(1), addResponseContent: 1);
            var serverResponse = state.AddHttpRequest(2);

            // act
            var response = await state.ExecuteRequest();

            // assert
            await CustomAssert.AssertResponse(1, response);

            state.Dependencies
            .Verify(x => x.Send(It.IsAny <Tuple <HttpRequestMessage, CancellationToken> >()), Times.Never);
        }
Esempio n. 3
0
        private static TestState SetUpWeakETagScenario(string secondResponseETag)
        {
            var state = new TestState();

            state.AddToCache(
                DateTime.MinValue,
                addResponseContent: 4,
                customHeaders: new[] { KeyValuePair.Create("x-custom-header", new[] { "cached value" }) },
                expiry: NewSoft(new CacheSettings.RevalidationSettings(
                                    DateTime.MinValue,
                                    CacheSettings.Validator.NewETag(CacheSettings.EntityTag.NewWeak("\"etg 1\"")))));

            var expectedResponse = state.AddHttpRequest(null, responseCode: 304);

            expectedResponse.Headers.Add("x-custom-header", new[] { "server value" });
            if (secondResponseETag != null)
            {
                expectedResponse.Headers.ETag = new EntityTagHeaderValue(secondResponseETag, true);
            }

            return(state);
        }
Esempio n. 4
0
        public async Task ClientRequest_WithStrongETag_ServerReturns304_ConstructsResponseCorrectly()
        {
            // arrange
            var state = new TestState();

            state.AddToCache(
                DateTime.MinValue,
                addResponseContent: 4,
                customHeaders: new[] { KeyValuePair.Create("x-custom-header", new[] { "cached value" }) },
                expiry: NewSoft(new CacheSettings.RevalidationSettings(
                                    DateTime.MinValue,
                                    CacheSettings.Validator.NewETag(CacheSettings.EntityTag.NewStrong("\"etg 1\"")))));

            var expectedResponse = state.AddHttpRequest(null, responseCode: 304);

            expectedResponse.Headers.Add("x-custom-header", new[] { "server value" });

            // act
            var response = await state.ExecuteRequest();

            // assert
            await CustomAssert.AssertResponse(4, response);

            state.Dependencies
            .Verify(x => x.Cache.Put(It.IsAny <Tuple <string, CachingHttpClient.CachedValues> >()), Times.Never);
            Assert.AreEqual("cached value", response.Headers.GetValues("x-custom-header").First());

            Predicate <Tuple <HttpRequestMessage, CancellationToken> > assertHttpSend = AssertHttpSend;

            state.Dependencies
            .Verify(x => x.Send(Match.Create(assertHttpSend)), Times.Once);

            bool AssertHttpSend(Tuple <HttpRequestMessage, CancellationToken> input)
            {
                Assert.AreEqual(@"""etg 1""", input.Item1.Headers.IfNoneMatch.First().Tag);
                Assert.False(input.Item1.Headers.IfNoneMatch.First().IsWeak);
                return(true);
            }
        }