public void GetAuthTest_StatusOk_Success() { const string ExpectedResult = "12A63255-1388-AB5E-071C-FA35D27C4098"; HttpResponseMessage moqResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); moqResponse.Headers.Add(BadsecClient.AuthTokenHeader, ExpectedResult); 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.GetAuth().Result; IEnumerable <string> authTokenHeaders; bool authTokenFound = response.Headers.TryGetValues(BadsecClient.AuthTokenHeader, out authTokenHeaders); Assert.IsTrue( condition: authTokenFound, message: "There was no auth token header found."); Assert.AreEqual( expected: ExpectedResult, actual: authTokenHeaders.First(), message: "The auth token in the header is not what was expected."); }
public void GetAuthTests_RetryMechanism_Failure() { var testData = new[] { new { StatusCode = System.Net.HttpStatusCode.NotFound }, new { StatusCode = System.Net.HttpStatusCode.Unauthorized }, new { StatusCode = System.Net.HttpStatusCode.InternalServerError } }; foreach (var data in testData) { Console.WriteLine("Running the scenario - Status Code " + data.StatusCode.ToString()); HttpResponseMessage moqResponse = new HttpResponseMessage(data.StatusCode); 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.GetAuth().Result; IEnumerable <string> authTokenHeaders; bool authTokenFound = response.Headers.TryGetValues(BadsecClient.AuthTokenHeader, out authTokenHeaders); Assert.AreEqual( expected: data.StatusCode, actual: response.StatusCode, message: "The status codes were not the same."); Assert.IsNull( value: response.Content, message: "response.Content is not null."); Assert.IsFalse( condition: authTokenFound, message: "An auth token header was found."); moqHttpMessageHandler.Protected() .Verify <Task <HttpResponseMessage> >( "SendAsync", Times.Exactly(3), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); } }