public void LtsaExceptionClientModel_Success() { // Arrange var message = "test2"; var status = HttpStatusCode.OK; var helper = new TestHelper(); var user = PrincipalHelper.CreateForPermission(); var options = Options.Create(new LtsaOptions()); helper.Create <LtsaService>(options, user); var exception = new HttpClientRequestException(message, status); var client = helper.GetService <Mock <IHttpRequestClient> >().Object; var error = new Error() { ErrorMessages = new List <string>() { "errorMessages" } }; // Act var ltsaException = new LtsaException(exception, client, error); // Assert Assert.NotNull(ltsaException.Message); ltsaException.Message.Should().Be(message + System.Environment.NewLine); ltsaException.Client.Should().Be(client); ltsaException.StatusCode.Should().Be(status); ltsaException.Detail.Should().Be(null); }
private async Task <Error> GetLtsaError(HttpClientRequestException ex, string url) { Error error = null; if (ex?.Response?.Content != null) { error = JsonSerializer.Deserialize <Error>(await ex.Response.Content.ReadAsStringAsync(), _jsonSerializerOptions); _logger.LogError(ex, $"Failed to send/receive request: ${url}"); } return(error); }
public void Constructor_Message() { // Arrange var msg = "message"; // Act var exception = new HttpClientRequestException(msg); // Assert Assert.NotNull(exception); exception.Message.Should().Be(msg); exception.StatusCode.Should().Be(HttpStatusCode.InternalServerError); }
public void Constructor_MessageAndStatusCode() { // Arrange var msg = "message"; var status = HttpStatusCode.Accepted; // Act var exception = new HttpClientRequestException(msg, status); // Assert Assert.NotNull(exception); exception.Message.Should().Be(msg); exception.StatusCode.Should().Be(HttpStatusCode.Accepted); }
public void Constructor_ResponseAndInnerException_ArgumentNullException() { // Arrange var response = new HttpResponseMessage() { RequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://test.com/"), Content = new StringContent("test") }; var inner = new HttpClientRequestException(response); // Act // Assert Assert.Throws <ArgumentNullException>(() => new HttpClientRequestException((HttpResponseMessage)null, inner)); }
public void LtsaExceptionMessageInnerExceptionStatus_Success() { // Arrange var message = "test for inner exception 2"; var status = HttpStatusCode.OK; var innerException = new HttpClientRequestException(message, status); var ltsaException = new LtsaException(message, innerException, status); // Assert Assert.NotNull(ltsaException); ltsaException.Message.Should().Be(message); ltsaException.InnerException.Should().Be(innerException); ltsaException.StatusCode.Should().Be(status); }
public void Constructor_MessageAndInnerExceptionAndStatus() { // Arrange var message = "test for inner exception 2"; var status = HttpStatusCode.OK; var innerException = new HttpClientRequestException(message, status); var chesException = new ChesException(message, innerException, status); // Assert Assert.NotNull(chesException); chesException.Message.Should().Be(message); chesException.InnerException.Should().Be(innerException); chesException.StatusCode.Should().Be(status); }
public void Constructor_MessageAndInnerException() { // Arrange var msg = "message"; var inner = new Exception(msg); // Act var exception = new HttpClientRequestException(msg, inner); // Assert Assert.NotNull(exception); exception.Message.Should().Be(msg); exception.InnerException.Should().NotBeNull(); exception.InnerException.Message.Should().Be(msg); exception.Response.Should().BeNull(); }
/// <summary> /// Send an HTTP request to the specified 'url'. /// </summary> /// <param name="url"></param> /// <param name="method"></param> /// <param name="headers"></param> /// <param name="content"></param> /// <param name="onError"></param> /// <exception cref="HttpClientRequestException">Response did not return a success status code.</exception> /// <returns></returns> public virtual async Task <TModel> SendAsync <TModel>(string url, HttpMethod method, HttpRequestHeaders headers, HttpContent content = null, Func <HttpResponseMessage, bool> onError = null) { var response = await SendAsync(url, method, headers, content); if (response.IsSuccessStatusCode) { return(await DeserializeAsync <TModel>(response)); } // If the error handle is not provided, or if it returns false throw an error. if ((onError?.Invoke(response) ?? false) == false) { var error = new HttpClientRequestException(response); _logger.LogError(error, error.Message); throw error; } return(default);
public void ChesExceptionResponseInnerException_Success() { // Arrange var message = "test for inner exception"; var status = HttpStatusCode.OK; var innerException = new HttpClientRequestException(message, status); var response = new HttpResponseMessage(status) { RequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://test") }; var chesException = new ChesException(response, innerException); // Assert Assert.NotNull(chesException); chesException.Response.Should().Be(response); chesException.InnerException.Should().Be(innerException); }
public void Constructor_WithResponse() { // Arrange var response = new HttpResponseMessage() { RequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://test.com/"), Content = new StringContent("test") }; // Act var exception = new HttpClientRequestException(response); // Assert Assert.NotNull(exception); exception.Message.Should().Be($"HTTP Request '{response?.RequestMessage.RequestUri}' failed"); exception.InnerException.Should().BeNull(); exception.Response.Should().Be(response); }
public void Constructor_ResponseAndMessage() { // Arrange var msg = "message"; var response = new HttpResponseMessage() { RequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://test.com/"), Content = new StringContent("test") }; // Act var exception = new HttpClientRequestException(response, msg); // Assert Assert.NotNull(exception); exception.Message.Should().Be(msg); exception.InnerException.Should().BeNull(); exception.Response.Should().Be(response); }
public void Constructor_NoStatus_ExceptionAndClientAndModel() { // Arrange var helper = new TestHelper(); var user = PrincipalHelper.CreateForPermission(); var options = Options.Create(new ChesOptions() { AuthUrl = "https:/test.com", EmailEnabled = true, EmailAuthorized = true, BccUser = true }); var service = helper.Create <ChesService>(options, user); var token = new TokenModel() { AccessToken = "test" }; var response = new HttpResponseMessage() { RequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://test") }; var exception = new HttpClientRequestException(response); var client = helper.GetService <Mock <IHttpRequestClient> >().Object; var model = new ErrorResponseModel(); // Act var chesException = new ChesException(exception, client, model); // Assert Assert.NotNull(chesException.Message); chesException.Message.Should().Be("HTTP Request 'https://test/' failed" + System.Environment.NewLine); chesException.Client.Should().Be(client); chesException.StatusCode.Should().Be(HttpStatusCode.OK); chesException.Detail.Should().Be(model.Title + System.Environment.NewLine + model.Detail + System.Environment.NewLine + model.Type + System.Environment.NewLine + System.String.Join(System.Environment.NewLine, model.Errors.Select(e => $"\t{e.Message}"))); }
private async Task <Error> GetLtsaError(HttpClientRequestException ex, string url) { Error error = null; if (ex?.Response?.Content != null) { var errorContent = await ex.Response.Content.ReadAsStringAsync(); try { error = JsonSerializer.Deserialize <Error>(errorContent, _jsonSerializerOptions); } catch (JsonException) { error = new Error(new List <String>() { ex.Message }); } _logger.LogError(ex, $"Failed to send/receive request: ${url}"); } return(error); }