Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rearrange EventHandler execution for Async methods #857

Open
wants to merge 1 commit into
base: v3-v2021-02-25
Choose a base branch
from
Open
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
35 changes: 27 additions & 8 deletions Recurly.Tests/BaseClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,31 @@ public void WillTriggerHookIfAvailable()
{
var client = MockClient.Build(SuccessResponse(System.Net.HttpStatusCode.OK));
var mockHandler = new Mock<IEventHandler>();
mockHandler
.Setup(x => x.OnRequest(It.IsAny<Recurly.Http.Request>()));
mockHandler
.Setup(x => x.OnResponse(It.IsAny<Recurly.Http.Response>()));
client.AddEventHandler(mockHandler.Object);
MyResource resource = client.GetResource("benjamin", "param1", new DateTime());

Assert.Equal("benjamin", resource.MyString);
mockHandler.Verify(g => g.OnRequest(It.IsAny<Recurly.Http.Request>()), Times.Once());
mockHandler.Verify(g => g.OnResponse(It.IsAny<Recurly.Http.Response>()), Times.Once());
}

[Fact]
public void WillTriggerHookIfAvailableOnError()
{
var client = MockClient.Build(NotFoundResponse());
var mockHandler = new Mock<IEventHandler>();
client.AddEventHandler(mockHandler.Object);

Assert.Throws<Recurly.Errors.NotFound>(() => client.GetResource("benjamin", "param1", new DateTime()));
mockHandler.Verify(g => g.OnRequest(It.IsAny<Recurly.Http.Request>()), Times.Once());
mockHandler.Verify(g => g.OnResponse(It.IsAny<Recurly.Http.Response>()), Times.Once());
}

[Fact]
public async void WillTriggerHookIfAvailableAsync()
{
var client = MockClient.Build(SuccessResponse(System.Net.HttpStatusCode.OK));
var mockHandler = new Mock<IEventHandler>();
mockHandler
.Setup(x => x.OnRequest(It.IsAny<Recurly.Http.Request>()));
mockHandler
.Setup(x => x.OnResponse(It.IsAny<Recurly.Http.Response>()));
client.AddEventHandler(mockHandler.Object);
MyResource resource = await client.GetResourceAsync("benjamin", "param1", new DateTime());

Expand All @@ -200,6 +207,18 @@ public async void WillTriggerHookIfAvailableAsync()
mockHandler.Verify(v => v.OnResponse(It.IsAny<Recurly.Http.Response>()), Times.Once());
}

[Fact]
public async void WillTriggerHookIfAvailableOnErrorAsync()
{
var client = MockClient.Build(NotFoundResponse());
var mockHandler = new Mock<IEventHandler>();
client.AddEventHandler(mockHandler.Object);

await Assert.ThrowsAsync<Recurly.Errors.NotFound>(() => client.GetResourceAsync("benjamin", "param1", new DateTime()));
mockHandler.Verify(v => v.OnRequest(It.IsAny<Recurly.Http.Request>()), Times.Once());
mockHandler.Verify(v => v.OnResponse(It.IsAny<Recurly.Http.Response>()), Times.Once());
}

private Mock<IRestResponse<MyResource>> SuccessResponse(System.Net.HttpStatusCode status)
{
var data = new MyResource()
Expand Down
3 changes: 2 additions & 1 deletion Recurly/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ public int Timeout
return await task.ContinueWith(t =>
{
var restResponse = t.Result;
this.HandleResponse(restResponse);
var httpResponse = Http.Response.Build(restResponse, httpRequest);

foreach (var handler in this.EventHandlers)
{
handler.OnResponse(httpResponse);
}

this.HandleResponse(restResponse);

if (restResponse.Data is Resource)
restResponse.Data.SetResponse(httpResponse);
return restResponse.Data;
Expand Down