|
| 1 | +using System.Security.Authentication; |
| 2 | + |
| 3 | +namespace RestSharp.Tests; |
| 4 | + |
| 5 | +public class ErrorMessageTests { |
| 6 | + [Fact] |
| 7 | + public async Task ErrorMessage_surfaces_innermost_exception_message() { |
| 8 | + const string innerMessage = "The remote certificate is invalid according to the validation procedure."; |
| 9 | + |
| 10 | + var innerException = new AuthenticationException(innerMessage); |
| 11 | + var wrappedException = new HttpRequestException("An error occurred while sending the request.", innerException); |
| 12 | + |
| 13 | + var handler = new FakeHandler(wrappedException); |
| 14 | + var client = new RestClient(new RestClientOptions("https://dummy.org") { ConfigureMessageHandler = _ => handler }); |
| 15 | + |
| 16 | + var response = await client.ExecuteAsync(new RestRequest("/")); |
| 17 | + |
| 18 | + response.ErrorMessage.Should().Be(innerMessage); |
| 19 | + response.ErrorException.Should().BeOfType<HttpRequestException>(); |
| 20 | + response.ErrorException!.InnerException.Should().BeOfType<AuthenticationException>(); |
| 21 | + response.ResponseStatus.Should().Be(ResponseStatus.Error); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public async Task ErrorMessage_uses_direct_message_when_no_inner_exception() { |
| 26 | + const string message = "No such host is known."; |
| 27 | + |
| 28 | + var exception = new HttpRequestException(message); |
| 29 | + var handler = new FakeHandler(exception); |
| 30 | + var client = new RestClient(new RestClientOptions("https://dummy.org") { ConfigureMessageHandler = _ => handler }); |
| 31 | + |
| 32 | + var response = await client.ExecuteAsync(new RestRequest("/")); |
| 33 | + |
| 34 | + response.ErrorMessage.Should().Be(message); |
| 35 | + response.ErrorException.Should().BeOfType<HttpRequestException>(); |
| 36 | + response.ErrorException!.InnerException.Should().BeNull(); |
| 37 | + } |
| 38 | + |
| 39 | + class FakeHandler(Exception exception) : HttpMessageHandler { |
| 40 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 41 | + => throw exception; |
| 42 | + } |
| 43 | +} |
0 commit comments