public async void TestHttpClientWithRetryIntercept()
        {
            // The fake handler lets us track the number of 'actual' requests
            var innerHandler = new FakeHandler();

            var retryHandler = new RetryDelegatingHandler
                          {
                              InnerHandler = innerHandler,
                              RetryIntercept = async delegate(HttpRequestMessage request, HttpResponseMessage responseMessage)
                                  {
                                      if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
                                      {
                                          // Predent we authenticate
                                          var retryClient = new HttpClient(innerHandler);

                                          var authorizeResponse = await retryClient.GetAsync("http://hello.world/authorize?a=bcd");

                                          return authorizeResponse.IsSuccessStatusCode;
                                      }

                                      return false;
                                  }
                          };

            var client = new HttpClient(retryHandler);

            var response = await client.GetAsync("http://hello.world/test");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            Assert.AreEqual(1, innerHandler.FailedCount);
            Assert.AreEqual(1, innerHandler.AuthorizationAttempts);
            Assert.AreEqual(1, innerHandler.OkCount);
        }
Ejemplo n.º 2
0
        public async void TestHttpClientWithRetryIntercept()
        {
            // The fake handler lets us track the number of 'actual' requests
            var innerHandler = new FakeHandler();

            var retryHandler = new RetryDelegatingHandler
            {
                InnerHandler   = innerHandler,
                RetryIntercept = async delegate(HttpRequestMessage request, HttpResponseMessage responseMessage)
                {
                    if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        // Predent we authenticate
                        var retryClient = new HttpClient(innerHandler);

                        var authorizeResponse = await retryClient.GetAsync("http://hello.world/authorize?a=bcd");

                        return(authorizeResponse.IsSuccessStatusCode);
                    }

                    return(false);
                }
            };

            var client = new HttpClient(retryHandler);

            var response = await client.GetAsync("http://hello.world/test");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            Assert.AreEqual(1, innerHandler.FailedCount);
            Assert.AreEqual(1, innerHandler.AuthorizationAttempts);
            Assert.AreEqual(1, innerHandler.OkCount);
        }
        public async Task TestHttpClientWithoutIntercept()
        {
            var innerHandler = new FakeHandler();

            var retryHandler = new RetryDelegatingHandler
            {
                InnerHandler = innerHandler
            };

            var client = new HttpClient(retryHandler);

            var response = await client.GetAsync("http://hello.world/test");

            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            Assert.AreEqual(1, innerHandler.FailedCount);
            Assert.AreEqual(0, innerHandler.AuthorizationAttempts);
            Assert.AreEqual(0, innerHandler.OkCount);
        }
        public async void TestHttpClientWithoutIntercept()
        {
            var innerHandler = new FakeHandler();

            var retryHandler = new RetryDelegatingHandler
                               {
                                   InnerHandler = innerHandler
                               };

            var client = new HttpClient(retryHandler);

            var response = await client.GetAsync("http://hello.world/test");

            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            Assert.AreEqual(1, innerHandler.FailedCount);
            Assert.AreEqual(0, innerHandler.AuthorizationAttempts);
            Assert.AreEqual(0, innerHandler.OkCount);
        }