public void GetHttpWithRetryTest_ExceptionHandling()
        {
            Mock <HttpMessageHandler> moqHttpMessageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            moqHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ThrowsAsync(new Exception());

            HttpClient   httpClient = new HttpClient(moqHttpMessageHandler.Object);
            BadsecClient client     = new BadsecClient(httpClient);

            try
            {
                HttpResponseMessage response =
                    client.GetHttpWithRetry(It.IsAny <string>(), It.IsAny <HttpCompletionOption>(), It.IsAny <string>()).Result;
            }
            catch (AggregateException aggregateException)
            {
                Assert.AreEqual(
                    expected: aggregateException.GetType(),
                    actual: typeof(AggregateException),
                    message: "The actual returned exception is not an AggregateException.");

                moqHttpMessageHandler.Protected()
                .Verify <Task <HttpResponseMessage> >(
                    "SendAsync",
                    Times.Exactly(3),
                    ItExpr.IsAny <HttpRequestMessage>(),
                    ItExpr.IsAny <CancellationToken>());
            }
        }
        public void GetHttpWithRetryTest_ExceptionHandling_Success()
        {
            Mock <HttpMessageHandler> moqHttpMessageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            moqHttpMessageHandler.Protected()
            .SetupSequence <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ThrowsAsync(new Exception())
            .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK));

            HttpClient   httpClient = new HttpClient(moqHttpMessageHandler.Object);
            BadsecClient client     = new BadsecClient(httpClient);

            HttpResponseMessage response =
                client.GetHttpWithRetry(It.IsAny <string>(), It.IsAny <HttpCompletionOption>(), It.IsAny <string>()).Result;

            Assert.AreEqual(
                expected: System.Net.HttpStatusCode.OK,
                actual: response.StatusCode,
                message: "The status codes were not the same.");

            moqHttpMessageHandler.Protected()
            .Verify <Task <HttpResponseMessage> >(
                "SendAsync",
                Times.Exactly(2),
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>());
        }
        public void GetHttpWithRetryTest()
        {
            var testData = new[]
            {
                new
                {
                    StatusCode  = System.Net.HttpStatusCode.NotFound,
                    Endpoint    = "endpoint",
                    Checksum    = String.Empty,
                    Content     = "Endpoint not found",
                    TimesCalled = 3
                },
                new
                {
                    StatusCode  = System.Net.HttpStatusCode.InternalServerError,
                    Endpoint    = "users",
                    Checksum    = String.Empty,
                    Content     = "You must authorize yourself before calling /users",
                    TimesCalled = 3
                },
                new
                {
                    StatusCode  = System.Net.HttpStatusCode.OK,
                    Endpoint    = "users",
                    Checksum    = "checksum",
                    Content     = "123456789,123456789,123456789",
                    TimesCalled = 1
                },
                new
                {
                    StatusCode  = System.Net.HttpStatusCode.InternalServerError,
                    Endpoint    = "users",
                    Checksum    = "checksum",
                    Content     = "Invalid checksum, expected 92D5FF242A0 but got checksum",
                    TimesCalled = 3
                }
            };

            foreach (var data in testData)
            {
                Console.WriteLine(
                    "Running the scenario - Calling endpoint /" + data.Endpoint +
                    ". Returning Status Code " + data.StatusCode.ToString());

                HttpResponseMessage moqResponse = new HttpResponseMessage(data.StatusCode);
                moqResponse.Content = new StringContent(data.Content);

                Mock <HttpMessageHandler> moqHttpMessageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict);
                moqHttpMessageHandler.Protected()
                .Setup <Task <HttpResponseMessage> >(
                    "SendAsync",
                    ItExpr.IsAny <HttpRequestMessage>(),
                    ItExpr.IsAny <CancellationToken>())
                .ReturnsAsync(moqResponse);

                HttpClient   httpClient = new HttpClient(moqHttpMessageHandler.Object);
                BadsecClient client     = new BadsecClient(httpClient);

                HttpResponseMessage response =
                    client.GetHttpWithRetry(data.Endpoint, It.IsAny <HttpCompletionOption>(), It.IsAny <string>()).Result;
                string responseString = response.Content.ReadAsStringAsync().Result;

                Assert.AreEqual(
                    expected: data.StatusCode,
                    actual: moqResponse.StatusCode,
                    message: "The status codes were not the same.");

                Assert.AreEqual(
                    expected: data.Content,
                    actual: responseString,
                    message: "response content is not the same.");

                moqHttpMessageHandler.Protected()
                .Verify <Task <HttpResponseMessage> >(
                    "SendAsync",
                    Times.Exactly(data.TimesCalled),
                    ItExpr.IsAny <HttpRequestMessage>(),
                    ItExpr.IsAny <CancellationToken>());
            }
        }