Exemplo n.º 1
0
        public async Task ClientRequest_WithWeakETag_ServerReturns304_ConstructsResponseCorrectly()
        {
            // arrange
            var state = SetUpWeakETagScenario(null);

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

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

            Assert.AreEqual("server 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.True(input.Item1.Headers.IfNoneMatch.First().IsWeak);
                return(true);
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public async Task ClientRequest_WithNoHeadersOrCache_AvoidsCache()
        {
            // arrange
            var state = new TestState();

            state.AddHttpRequest(1);

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

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

            state.Dependencies
            .Verify(x => x.Cache.Put(It.IsAny <Tuple <string, CachingHttpClient.CachedValues> >()), Times.Never);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
            }
        }