-
Notifications
You must be signed in to change notification settings - Fork 2
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 #21 from myarichuk/develop
minor convenience methods in client builder, some more testing
- Loading branch information
Showing
10 changed files
with
200 additions
and
119 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,72 @@ | ||
namespace Simple.HttpClientFactory.Tests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Defines the <see cref="EventMessageHandler" />. | ||
/// </summary> | ||
public class EventMessageHandler : DelegatingHandler | ||
{ | ||
/// <summary> | ||
/// Defines the Request. | ||
/// </summary> | ||
public event EventHandler<RequestEventArgs> Request; | ||
|
||
/// <summary> | ||
/// Defines the Response. | ||
/// </summary> | ||
public event EventHandler<ResponseEventArgs> Response; | ||
|
||
/// <summary> | ||
/// Defines the _visitedMiddleware. | ||
/// </summary> | ||
private readonly List<string> _visitedMiddleware; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EventMessageHandler"/> class. | ||
/// </summary> | ||
/// <param name="visitedMiddleware">The visitedMiddleware<see cref="List{string}"/>.</param> | ||
public EventMessageHandler(List<string> visitedMiddleware) => _visitedMiddleware = visitedMiddleware; | ||
|
||
/// <summary> | ||
/// Defines the <see cref="RequestEventArgs" />. | ||
/// </summary> | ||
public class RequestEventArgs : EventArgs | ||
{ | ||
/// <summary> | ||
/// Gets or sets the Request. | ||
/// </summary> | ||
public HttpRequestMessage Request { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Defines the <see cref="ResponseEventArgs" />. | ||
/// </summary> | ||
public class ResponseEventArgs : EventArgs | ||
{ | ||
/// <summary> | ||
/// Gets or sets the Response. | ||
/// </summary> | ||
public HttpResponseMessage Response { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// The SendAsync. | ||
/// </summary> | ||
/// <param name="request">The request<see cref="HttpRequestMessage"/>.</param> | ||
/// <param name="cancellationToken">The cancellationToken<see cref="CancellationToken"/>.</param> | ||
/// <returns>The <see cref="Task{HttpResponseMessage}"/>.</returns> | ||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
Request?.Invoke(this, new RequestEventArgs { Request = request }); | ||
var response = await base.SendAsync(request, cancellationToken); | ||
Response?.Invoke(this, new ResponseEventArgs { Response = response }); | ||
_visitedMiddleware.Add(nameof(EventMessageHandler)); | ||
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
32 changes: 32 additions & 0 deletions
32
Simple.HttpClientFactory.Tests/PollyHttpMessageHandlerTests.cs
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,32 @@ | ||
using Polly; | ||
using Simple.HttpClientFactory.Polly; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Simple.HttpClientFactory.Tests | ||
{ | ||
//some sanity checks | ||
public class PollyHttpMessageHandlerTests | ||
{ | ||
[Fact] | ||
public void Ctor_with_null_should_throw() => | ||
Assert.Throws<ArgumentNullException>(() => new PolicyHttpMessageHandler(null)); | ||
|
||
[Fact] | ||
public async Task Null_param_in_send_async_should_throw() | ||
{ | ||
var middlewareHandler = new PolicyHttpMessageHandler(Policy<HttpResponseMessage> | ||
.Handle<HttpRequestException>() | ||
.OrResult(result => (int)result.StatusCode >= 500 || result.StatusCode == HttpStatusCode.RequestTimeout) | ||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))); | ||
|
||
using(var client = HttpClientFactory.Create(middlewareHandler).Build()) | ||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.SendAsync(null)); | ||
} | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
Simple.HttpClientFactory.Tests/TrafficRecorderMessageHandler.cs
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,45 @@ | ||
namespace Simple.HttpClientFactory.Tests | ||
{ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Defines the <see cref="TrafficRecorderMessageHandler" />. | ||
/// </summary> | ||
public class TrafficRecorderMessageHandler : DelegatingHandler | ||
{ | ||
/// <summary> | ||
/// Gets the Traffic. | ||
/// </summary> | ||
public List<(HttpRequestMessage, HttpResponseMessage)> Traffic { get; } = new List<(HttpRequestMessage, HttpResponseMessage)>(); | ||
|
||
/// <summary> | ||
/// Defines the _visitedMiddleware. | ||
/// </summary> | ||
private readonly List<string> _visitedMiddleware; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TrafficRecorderMessageHandler"/> class. | ||
/// </summary> | ||
/// <param name="visitedMiddleware">The visitedMiddleware<see cref="List{string}"/>.</param> | ||
public TrafficRecorderMessageHandler(List<string> visitedMiddleware) => _visitedMiddleware = visitedMiddleware; | ||
|
||
/// <summary> | ||
/// The SendAsync. | ||
/// </summary> | ||
/// <param name="request">The request<see cref="HttpRequestMessage"/>.</param> | ||
/// <param name="cancellationToken">The cancellationToken<see cref="CancellationToken"/>.</param> | ||
/// <returns>The <see cref="Task{HttpResponseMessage}"/>.</returns> | ||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
request.Headers.Add("foobar", "foobar"); | ||
var response = await base.SendAsync(request, cancellationToken); | ||
_visitedMiddleware.Add(nameof(TrafficRecorderMessageHandler)); | ||
Traffic.Add((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
Oops, something went wrong.