Skip to content

Commit

Permalink
#1 | ADDED: Transmitter concept for hooking Sending Function.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanluu committed Nov 16, 2020
1 parent 0ddbd29 commit d640a3a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
4 changes: 2 additions & 2 deletions TitanShark.Thresher.Core.Tests/InMemoryRecorderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task InMemoryRecorder_Works_WithSingleCall()
// prepares
var persistence = new InMemoryRecordsPersistence();
var recorder = new Recorder(persistence);
var handler = new SerialInterceptionHttpClientHandler(recorder);
var handler = new SerialInterceptionHttpClientHandler(interceptors: recorder);
var sut = new HttpClient(handler);

// acts
Expand All @@ -48,7 +48,7 @@ public async Task InMemoryRecorder_Works_WithMultipleCalls()
// prepares
var persistence = new InMemoryRecordsPersistence();
var recorder = new Recorder(persistence);
var handler = new SerialInterceptionHttpClientHandler(recorder);
var handler = new SerialInterceptionHttpClientHandler(interceptors: recorder);
var sut = new HttpClient(handler);

const int numberOfCalls = 100;
Expand Down
47 changes: 47 additions & 0 deletions TitanShark.Thresher.Core.Tests/TransmitterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using FluentAssertions;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace TitanShark.Thresher.Core.Tests
{
public class TransmitterTests
{
private readonly ITestOutputHelper _output;

public TransmitterTests(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public async Task Custom_SendFunction_In_Transmitter_Is_Possible()
{
// prepares
const string responseContentText = "Hello World!";
var transmitter = new Transmitter
(
(callId, request, cancellationToken) =>
{
_output.WriteLine($"Request to '{request.RequestUri}' was sent out.");

return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.Created)
{
Content = new StringContent(responseContentText)
});
}
);
var handler = new SerialInterceptionHttpClientHandler(transmitter: transmitter);
var sut = new HttpClient(handler);

// acts
var response = await sut.GetAsync("https://testing.only").ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

// asserts
response.StatusCode.Should().Be(System.Net.HttpStatusCode.Created);
content.Should().Be(responseContentText);
}
}
}
23 changes: 21 additions & 2 deletions TitanShark.Thresher.Core/InterceptableHttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ public abstract class InterceptableHttpClientHandler : HttpClientHandler
{
internal IList<IInterceptor> Interceptors { get; } = new List<IInterceptor>();

internal Transmitter Transmitter { get; }

public bool HasAnyInterceptor => Interceptors != null && Interceptors.Count > 0;

protected InterceptableHttpClientHandler(params IInterceptor[] interceptors)
protected InterceptableHttpClientHandler(Transmitter transmitter = null, params IInterceptor[] interceptors)
{
Transmitter = transmitter ?? CreateDefaultTransmitter();

if (interceptors != null && interceptors.Length > 0)
{
foreach(var interceptor in interceptors)
Expand Down Expand Up @@ -45,9 +49,24 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}
}

protected virtual Transmitter CreateDefaultTransmitter()
{
return new Transmitter
(
(callId, request, cancellationToken) => base.SendAsync(request, cancellationToken)
);
}

protected virtual Task<HttpResponseMessage> OnSending(CallId callId, HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken);
var sendFunction = Transmitter?.SendFunction;

if (sendFunction != null)
{
return sendFunction(callId, request, cancellationToken);
}

return Task.FromResult(default(HttpResponseMessage));
}

protected abstract Task OnPreparing(CallId callId, HttpRequestMessage request, CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace TitanShark.Thresher.Core
{
public class SerialInterceptionHttpClientHandler : InterceptableHttpClientHandler
{
public SerialInterceptionHttpClientHandler(params IInterceptor[] interceptors) : base(interceptors)
public SerialInterceptionHttpClientHandler(Transmitter transmitter = null, params IInterceptor[] interceptors) : base(transmitter, interceptors)
{
}

Expand Down
22 changes: 22 additions & 0 deletions TitanShark.Thresher.Core/_Fundamentals/Transmitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace TitanShark.Thresher.Core
{
public class Transmitter
{
public Func<CallId, HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> SendFunction { get; }

public Transmitter(Func<CallId, HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> sendFunction)
{
if (sendFunction == null)
{
throw new ArgumentNullException(nameof(sendFunction));
}

SendFunction = sendFunction;
}
}
}

0 comments on commit d640a3a

Please sign in to comment.