Support string ApiException.Content when actual

type is ValidationApiException.

fixes reactiveui/refit#636
This commit is contained in:
Drake Lambert 2020-07-31 08:48:41 -05:00
Родитель f346e9dd94
Коммит cb0267f4fe
2 изменённых файлов: 28 добавлений и 1 удалений

Просмотреть файл

@ -221,6 +221,31 @@ namespace Refit.Tests
Assert.NotNull(actualException.Content);
Assert.Equal("Hello world", actualException.Content);
}
[Fact]
public async Task ValidationApiException_HydratesBaseContent()
{
var expectedProblemDetails = new ProblemDetails
{
Detail = "detail",
Instance = "instance",
Status = 1,
Title = "title",
Type = "type"
};
var expectedContent = JsonConvert.SerializeObject(expectedProblemDetails);
var expectedResponse = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(expectedContent)
};
expectedResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/problem+json");
mockHandler.Expect(HttpMethod.Get, "http://api/aliasTest")
.Respond(req => expectedResponse);
var actualException = await Assert.ThrowsAsync<ValidationApiException>(() => fixture.GetTestObject());
var actualBaseExcpetion = actualException as ApiException;
Assert.Equal(expectedContent, actualBaseExcpetion.Content);
}
}
public sealed class ThrowOnGetLengthMemoryStream : MemoryStream

Просмотреть файл

@ -53,11 +53,13 @@ namespace Refit
try
{
exception.ContentHeaders = response.Content.Headers;
exception.Content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
exception.Content = content;
if (response.Content.Headers?.ContentType?.MediaType?.Equals("application/problem+json") ?? false)
{
exception = await ValidationApiException.Create(exception).ConfigureAwait(false);
exception.Content = content;
}
response.Content.Dispose();