-
-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #900 from Dreamescaper/customize_exceptions
Add ability to provide custom ExceptionFactory
- Loading branch information
Showing
8 changed files
with
331 additions
and
15 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,111 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
using Refit; // for the code gen | ||
|
||
using RichardSzalay.MockHttp; | ||
|
||
using Xunit; | ||
|
||
namespace Refit.Tests | ||
{ | ||
public class ExceptionFactoryTests | ||
{ | ||
public interface IMyService | ||
{ | ||
[Get("/get-with-result")] | ||
Task<string> GetWithResult(); | ||
|
||
[Put("/put-without-result")] | ||
Task PutWithoutResult(); | ||
} | ||
|
||
[Fact] | ||
public async Task ProvideFactoryWhichAlwaysReturnsNull_WithResult() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
ExceptionFactory = _ => Task.FromResult<Exception>(null) | ||
}; | ||
|
||
handler.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.NotFound, new StringContent("error-result")); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var result = await fixture.GetWithResult(); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
|
||
Assert.Equal("error-result", result); | ||
} | ||
|
||
[Fact] | ||
public async Task ProvideFactoryWhichAlwaysReturnsNull_WithoutResult() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
ExceptionFactory = _ => Task.FromResult<Exception>(null) | ||
}; | ||
|
||
handler.Expect(HttpMethod.Put, "http://api/put-without-result") | ||
.Respond(HttpStatusCode.NotFound); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
await fixture.PutWithoutResult(); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
} | ||
|
||
[Fact] | ||
public async Task ProvideFactoryWhichAlwaysReturnsException_WithResult() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var exception = new Exception("I like to fail"); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
ExceptionFactory = _ => Task.FromResult<Exception>(exception) | ||
}; | ||
|
||
handler.Expect(HttpMethod.Get, "http://api/get-with-result") | ||
.Respond(HttpStatusCode.OK, new StringContent("success-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 ProvideFactoryWhichAlwaysReturnsException_WithoutResult() | ||
{ | ||
var handler = new MockHttpMessageHandler(); | ||
var exception = new Exception("I like to fail"); | ||
var settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => handler, | ||
ExceptionFactory = _ => Task.FromResult<Exception>(exception) | ||
}; | ||
|
||
handler.Expect(HttpMethod.Put, "http://api/put-without-result") | ||
.Respond(HttpStatusCode.OK); | ||
|
||
var fixture = RestService.For<IMyService>("http://api", settings); | ||
|
||
var thrownException = await Assert.ThrowsAsync<Exception>(() => fixture.PutWithoutResult()); | ||
Assert.Equal(exception, thrownException); | ||
|
||
handler.VerifyNoOutstandingExpectation(); | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.