-
Notifications
You must be signed in to change notification settings - Fork 23
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 #51 from keenlabs/jm_HttpClient_Refactor
Refactor HttpClient usage
- Loading branch information
Showing
54 changed files
with
2,000 additions
and
464 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
|
||
namespace Keen.Net.Test | ||
{ | ||
/// <summary> | ||
/// Wraps an <see cref="IHttpMessageHandler"/> and allows for using it as a DelegatingHandler. | ||
/// If the IHttpMessageHandler doesn't already have a default action set, we'll have it call | ||
/// our own base SendAsync() which will forward the request down the handler chain. | ||
/// </summary> | ||
internal class DelegatingHandlerMock : DelegatingHandler | ||
{ | ||
private readonly IHttpMessageHandler _handler; | ||
|
||
internal DelegatingHandlerMock(IHttpMessageHandler handler) | ||
{ | ||
_handler = handler; | ||
_handler.DefaultAsync = (_handler.DefaultAsync ?? base.SendAsync); | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
return _handler.SendAsync(request, cancellationToken); | ||
} | ||
} | ||
} |
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,64 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
|
||
namespace Keen.Net.Test | ||
{ | ||
/// <summary> | ||
/// An <see cref="IHttpMessageHandler"/> that has pre/post/default message handlers functors, | ||
/// as well as a Func<> that produces the actual HttpResponseMessage. These can all be set by | ||
/// test code and will be called if available. There are defaults in place that essentially do | ||
/// nothing, but client code should make sure DefaultAsync gets set, either by a wrapper or | ||
/// explicitly. | ||
/// </summary> | ||
internal class FuncHandler : IHttpMessageHandler | ||
{ | ||
internal Action<HttpRequestMessage, CancellationToken> PreProcess = (request, ct) => { }; | ||
|
||
internal Func<HttpRequestMessage, | ||
CancellationToken, | ||
Task<HttpResponseMessage>> ProduceResultAsync = | ||
(request, ct) => Task.FromResult<HttpResponseMessage>(null); | ||
|
||
internal Func<HttpRequestMessage, | ||
HttpResponseMessage, | ||
HttpResponseMessage> PostProcess = (request, response) => response; | ||
|
||
internal bool DeferToDefault { get; set; } = true; | ||
|
||
public Func<HttpRequestMessage, | ||
CancellationToken, | ||
Task<HttpResponseMessage>> DefaultAsync { get; set; } | ||
|
||
|
||
public async Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
PreProcess(request, cancellationToken); | ||
HttpResponseMessage response = | ||
await ProduceResultAsync(request, cancellationToken).ConfigureAwait(false); | ||
|
||
// Pass it along down the line if we didn't create a result already. | ||
if (null == response) | ||
{ | ||
if (DeferToDefault) | ||
{ | ||
response = await DefaultAsync(request, cancellationToken).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
// Create a dummy successful response so HttpClient doesn't just throw always. | ||
response = await HttpTests.CreateJsonStringResponseAsync(new { dummy = "" }) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
|
||
PostProcess(request, response); | ||
|
||
return response; | ||
} | ||
} | ||
} |
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,31 @@ | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
|
||
namespace Keen.Net.Test | ||
{ | ||
/// <summary> | ||
/// Wraps an <see cref="IHttpMessageHandler"/> and allows for using it as an HttpClientHandler. | ||
/// If the IHttpMessageHandler doesn't already have a default action set, we'll have it call | ||
/// our own base SendAsync() which will forward the request to the actual HttpClientHandler | ||
/// implementation, with all the configuration and proxies and such, which may actually go out | ||
/// over the network. | ||
/// </summary> | ||
internal class HttpClientHandlerMock : HttpClientHandler | ||
{ | ||
internal readonly IHttpMessageHandler _handler; | ||
|
||
internal HttpClientHandlerMock(IHttpMessageHandler handler) | ||
{ | ||
_handler = handler; | ||
_handler.DefaultAsync = (_handler.DefaultAsync ?? base.SendAsync); | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
return _handler.SendAsync(request, cancellationToken); | ||
} | ||
} | ||
} |
Oops, something went wrong.