Skip to content

Commit

Permalink
Handle !OK responses for Autocomplete.
Browse files Browse the repository at this point in the history
  • Loading branch information
PureKrome committed Jul 5, 2017
1 parent 22f46b8 commit 2cc30ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/SimpleGoogleWebServices/GooglePlacesApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public async Task<AutocompleteResult> AutocompleteAsync(string query)
{
var errorMessage =
$"Failed to retrieve a Google Maps Autocomplete result. Status Code: {response.StatusCode}. Message: {content}";
throw new Exception(errorMessage);
return new AutocompleteResult
{
Status = response.StatusCode.ToString(),
ErrorMessage = errorMessage,
Results = Enumerable.Empty<Autocomplete.Address>()
};
}

// Get content from json into rich object model.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Shouldly;
Expand All @@ -14,17 +15,20 @@ namespace WorldDomination.Spatial.SimpleGoogleWebServices.Tests.GooglePlacesApiS
public class AutocompleteAsyncTests
{
[Theory]
[InlineData("Invalid Request", "INVALID_REQUEST")]
[InlineData("Request Denied", "REQUEST_DENIED")]
[InlineData("Zero Results", "ZERO_RESULTS")]
[InlineData("Unknown Error", "InternalServerError", HttpStatusCode.InternalServerError)]
[InlineData("Invalid Request", "INVALID_REQUEST", HttpStatusCode.OK)]
[InlineData("Request Denied", "REQUEST_DENIED", HttpStatusCode.OK)]
[InlineData("Zero Results", "ZERO_RESULTS", HttpStatusCode.OK)]
public async Task GivenAnInvalidRequest_AutocompleteAsync_ReturnsAnErrorResult(string fileName,
string status)
string status,
HttpStatusCode statusCode)
{
// Arrange.
var json = File.ReadAllText($"Sample Data\\Autocomplete\\{fileName}.json");
var options = new HttpMessageOptions
{
HttpResponseMessage = FakeHttpMessageHandler.GetStringHttpResponseMessage(json)

HttpResponseMessage = FakeHttpMessageHandler.GetStringHttpResponseMessage(json, statusCode)
};
var httpClient = new HttpClient(new FakeHttpMessageHandler(options));
var service = new GooglePlacesApiService("aaa", httpClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"predictions": [],
"status": "UNKNOWN_ERROR"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
</ItemGroup>

<ItemGroup>
<None Update="Sample Data\Autocomplete\Unknown Error.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Sample Data\Autocomplete\Zero Results.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 2cc30ec

Please sign in to comment.