From ccd5b6bd044ce9cf959e61df94760b4155991d94 Mon Sep 17 00:00:00 2001 From: Hans De Mulder Date: Sun, 7 Apr 2019 18:59:43 +0200 Subject: [PATCH 1/2] add test to simulate issue --- Refit.Tests/ResponseTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Refit.Tests/ResponseTests.cs b/Refit.Tests/ResponseTests.cs index 89694707d..b1c20308d 100644 --- a/Refit.Tests/ResponseTests.cs +++ b/Refit.Tests/ResponseTests.cs @@ -99,7 +99,24 @@ public async Task ThrowsValidationException() Assert.Equal(1, actualException.Content.Status); Assert.Equal("title", actualException.Content.Title); Assert.Equal("type", actualException.Content.Type); + } + + [Fact] + public async Task BadRequestWithEmptyContent_ShouldReturnApiException() + { + var expectedResponse = new HttpResponseMessage(HttpStatusCode.BadRequest) + { + Content = new StringContent("Hello world") + }; + expectedResponse.Content.Headers.Clear(); + mockHandler.Expect(HttpMethod.Get, "http://api/aliasTest") + .Respond(req => expectedResponse); + + var actualException = await Assert.ThrowsAsync(() => fixture.GetTestObject()); + + Assert.NotNull(actualException.Content); + Assert.Equal("Hello world", actualException.Content); } } } From ac4188e6627d45f2adfac7e9dda71b67ea0d44d1 Mon Sep 17 00:00:00 2001 From: Hans De Mulder Date: Sun, 7 Apr 2019 19:03:04 +0200 Subject: [PATCH 2/2] check for null values in handling (exception) content --- Refit/ApiException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Refit/ApiException.cs b/Refit/ApiException.cs index c5d67f44c..2b76ce6e4 100644 --- a/Refit/ApiException.cs +++ b/Refit/ApiException.cs @@ -57,7 +57,7 @@ public static async Task Create(HttpRequestMessage message, HttpMe exception.ContentHeaders = response.Content.Headers; exception.Content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - if (response.Content.Headers.ContentType.MediaType.Equals("application/problem+json")) + if (response.Content.Headers?.ContentType?.MediaType?.Equals("application/problem+json") ?? false) { exception = await ValidationApiException.Create(exception).ConfigureAwait(false); }