Пример #1
0
        public void HttpRequestTests_ExceptionHandling()
        {
            IList <Exception> exceptionsToThrow = new List <Exception>
            {
                new TaskCanceledException(),
                new HttpRequestException(),
                new InsufficientMemoryException(),
                new Exception()
            };

            foreach (var exception in exceptionsToThrow)
            {
                Console.WriteLine("Running the scenario - Throwing " + exception.ToString());

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

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

                try
                {
                    HttpResponseMessage response =
                        client.HttpRequest(
                            new HttpRequestMessage(HttpMethod.Get, "endpoint"),
                            It.IsAny <HttpCompletionOption>()).Result;
                }
                catch (Exception ex)
                {
                    Assert.AreEqual(
                        expected: exception.GetType(),
                        actual: ex.GetBaseException().GetType(),
                        message: "The exceptions are not the same.");
                }
            }
        }
Пример #2
0
        public void HttpRequestTests_StatusCodes()
        {
            var testData = new[]
            {
                new
                {
                    StatusCode = System.Net.HttpStatusCode.OK,
                },
                new
                {
                    StatusCode = System.Net.HttpStatusCode.InternalServerError
                }
            };

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

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

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

                HttpResponseMessage response =
                    client.HttpRequest(
                        new HttpRequestMessage(HttpMethod.Get, "endpoint"),
                        It.IsAny <HttpCompletionOption>()).Result;

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