Skip to content

Commit

Permalink
#1 | CHANGED: Unit-Tests for Replayer contain check whether the given…
Browse files Browse the repository at this point in the history
… Request-Headers are persisted.
  • Loading branch information
lanluu committed Dec 13, 2020
1 parent ff68398 commit 5dab93e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
58 changes: 53 additions & 5 deletions TitanShark.Thresher.Core.Tests/ReplayerTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -12,6 +14,10 @@ namespace TitanShark.Thresher.Core.Tests
{
public class ReplayerTests
{
private const string AcceptJson = "application/json";
private const string BasicAuthScheme = "Basic";
private const string BasicAuthValue = "cm9vdDpyb290";

private readonly ITestOutputHelper _output;

public ReplayerTests(ITestOutputHelper output)
Expand All @@ -26,6 +32,8 @@ public async Task Record_And_Sequential_Replay_With_Filter_On_StatusCodes()
var stats = new Dictionary<HttpStatusCode, int>
{
[HttpStatusCode.OK] = 0,
[HttpStatusCode.NotAcceptable] = 0,
[HttpStatusCode.Unauthorized] = 0,
[HttpStatusCode.BadRequest] = 0,
[HttpStatusCode.NotFound] = 0
};
Expand All @@ -36,7 +44,7 @@ public async Task Record_And_Sequential_Replay_With_Filter_On_StatusCodes()
{
_output.WriteLine($"Request to '{request.RequestUri}' was sent out.");

var response = Mock.BuildResponse(request);
var response = Mock.Build(request);

stats[response.StatusCode] += 1;

Expand All @@ -48,7 +56,10 @@ public async Task Record_And_Sequential_Replay_With_Filter_On_StatusCodes()
var handler = new InterceptableHttpClientHandler(
transmitter: transmitter,
interceptorsRunner: new SequentialInterceptorsRunner(recorder));

var client = new HttpClient(handler);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(AcceptJson));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(BasicAuthScheme, BasicAuthValue);

var started = DateTime.UtcNow;
await client.GetAsync("https://testing.only/Successful").ConfigureAwait(false);
Expand All @@ -59,19 +70,31 @@ public async Task Record_And_Sequential_Replay_With_Filter_On_StatusCodes()
await client.GetAsync("https://testing.only/Failed").ConfigureAwait(false);

stats[HttpStatusCode.OK].Should().Be(1);
stats[HttpStatusCode.NotAcceptable].Should().Be(0);
stats[HttpStatusCode.Unauthorized].Should().Be(0);
stats[HttpStatusCode.BadRequest].Should().Be(2);
stats[HttpStatusCode.NotFound].Should().Be(1);

stats[HttpStatusCode.OK] = 0;
stats[HttpStatusCode.NotAcceptable] = 0;
stats[HttpStatusCode.Unauthorized] = 0;
stats[HttpStatusCode.BadRequest] = 0;
stats[HttpStatusCode.NotFound] = 0;

client.Dispose();

// acts
// ... gets only logic-failed requests
var snapshot = await persistence.Snapshot(
CancellationToken.None,
started, ended,
new[] { HttpStatusCode.BadRequest, HttpStatusCode.NotFound });

// ... re-creates a fresh instance of HttpClient, without recorder.
handler = new InterceptableHttpClientHandler(transmitter: transmitter);
client = new HttpClient(handler);

// ... replays
var replayer = new Replayer(new SequentialReplayingStrategy(), client, snapshot);
replayer.Start();

Expand All @@ -85,27 +108,52 @@ public async Task Record_And_Sequential_Replay_With_Filter_On_StatusCodes()
stats[HttpStatusCode.OK].Should().Be(0);
stats[HttpStatusCode.BadRequest].Should().Be(1);
stats[HttpStatusCode.NotFound].Should().Be(1);

client.Dispose();
}

private class Mock
{
public const string Ok = "Okay!";
public const string Unauthorized = "No auth!";
public const string NotAcceptable = "No auth!";
public const string BadRequest = "Bad request!";
public const string NotFound = "Not found!";

public static HttpResponseMessage BuildResponse(HttpRequestMessage request) => request.RequestUri.PathAndQuery switch
public static HttpResponseMessage Build(HttpRequestMessage request)
{
if (!request.Headers.Accept.Any(accept => string.Equals(accept.MediaType, AcceptJson, StringComparison.InvariantCultureIgnoreCase)))
{
return new HttpResponseMessage(HttpStatusCode.NotAcceptable)
{
Content = new StringContent(NotAcceptable)
};
}

if (!string.Equals(request.Headers.Authorization.ToString(), $"{BasicAuthScheme} {BasicAuthValue}", StringComparison.InvariantCultureIgnoreCase))
{
return new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent(Unauthorized)
};
}

return BuildInternal(request);
}

private static HttpResponseMessage BuildInternal(HttpRequestMessage request) => request.RequestUri.PathAndQuery switch
{
"/Successful" => new HttpResponseMessage(System.Net.HttpStatusCode.OK)
"/Successful" => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(Ok)
},

"/Failed" => new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)
"/Failed" => new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(BadRequest)
},

_ => new HttpResponseMessage(System.Net.HttpStatusCode.NotFound)
_ => new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(NotFound)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
Expand Down
6 changes: 6 additions & 0 deletions TitanShark.Thresher.Core.Tests/TransmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public async Task Custom_SendFunction_Is_Possible()
// asserts
response.StatusCode.Should().Be(System.Net.HttpStatusCode.Created);
content.Should().Be(responseContentText);

// cleans up
sut.Dispose();
}

[Fact]
Expand Down Expand Up @@ -77,6 +80,9 @@ public async Task Mock_Is_Possible()

incorrectResponse.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound);
incorrectContent.Should().Be(Mock.NotFound);

// cleans up
sut.Dispose();
}

private class Mock
Expand Down
2 changes: 1 addition & 1 deletion TitanShark.Thresher.Core/TitanShark.Thresher.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Net.Http" Version="4.3.4" Condition="'$(TargetFramework)' == 'net462'" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="System.Text.Json" Version="5.0.0" />
</ItemGroup>

</Project>

0 comments on commit 5dab93e

Please sign in to comment.