|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Shouldly; |
| 6 | +using Volo.Abp.Cli.ProjectBuilding; |
| 7 | +using Volo.Abp.Json; |
| 8 | +using Volo.Abp.Json.SystemTextJson; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Volo.Abp.Cli.ProjectBuilding; |
| 12 | + |
| 13 | +public class RemoteServiceExceptionHandler_Tests |
| 14 | +{ |
| 15 | + private readonly RemoteServiceExceptionHandler _handler; |
| 16 | + |
| 17 | + public RemoteServiceExceptionHandler_Tests() |
| 18 | + { |
| 19 | + var jsonSerializer = new AbpSystemTextJsonSerializer( |
| 20 | + Microsoft.Extensions.Options.Options.Create(new AbpSystemTextJsonSerializerOptions()) |
| 21 | + ); |
| 22 | + _handler = new RemoteServiceExceptionHandler(jsonSerializer); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public async Task EnsureSuccessfulHttpResponseAsync_Should_Not_Throw_On_Success() |
| 27 | + { |
| 28 | + var response = new HttpResponseMessage(HttpStatusCode.OK) |
| 29 | + { |
| 30 | + Content = new StringContent("{}") |
| 31 | + }; |
| 32 | + |
| 33 | + await _handler.EnsureSuccessfulHttpResponseAsync(response); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task EnsureSuccessfulHttpResponseAsync_Should_Not_Throw_When_Response_Is_Null() |
| 38 | + { |
| 39 | + await _handler.EnsureSuccessfulHttpResponseAsync(null); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task Should_Wrap_Html_Body_Without_Json_Parse_Exception() |
| 44 | + { |
| 45 | + var response = new HttpResponseMessage(HttpStatusCode.Forbidden) |
| 46 | + { |
| 47 | + ReasonPhrase = "Forbidden", |
| 48 | + Content = new StringContent("<!DOCTYPE html><html><body>Forbidden</body></html>", System.Text.Encoding.UTF8, "text/html") |
| 49 | + }; |
| 50 | + |
| 51 | + var exception = await Should.ThrowAsync<Exception>(() => _handler.EnsureSuccessfulHttpResponseAsync(response)); |
| 52 | + |
| 53 | + exception.Message.ShouldContain("403-Forbidden"); |
| 54 | + exception.Message.ShouldNotContain("invalid start of a value"); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public async Task Should_Surface_Server_Error_Message_When_Body_Is_Valid_Json() |
| 59 | + { |
| 60 | + var response = new HttpResponseMessage(HttpStatusCode.Forbidden) |
| 61 | + { |
| 62 | + ReasonPhrase = "Forbidden", |
| 63 | + Content = new StringContent( |
| 64 | + "{\"error\":{\"code\":\"LicenseExpired\",\"message\":\"Your ABP license has expired.\"}}", |
| 65 | + System.Text.Encoding.UTF8, |
| 66 | + "application/json") |
| 67 | + }; |
| 68 | + |
| 69 | + var exception = await Should.ThrowAsync<Exception>(() => _handler.EnsureSuccessfulHttpResponseAsync(response)); |
| 70 | + |
| 71 | + exception.Message.ShouldContain("403-Forbidden"); |
| 72 | + exception.Message.ShouldContain("LicenseExpired"); |
| 73 | + exception.Message.ShouldContain("Your ABP license has expired."); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public async Task Should_Surface_Server_Error_Message_For_5xx_With_Json_Body() |
| 78 | + { |
| 79 | + var response = new HttpResponseMessage(HttpStatusCode.InternalServerError) |
| 80 | + { |
| 81 | + ReasonPhrase = "Internal Server Error", |
| 82 | + Content = new StringContent( |
| 83 | + "{\"error\":{\"code\":\"InternalError\",\"message\":\"Database connection failed\"}}", |
| 84 | + System.Text.Encoding.UTF8, |
| 85 | + "application/json") |
| 86 | + }; |
| 87 | + |
| 88 | + var exception = await Should.ThrowAsync<Exception>(() => _handler.EnsureSuccessfulHttpResponseAsync(response)); |
| 89 | + |
| 90 | + exception.Message.ShouldContain("500-Internal Server Error"); |
| 91 | + exception.Message.ShouldContain("InternalError"); |
| 92 | + exception.Message.ShouldContain("Database connection failed"); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public async Task GetAbpRemoteServiceErrorAsync_Should_Propagate_OperationCanceledException() |
| 97 | + { |
| 98 | + var response = new HttpResponseMessage(HttpStatusCode.Forbidden) |
| 99 | + { |
| 100 | + Content = new CanceledStringContent() |
| 101 | + }; |
| 102 | + |
| 103 | + await Should.ThrowAsync<OperationCanceledException>( |
| 104 | + () => _handler.GetAbpRemoteServiceErrorAsync(response) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public async Task GetAbpRemoteServiceErrorAsync_Should_Return_Null_For_Html_Body() |
| 110 | + { |
| 111 | + var response = new HttpResponseMessage(HttpStatusCode.Forbidden) |
| 112 | + { |
| 113 | + Content = new StringContent("<!DOCTYPE html><html></html>", System.Text.Encoding.UTF8, "text/html") |
| 114 | + }; |
| 115 | + |
| 116 | + var result = await _handler.GetAbpRemoteServiceErrorAsync(response); |
| 117 | + |
| 118 | + result.ShouldBeNull(); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task GetAbpRemoteServiceErrorAsync_Should_Return_Null_For_Newtonsoft_JsonException() |
| 123 | + { |
| 124 | + var handler = new RemoteServiceExceptionHandler( |
| 125 | + new ThrowingJsonSerializer(new Newtonsoft.Json.JsonException("Invalid JSON")) |
| 126 | + ); |
| 127 | + var response = new HttpResponseMessage(HttpStatusCode.Forbidden) |
| 128 | + { |
| 129 | + Content = new StringContent("{}") |
| 130 | + }; |
| 131 | + |
| 132 | + var result = await handler.GetAbpRemoteServiceErrorAsync(response); |
| 133 | + |
| 134 | + result.ShouldBeNull(); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public async Task GetAbpRemoteServiceErrorAsync_Should_Propagate_Non_Json_Exceptions() |
| 139 | + { |
| 140 | + var handler = new RemoteServiceExceptionHandler( |
| 141 | + new ThrowingJsonSerializer(new InvalidOperationException("Unexpected serializer failure")) |
| 142 | + ); |
| 143 | + var response = new HttpResponseMessage(HttpStatusCode.Forbidden) |
| 144 | + { |
| 145 | + Content = new StringContent("{}") |
| 146 | + }; |
| 147 | + |
| 148 | + var exception = await Should.ThrowAsync<InvalidOperationException>( |
| 149 | + () => handler.GetAbpRemoteServiceErrorAsync(response) |
| 150 | + ); |
| 151 | + |
| 152 | + exception.Message.ShouldBe("Unexpected serializer failure"); |
| 153 | + } |
| 154 | + |
| 155 | + private class CanceledStringContent : HttpContent |
| 156 | + { |
| 157 | + protected override Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext context) |
| 158 | + { |
| 159 | + throw new OperationCanceledException(); |
| 160 | + } |
| 161 | + |
| 162 | + protected override bool TryComputeLength(out long length) |
| 163 | + { |
| 164 | + length = 0; |
| 165 | + return false; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private class ThrowingJsonSerializer : IJsonSerializer |
| 170 | + { |
| 171 | + private readonly Exception _exception; |
| 172 | + |
| 173 | + public ThrowingJsonSerializer(Exception exception) |
| 174 | + { |
| 175 | + _exception = exception; |
| 176 | + } |
| 177 | + |
| 178 | + public string Serialize(object obj, bool camelCase = true, bool indented = false) |
| 179 | + { |
| 180 | + throw new NotImplementedException(); |
| 181 | + } |
| 182 | + |
| 183 | + public T Deserialize<T>(string jsonString, bool camelCase = true) |
| 184 | + { |
| 185 | + throw _exception; |
| 186 | + } |
| 187 | + |
| 188 | + public object Deserialize(Type type, string jsonString, bool camelCase = true) |
| 189 | + { |
| 190 | + throw _exception; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments