|
1 | | -using Geocoding.Google; |
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using Geocoding.Google; |
2 | 4 | using Xunit; |
3 | 5 |
|
4 | 6 | namespace Geocoding.Tests; |
@@ -206,8 +208,58 @@ public void GoogleGeocodingException_WithoutProviderMessage_LeavesProviderMessag |
206 | 208 | Assert.Contains("OverQueryLimit", exception.Message); |
207 | 209 | } |
208 | 210 |
|
| 211 | + [Fact] |
| 212 | + public async Task Geocode_HttpFailure_PreservesInnerExceptionPreview() |
| 213 | + { |
| 214 | + // Arrange |
| 215 | + var body = new string('x', 300); |
| 216 | + var geocoder = new TestableGoogleGeocoder(new TestHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest) |
| 217 | + { |
| 218 | + ReasonPhrase = "Bad Request", |
| 219 | + Content = new StringContent(body) |
| 220 | + }))); |
| 221 | + |
| 222 | + // Act |
| 223 | + var exception = await Assert.ThrowsAsync<GoogleGeocodingException>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken)); |
| 224 | + |
| 225 | + // Assert |
| 226 | + Assert.NotNull(exception.InnerException); |
| 227 | + Assert.Contains("Google request failed (400 Bad Request).", exception.InnerException!.Message, StringComparison.Ordinal); |
| 228 | + Assert.Contains("Response preview:", exception.InnerException.Message, StringComparison.Ordinal); |
| 229 | + Assert.DoesNotContain(body, exception.InnerException.Message, StringComparison.Ordinal); |
| 230 | + } |
| 231 | + |
| 232 | + [Fact] |
| 233 | + public async Task Geocode_TransportFailure_WrapsInnerException() |
| 234 | + { |
| 235 | + // Arrange |
| 236 | + var geocoder = new TestableGoogleGeocoder(new TestHttpMessageHandler((_, _) => throw new HttpRequestException("socket failure"))); |
| 237 | + |
| 238 | + // Act |
| 239 | + var exception = await Assert.ThrowsAsync<GoogleGeocodingException>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken)); |
| 240 | + |
| 241 | + // Assert |
| 242 | + Assert.IsType<HttpRequestException>(exception.InnerException); |
| 243 | + Assert.Contains("socket failure", exception.InnerException!.Message, StringComparison.Ordinal); |
| 244 | + } |
| 245 | + |
209 | 246 | private static bool HasShortName(GoogleAddress address, string shortName) |
210 | 247 | { |
211 | 248 | return address.Components.Any(component => String.Equals(component.ShortName, shortName, StringComparison.Ordinal)); |
212 | 249 | } |
| 250 | + |
| 251 | + private sealed class TestableGoogleGeocoder : GoogleGeocoder |
| 252 | + { |
| 253 | + private readonly HttpMessageHandler _handler; |
| 254 | + |
| 255 | + public TestableGoogleGeocoder(HttpMessageHandler handler) |
| 256 | + { |
| 257 | + _handler = handler; |
| 258 | + } |
| 259 | + |
| 260 | + protected override HttpClient BuildClient() |
| 261 | + { |
| 262 | + return new HttpClient(_handler, disposeHandler: false); |
| 263 | + } |
| 264 | + } |
213 | 265 | } |
0 commit comments