예제 #1
0
        public async Task FireAsync_should_send_a_request_to_loopback_address()
        {
            // Arrange
            WebApiExtensions._fwConfig = new FlatwhiteWebApiConfiguration
            {
                LoopbackAddress = "http://192.188.2.1:8080"
            };
            using (var phoenix = new WebApiPhoenixWithPublicMethods(Substitute.For <_IInvocation>(), new WebApiCacheItem {
                Key = "1"
            }, _requestMessage))
            {
                phoenix.HttpClient = _client;

                // Action
                await phoenix.FireAsyncPublic();

                // Assert
                await _client
                .Received(1)
                .SendAsync(Arg.Is <HttpRequestMessage>(msg => msg.Properties.Count == 0 &&
                                                       msg.Headers.CacheControl.Extensions.Any(e => e.Name == WebApiExtensions.__cacheControl_flatwhite_force_refresh) &&
                                                       msg.RequestUri.ToString() == "http://192.188.2.1:8080/api/method/id")
                           , HttpCompletionOption.ResponseHeadersRead);
            }
        }
예제 #2
0
        public async Task FireAsync_should_send_a_request_to_original_endpoint_when_loopback_is_not_set()
        {
            // Arrange
            var currentCacheItem = new WebApiCacheItem
            {
                CreatedTime          = DateTime.UtcNow,
                MaxAge               = 2,
                StaleWhileRevalidate = 3,
                StaleIfError         = 4,
                Key = "1"
            };
            var invocation = Substitute.For <_IInvocation>();


            using (var phoenix = new WebApiPhoenixWithPublicMethods(invocation, currentCacheItem, _requestMessage))
            {
                phoenix.HttpClient = _client;


                // Action
                var state = await phoenix.FireAsyncPublic();

                // Assert
                Assert.IsTrue(state is InActivePhoenix);
                _client.Received(1).Timeout = Arg.Is <TimeSpan>(x => x.TotalSeconds > 4);
                await _client
                .Received(1)
                .SendAsync(Arg.Is <HttpRequestMessage>(msg => msg.Properties.Count == 0 &&
                                                       msg.Headers.CacheControl.Extensions.Any(e => e.Name == WebApiExtensions.__cacheControl_flatwhite_force_refresh) &&
                                                       msg.RequestUri.ToString() == "http://localhost/api/method/id")
                           , HttpCompletionOption.ResponseHeadersRead);
            }
        }
        public async Task FireAsync_should_send_a_request_to_original_endpoint_when_loopback_is_not_set()
        {
            // Arrange
            var currentCacheItem = new WebApiCacheItem
            {
                CreatedTime = DateTime.UtcNow,
                MaxAge = 2,
                StaleWhileRevalidate = 3,
                StaleIfError = 4,
                Key = "1"
            };
            var invocation = Substitute.For<_IInvocation>();


            using (var phoenix = new WebApiPhoenixWithPublicMethods(invocation, currentCacheItem, _requestMessage))
            { 
                phoenix.HttpClient = _client;
            

                // Action
                var state = await phoenix.FireAsyncPublic();

                // Assert
                Assert.IsTrue(state is InActivePhoenix);
                await _client
                    .Received(1)
                    .SendAsync(Arg.Is<HttpRequestMessage>(msg => msg.Properties.Count == 0 && 
                                                                 msg.Headers.CacheControl.Extensions.Any(e => e.Name == WebApiExtensions.__cacheControl_flatwhite_force_refresh) &&
                                                                 msg.RequestUri.ToString() == "http://localhost/api/method/id")
                                , HttpCompletionOption.ResponseHeadersRead);
            }
        }
예제 #4
0
        public void FireAsync_should_throw_if_request_failed()
        {
            // Arrange
            _client
            .SendAsync(Arg.Any <HttpRequestMessage>(), HttpCompletionOption.ResponseHeadersRead)
            .Returns(Task.FromResult(new HttpResponseMessage {
                StatusCode = System.Net.HttpStatusCode.InternalServerError
            }));

            using (var phoenix = new WebApiPhoenixWithPublicMethods(Substitute.For <_IInvocation>(), new WebApiCacheItem {
                Key = "1"
            }, _requestMessage))
            {
                phoenix.HttpClient = _client;

                // Action
                Assert.Throws <HttpRequestException>(async() => { await phoenix.FireAsyncPublic(); });
            }
        }
        public async Task FireAsync_should_send_a_request_to_loopback_address()
        {
            // Arrange
            WebApiExtensions._fwConfig = new FlatwhiteWebApiConfiguration
            {
                LoopbackAddress = "http://192.188.2.1:8080"
            };
            using (var phoenix = new WebApiPhoenixWithPublicMethods(Substitute.For<_IInvocation>(), new WebApiCacheItem { Key = "1" }, _requestMessage))
            {
                phoenix.HttpClient = _client;

                // Action
                await phoenix.FireAsyncPublic();

                // Assert
                await _client
                    .Received(1)
                    .SendAsync(Arg.Is<HttpRequestMessage>(msg => msg.Properties.Count == 0 &&
                                                                 msg.Headers.CacheControl.Extensions.Any(e => e.Name == WebApiExtensions.__cacheControl_flatwhite_force_refresh) &&
                                                                 msg.RequestUri.ToString() == "http://192.188.2.1:8080/api/method/id")
                                , HttpCompletionOption.ResponseHeadersRead);
            }
        }
        public void FireAsync_should_throw_if_request_failed()
        {
            // Arrange
            _client
                .SendAsync(Arg.Any<HttpRequestMessage>(), HttpCompletionOption.ResponseHeadersRead)
                .Returns(Task.FromResult(new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.InternalServerError }));

            using (var phoenix = new WebApiPhoenixWithPublicMethods(Substitute.For<_IInvocation>(), new WebApiCacheItem {Key = "1"}, _requestMessage))
            { 
                phoenix.HttpClient = _client;

                // Action
                Assert.Throws<HttpRequestException>(async () => { await phoenix.FireAsyncPublic(); });
            }
        }