Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Error property when throwing exception #1448

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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