-
-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom exception factory for deserialization exceptions (#1862)
Co-authored-by: Teddy Assefa <[email protected]>
- Loading branch information
Showing
4 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using RichardSzalay.MockHttp; | ||
using Xunit; | ||
|
||
namespace Refit.Tests; | ||
|
||
public class DeserializationExceptionFactoryTests | ||
{ | ||
public interface IMyService | ||
{ | ||
[Get("/get-with-result")] | ||
Task<int> GetWithResult(); | ||
} | ||
|
||
[Fact] | ||
public async Task NoDeserializationExceptionFactory_WithSuccessfulDeserialization() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
}; | ||
|
||
var intContent = 123; | ||
handler | ||
.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.OK, new StringContent($"{intContent}")); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var result = await fixture.GetWithResult(); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
|
||
Assert.Equal(intContent, result); | ||
} | ||
|
||
[Fact] | ||
public async Task NoDeserializationExceptionFactory_WithUnsuccessfulDeserialization() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
}; | ||
|
||
handler | ||
.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.OK, new StringContent("non-int-result")); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var thrownException = await Assert.ThrowsAsync<ApiException>(() => fixture.GetWithResult()); | ||
Assert.Equal("An error occured deserializing the response.", thrownException.Message); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
} | ||
|
||
[Fact] | ||
public async Task ProvideFactoryWhichReturnsNull_WithSuccessfulDeserialization() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
DeserializationExceptionFactory = (_, _) => Task.FromResult<Exception>(null) | ||
}; | ||
|
||
var intContent = 123; | ||
handler | ||
.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.OK, new StringContent($"{intContent}")); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var result = await fixture.GetWithResult(); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
|
||
Assert.Equal(intContent, result); | ||
} | ||
|
||
[Fact] | ||
public async Task ProvideFactoryWhichReturnsNull_WithUnsuccessfulDeserialization() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
DeserializationExceptionFactory = (_, _) => Task.FromResult<Exception>(null) | ||
}; | ||
|
||
handler | ||
.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.OK, new StringContent("non-int-result")); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var result = await fixture.GetWithResult(); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
|
||
Assert.Equal(default, result); | ||
} | ||
|
||
[Fact] | ||
public async Task ProvideFactoryWhichReturnsException_WithUnsuccessfulDeserialization() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var exception = new Exception("Unsuccessful Deserialization Exception"); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
DeserializationExceptionFactory = (_, _) => Task.FromResult<Exception>(exception) | ||
}; | ||
|
||
handler | ||
.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.OK, new StringContent("non-int-result")); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var thrownException = await Assert.ThrowsAsync<Exception>(() => fixture.GetWithResult()); | ||
Assert.Equal(exception, thrownException); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
} | ||
|
||
[Fact] | ||
public async Task ProvideFactoryWhichReturnsException_WithSuccessfulDeserialization() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var exception = new Exception("Unsuccessful Deserialization Exception"); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
DeserializationExceptionFactory = (_, _) => Task.FromResult<Exception>(exception) | ||
}; | ||
|
||
var intContent = 123; | ||
handler | ||
.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.OK, new StringContent($"{intContent}")); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var result = await fixture.GetWithResult(); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
|
||
Assert.Equal(intContent, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters