Skip to content

Commit

Permalink
Merge pull request #1536 from stripe/ob-check-valid-json
Browse files Browse the repository at this point in the history
Check validity of JSON in OK responses
  • Loading branch information
ob-stripe authored Feb 22, 2019
2 parents 607c8ee + f42c1a2 commit 31d32e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/Stripe.net/Infrastructure/Public/StripeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ private static T ProcessResponse<T>(StripeResponse response)
throw BuildStripeException(response);
}

var obj = StripeEntity.FromJson<T>(response.Content);
T obj;
try
{
obj = StripeEntity.FromJson<T>(response.Content);
}
catch (Newtonsoft.Json.JsonException)
{
throw BuildInvalidResponseException(response);
}

obj.StripeResponse = response;

return obj;
Expand Down Expand Up @@ -111,7 +120,7 @@ private static StripeException BuildInvalidResponseException(StripeResponse resp
return new StripeException(
response.StatusCode,
null,
$"Invalid response object from API: {response.Content}")
$"Invalid response object from API: \"{response.Content}\"")
{
StripeResponse = response,
};
Expand Down
23 changes: 21 additions & 2 deletions src/StripeTests/Infrastructure/Public/StripeClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ public async Task RequestAsync_OkResponse()
Assert.Equal(response, charge.StripeResponse);
}

[Fact]
public async Task RequestAsync_OkResponse_InvalidJson()
{
var response = new StripeResponse(HttpStatusCode.OK, null, "this isn't JSON");
this.httpClient.Response = response;

var exception = await Assert.ThrowsAsync<StripeException>(async () =>
await this.stripeClient.RequestAsync<Charge>(
HttpMethod.Post,
"/v1/charges",
this.options,
this.requestOptions));

Assert.NotNull(exception);
Assert.Equal(HttpStatusCode.OK, exception.HttpStatusCode);
Assert.Equal("Invalid response object from API: \"this isn't JSON\"", exception.Message);
Assert.Equal(response, exception.StripeResponse);
}

[Fact]
public async Task RequestAsync_ApiError()
{
Expand Down Expand Up @@ -106,7 +125,7 @@ await this.stripeClient.RequestAsync<Charge>(

Assert.NotNull(exception);
Assert.Equal(HttpStatusCode.InternalServerError, exception.HttpStatusCode);
Assert.Equal("Invalid response object from API: this isn't JSON", exception.Message);
Assert.Equal("Invalid response object from API: \"this isn't JSON\"", exception.Message);
Assert.Equal(response, exception.StripeResponse);
}

Expand All @@ -128,7 +147,7 @@ await this.stripeClient.RequestAsync<Charge>(

Assert.NotNull(exception);
Assert.Equal(HttpStatusCode.InternalServerError, exception.HttpStatusCode);
Assert.Equal("Invalid response object from API: {}", exception.Message);
Assert.Equal("Invalid response object from API: \"{}\"", exception.Message);
Assert.Equal(response, exception.StripeResponse);
}

Expand Down

0 comments on commit 31d32e0

Please sign in to comment.