Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
17 changes: 17 additions & 0 deletions Refit.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiException>(() => fixture.GetTestObject());

Assert.NotNull(actualException.Content);
Assert.Equal("Hello world", actualException.Content);
}
}
}
2 changes: 1 addition & 1 deletion Refit/ApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static async Task<ApiException> 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);
}
Expand Down