Skip to content

Commit

Permalink
Use Error property when throwing exception (reactiveui#1448)
Browse files Browse the repository at this point in the history
* Use Error property when throwing exception reactiveui#1376

* Add Unit test

* Update test assertions
  • Loading branch information
marcominerva authored Apr 12, 2023
1 parent c97fcb6 commit 5589ab9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions Refit.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,44 @@ public async Task ThrowsValidationException()
Assert.Equal("title", actualException.Content.Title);
Assert.Equal("type", actualException.Content.Type);
}

/// <summary>
/// Test to verify if EnsureSuccessStatusCodeAsync throws a ValidationApiException for a Bad Request in terms of RFC 7807
/// </summary>
[Fact]
public async Task When_BadRequest_EnsureSuccessStatusCodeAsync_ThrowsValidationException()
{
var expectedContent = new ProblemDetails
{
Detail = "detail",
Errors = { { "Field1", new string[] { "Problem1" } }, { "Field2", new string[] { "Problem2" } } },
Instance = "instance",
Status = 1,
Title = "title",
Type = "type"
};

var expectedResponse = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(JsonConvert.SerializeObject(expectedContent))
};

expectedResponse.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/problem+json");
mockHandler.Expect(HttpMethod.Get, "http://api/GetApiResponseTestObject")
.Respond(req => expectedResponse);

using var response = await fixture.GetApiResponseTestObject();
var actualException = await Assert.ThrowsAsync<ValidationApiException>(() => response.EnsureSuccessStatusCodeAsync());

Assert.NotNull(actualException.Content);
Assert.Equal("detail", actualException.Content.Detail);
Assert.Equal("Problem1", actualException.Content.Errors["Field1"][0]);
Assert.Equal("Problem2", actualException.Content.Errors["Field2"][0]);
Assert.Equal("instance", actualException.Content.Instance);
Assert.Equal(1, actualException.Content.Status);
Assert.Equal("title", actualException.Content.Title);
Assert.Equal("type", actualException.Content.Type);
}

[Fact]
public async Task WhenProblemDetailsResponseContainsExtensions_ShouldHydrateExtensions()
Expand Down
2 changes: 1 addition & 1 deletion Refit/ApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<ApiResponse<T>> EnsureSuccessStatusCodeAsync()
{
if (!IsSuccessStatusCode)
{
var exception = await ApiException.Create(response.RequestMessage!, response.RequestMessage!.Method, response, Settings).ConfigureAwait(false);
var exception = Error ?? await ApiException.Create(response.RequestMessage!, response.RequestMessage!.Method, response, Settings).ConfigureAwait(false);

Dispose();

Expand Down

0 comments on commit 5589ab9

Please sign in to comment.