/// <summary>Test helper for testing unsuccessful response handlers.</summary>
        private void SubtestSendAsyncUnsuccessfulReponseHanlder(HttpStatusCode code)
        {
            var handler = new UnsuccessfulResponseMessageHandler {
                ResponseStatusCode = code
            };

            var configurableHanlder = new ConfigurableMessageHandler(handler);
            var unsuccessfulHandler = new UnsuccessfulResponseMessageHandler.ServiceUnavailableResponseHandler();

            configurableHanlder.AddUnsuccessfulResponseHandler(unsuccessfulHandler);

            using (var client = new HttpClient(configurableHanlder))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-unsuccessful-handler");

                HttpResponseMessage response = client.SendAsync(request).Result;
                Assert.That(response.StatusCode, Is.EqualTo(code));

                // if service unavailable, retry will occur because we plugged unsuccessful response handler which
                // handles service unavailable responses
                if (code == HttpStatusCode.ServiceUnavailable)
                {
                    Assert.That(unsuccessfulHandler.Calls, Is.EqualTo(configurableHanlder.NumTries));
                    Assert.That(handler.Calls, Is.EqualTo(configurableHanlder.NumTries));
                }
                else
                {
                    // if status is OK, there isn't any call to unsuccessful response handler
                    Assert.That(unsuccessfulHandler.Calls, Is.EqualTo(code != HttpStatusCode.OK ? 1 : 0));
                    Assert.That(handler.Calls, Is.EqualTo(1));
                }
            }
        }
示例#2
0
        /// <summary>
        /// Tests the retry mechanism. In case the abnormal response is handled, there should be retries, but otherwise
        /// there should not be any retry.
        /// </summary>
        /// <param name="numTries"></param>
        /// <param name="handle"></param>
        private void SubtestSendAsyncNumTries(int numTries, bool handle = true)
        {
            var handler = new UnsuccessfulResponseMessageHandler
            {
                ResponseStatusCode = HttpStatusCode.ServiceUnavailable
            };
            var configurableHanlder = new ConfigurableMessageHandler(handler)
            {
                NumTries = numTries
            };

            if (handle)
            {
                var unsuccessfulHandler = new UnsuccessfulResponseMessageHandler.ServiceUnavailableResponseHandler();
                configurableHanlder.UnsuccessfulResponseHandlers.Add(unsuccessfulHandler);
            }

            using (var client = new HttpClient(configurableHanlder))
            {
                client.GetAsync("http://num-retres");
                Assert.That(handler.Calls, Is.EqualTo(handle ? numTries : 1));
            }
        }
        /// <summary>
        /// Tests the retry mechanism. In case the abnormal response is handled, there should be retries, but otherwise
        /// there should not be any retry.
        /// </summary>
        /// <param name="numTries"></param>
        /// <param name="handle"></param>
        private void SubtestSendAsyncNumTries(int numTries, bool handle = true)
        {
            var handler = new UnsuccessfulResponseMessageHandler
                {
                    ResponseStatusCode = HttpStatusCode.ServiceUnavailable
                };
            var configurableHanlder = new ConfigurableMessageHandler(handler)
                {
                    NumTries = numTries
                };
            if (handle)
            {
                var unsuccessfulHandler = new UnsuccessfulResponseMessageHandler.ServiceUnavailableResponseHandler();
                configurableHanlder.AddUnsuccessfulResponseHandler(unsuccessfulHandler);
            }

            using (var client = new HttpClient(configurableHanlder))
            {
                client.GetAsync("http://num-retres");
                Assert.That(handler.Calls, Is.EqualTo(handle ? numTries : 1));
            }
        }
        /// <summary>Test helper for testing unsuccessful response handlers.</summary>
        private void SubtestSendAsyncUnsuccessfulReponseHanlder(HttpStatusCode code)
        {
            var handler = new UnsuccessfulResponseMessageHandler { ResponseStatusCode = code };

            var configurableHanlder = new ConfigurableMessageHandler(handler);
            var unsuccessfulHandler = new UnsuccessfulResponseMessageHandler.ServiceUnavailableResponseHandler();
            configurableHanlder.AddUnsuccessfulResponseHandler(unsuccessfulHandler);

            using (var client = new HttpClient(configurableHanlder))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-unsuccessful-handler");

                HttpResponseMessage response = client.SendAsync(request).Result;
                Assert.That(response.StatusCode, Is.EqualTo(code));

                // if service unavailable, retry will occur because we plugged unsuccessful response handler which 
                // handles service unavailable responses
                if (code == HttpStatusCode.ServiceUnavailable)
                {
                    Assert.That(unsuccessfulHandler.Calls, Is.EqualTo(configurableHanlder.NumTries));
                    Assert.That(handler.Calls, Is.EqualTo(configurableHanlder.NumTries));
                }
                else
                {
                    // if status is OK, there isn't any call to unsuccessful response handler
                    Assert.That(unsuccessfulHandler.Calls, Is.EqualTo(code != HttpStatusCode.OK ? 1 : 0));
                    Assert.That(handler.Calls, Is.EqualTo(1));
                }
            }
        }