Skip to content

Commit 55506b2

Browse files
committed
Tighten transport regression coverage
Root cause: the new transport hardening tests still had analyzer noise and one locale-sensitive Google channel edge case without regression coverage.
1 parent 39afda4 commit 55506b2

9 files changed

Lines changed: 51 additions & 34 deletions

src/Geocoding.Google/BusinessKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public string? Channel
4242
{
4343
return;
4444
}
45-
string formattedChannel = value!.Trim().ToLower();
45+
string formattedChannel = value!.Trim().ToLowerInvariant();
4646
if (Regex.IsMatch(formattedChannel, @"^[a-z_0-9.-]+$"))
4747
{
4848
_channel = formattedChannel;

test/Geocoding.Tests/AzureMapsAsyncTest.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ public async Task Geocode_HttpFailure_UsesTrimmedPreviewMessage()
105105
{
106106
// Arrange
107107
var body = new string('x', 300);
108-
var geocoder = new TestableAzureMapsGeocoder(new TestHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)
109-
{
110-
ReasonPhrase = "Bad Request",
111-
Content = new StringContent(body)
112-
})));
108+
var geocoder = new TestableAzureMapsGeocoder(new TestHttpMessageHandler((_, _) => TestHttpMessageHandler.CreateResponseAsync(HttpStatusCode.BadRequest, "Bad Request", body)));
113109

114110
// Act
115111
var exception = await Assert.ThrowsAsync<AzureMapsGeocodingException>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken));
@@ -123,7 +119,7 @@ public async Task Geocode_HttpFailure_UsesTrimmedPreviewMessage()
123119
private static AzureMapsAddress[] ParseResponse(AzureMapsGeocoder geocoder, string json)
124120
{
125121
var responseType = typeof(AzureMapsGeocoder).GetNestedType("AzureSearchResponse", BindingFlags.NonPublic)!;
126-
var response = JsonSerializer.Deserialize(json, responseType);
122+
var response = JsonSerializer.Deserialize(json, responseType, Extensions.JsonOptions);
127123
var parseMethod = typeof(AzureMapsGeocoder).GetMethod("ParseResponse", BindingFlags.Instance | BindingFlags.NonPublic)!;
128124

129125
var results = (IEnumerable)parseMethod.Invoke(geocoder, [response!])!;

test/Geocoding.Tests/BingMapsTest.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,7 @@ public async Task Geocode_HttpFailure_PreservesInnerExceptionPreview()
220220
{
221221
// Arrange
222222
var body = new string('x', 300);
223-
var geocoder = new TestableBingMapsGeocoder(new TestHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)
224-
{
225-
ReasonPhrase = "Bad Request",
226-
Content = new StringContent(body)
227-
})));
223+
var geocoder = new TestableBingMapsGeocoder(new TestHttpMessageHandler((_, _) => TestHttpMessageHandler.CreateResponseAsync(HttpStatusCode.BadRequest, "Bad Request", body)));
228224

229225
// Act
230226
var exception = await Assert.ThrowsAsync<BingGeocodingException>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken));

test/Geocoding.Tests/GoogleBusinessKeyTest.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Geocoding.Google;
1+
using System.Globalization;
2+
using Geocoding.Google;
23
using Xunit;
34

45
namespace Geocoding.Tests;
@@ -96,7 +97,32 @@ public void Constructor_ChannelWithWhitespace_TrimsAndLowercases(string channel)
9697
var key = new BusinessKey("client-id", "signature", channel);
9798

9899
// Assert
99-
Assert.Equal(channel.Trim().ToLower(), key.Channel);
100+
Assert.Equal(channel.Trim().ToLowerInvariant(), key.Channel);
101+
}
102+
103+
[Fact]
104+
public void Constructor_ChannelNormalization_IsCultureInvariant()
105+
{
106+
// Arrange
107+
var originalCulture = CultureInfo.CurrentCulture;
108+
var originalUICulture = CultureInfo.CurrentUICulture;
109+
110+
try
111+
{
112+
CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
113+
CultureInfo.CurrentUICulture = new CultureInfo("tr-TR");
114+
115+
// Act
116+
var key = new BusinessKey("client-id", "signature", "CHANNELI");
117+
118+
// Assert
119+
Assert.Equal("channeli", key.Channel);
120+
}
121+
finally
122+
{
123+
CultureInfo.CurrentCulture = originalCulture;
124+
CultureInfo.CurrentUICulture = originalUICulture;
125+
}
100126
}
101127

102128
[Theory]

test/Geocoding.Tests/GoogleGeocoderTest.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,7 @@ public async Task Geocode_HttpFailure_PreservesInnerExceptionPreview()
213213
{
214214
// Arrange
215215
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-
})));
216+
var geocoder = new TestableGoogleGeocoder(new TestHttpMessageHandler((_, _) => TestHttpMessageHandler.CreateResponseAsync(HttpStatusCode.BadRequest, "Bad Request", body)));
221217

222218
// Act
223219
var exception = await Assert.ThrowsAsync<GoogleGeocodingException>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken));

test/Geocoding.Tests/HereAsyncGeocoderTest.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ public async Task Geocode_HttpFailure_UsesTrimmedPreviewMessage()
6767
{
6868
// Arrange
6969
var body = new string('x', 300);
70-
var geocoder = new TestableHereGeocoder(new TestHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)
71-
{
72-
ReasonPhrase = "Bad Request",
73-
Content = new StringContent(body)
74-
})));
70+
var geocoder = new TestableHereGeocoder(new TestHttpMessageHandler((_, _) => TestHttpMessageHandler.CreateResponseAsync(HttpStatusCode.BadRequest, "Bad Request", body)));
7571

7672
// Act
7773
var exception = await Assert.ThrowsAsync<HereGeocodingException>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken));

test/Geocoding.Tests/MapQuestGeocoderTest.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net.Http;
22
using System.Net;
3-
using System.Net.Http;
43
using System.Reflection;
54
using Geocoding.MapQuest;
65
using Xunit;
@@ -85,11 +84,7 @@ public async Task Geocode_StatusFailure_UsesTrimmedPreviewMessage()
8584
{
8685
// Arrange
8786
var body = new string('x', 300);
88-
var geocoder = new TestableMapQuestGeocoder(new TestHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadGateway)
89-
{
90-
ReasonPhrase = "Bad Gateway",
91-
Content = new StringContent(body)
92-
})));
87+
var geocoder = new TestableMapQuestGeocoder(new TestHttpMessageHandler((_, _) => TestHttpMessageHandler.CreateResponseAsync(HttpStatusCode.BadGateway, "Bad Gateway", body)));
9388

9489
// Act
9590
var exception = await Assert.ThrowsAsync<Exception>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken));

test/Geocoding.Tests/TestHttpMessageHandler.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Net;
12
using System.Net.Http;
3+
using System.Text;
24

35
namespace Geocoding.Tests;
46

@@ -15,4 +17,17 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
1517
{
1618
return _sendAsync(request, cancellationToken);
1719
}
20+
21+
public static Task<HttpResponseMessage> CreateResponseAsync(HttpStatusCode statusCode, string? reasonPhrase = null, string? body = null)
22+
{
23+
var response = new HttpResponseMessage(statusCode)
24+
{
25+
ReasonPhrase = reasonPhrase
26+
};
27+
28+
if (!String.IsNullOrWhiteSpace(body))
29+
response.Content = new StringContent(body, Encoding.UTF8, "text/plain");
30+
31+
return Task.FromResult(response);
32+
}
1833
}

test/Geocoding.Tests/YahooGeocoderTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ public async Task Geocode_StatusFailure_WrapsHttpRequestException()
106106
{
107107
// Arrange
108108
var body = new string('x', 300);
109-
var geocoder = new TestableYahooGeocoder(new TestHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.Unauthorized)
110-
{
111-
Content = new StringContent(body)
112-
})));
109+
var geocoder = new TestableYahooGeocoder(new TestHttpMessageHandler((_, _) => TestHttpMessageHandler.CreateResponseAsync(HttpStatusCode.Unauthorized, "Unauthorized", body)));
113110

114111
// Act
115112
var exception = await Assert.ThrowsAsync<YahooGeocodingException>(() => geocoder.GeocodeAsync("1600 pennsylvania ave nw, washington dc", TestContext.Current.CancellationToken));

0 commit comments

Comments
 (0)