public void GetUsersTests_RetryMechanism_Failure() { var testData = new[] { new { StatusCode = System.Net.HttpStatusCode.Unauthorized, Content = "Unauthorized call", }, new { StatusCode = System.Net.HttpStatusCode.InternalServerError, Content = "You must authorize yourself before calling /users", } }; foreach (var data in testData) { Console.WriteLine("Running the scenario - 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.GetUsers(It.IsAny <string>()).Result; string responseString = response.Content.ReadAsStringAsync().Result; Assert.AreEqual( expected: data.StatusCode, actual: response.StatusCode, message: "The status codes were not the same."); Assert.AreEqual( expected: data.Content, actual: responseString, message: "response content was not the same."); moqHttpMessageHandler.Protected() .Verify <Task <HttpResponseMessage> >( "SendAsync", Times.Exactly(3), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); } }
public void GetUsersTests_MultipleRetries_Success() { const string ErrorCallResponse = "The BADSEC server timed out"; const string OKCallResponse = "123456789\n234567891\n345678912\n"; IList <HttpResponseMessage> responseList = new List <HttpResponseMessage> { new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError), new HttpResponseMessage(System.Net.HttpStatusCode.OK), }; responseList[0].Content = new StringContent(ErrorCallResponse); responseList[1].Content = new StringContent(OKCallResponse); Mock <HttpMessageHandler> moqHttpMessageHandler = new Mock <HttpMessageHandler>(MockBehavior.Strict); moqHttpMessageHandler.Protected() .SetupSequence <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(responseList[0]) .ReturnsAsync(responseList[1]); HttpClient httpClient = new HttpClient(moqHttpMessageHandler.Object); BadsecClient client = new BadsecClient(httpClient); HttpResponseMessage response = client.GetUsers(It.IsAny <string>()).Result; string responseString = response.Content.ReadAsStringAsync().Result; Assert.AreEqual( expected: System.Net.HttpStatusCode.OK, actual: response.StatusCode, message: "The status codes were not the same."); Assert.AreEqual( expected: OKCallResponse, actual: responseString, message: "response content was not the same."); moqHttpMessageHandler.Protected() .Verify <Task <HttpResponseMessage> >( "SendAsync", Times.Exactly(2), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); }