Skip to content

Commit

Permalink
chore: make metrics and regsitration tests actually useful (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Jul 8, 2024
1 parent dd3572b commit 246fb54
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 134 deletions.
52 changes: 0 additions & 52 deletions tests/Unleash.Tests/Communication/BaseUnleashApiClientTest.cs

This file was deleted.

37 changes: 37 additions & 0 deletions tests/Unleash.Tests/Communication/MockHttpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Unleash.Communication;
using Unleash.Internal;
using Unleash.Serialization;
using RichardSzalay.MockHttp;

namespace Unleash.Tests.Communication
{
internal static class MockHttpClient
{
internal static Tuple<MockHttpMessageHandler, UnleashApiClient> MakeMockClient(string url)
{
DynamicNewtonsoftJsonSerializer jsonSerializer = new DynamicNewtonsoftJsonSerializer();
jsonSerializer.TryLoad();

var mockHttp = new MockHttpMessageHandler();

var httpClient = new HttpClient(mockHttp)
{
BaseAddress = new Uri(url)
};

var requestHeaders = new UnleashApiClientRequestHeaders
{
AppName = "api-test-client",
CustomHttpHeaders = new Dictionary<string, string>()
{
{ "Authorization", "*:default.some-mock-hash" }
},
CustomHttpHeaderProvider = null
};

var unleashClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders, new EventCallbackConfig());
return new Tuple<MockHttpMessageHandler, UnleashApiClient>(mockHttp, unleashClient);

}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using Unleash.Metrics;

namespace Unleash.Tests.Communication
{
public class UnleashApiClient_RegisterClient_Tests : BaseUnleashApiClientTest
public class UnleashApiClient_RegisterClient_Tests
{
private const string BASE_URL = "http://some-mock-url/api/client";

[Test]
[Ignore("Requires a valid accesstoken")]
public async Task RegisterClient_Success()
{
var (mockHttp, client) = MockHttpClient.MakeMockClient(BASE_URL);

mockHttp.When($"{BASE_URL}/register")
.WithPartialContent("\"appName\":\"SomeTestAppName\"")
.WithPartialContent("\"interval\":1000")
.WithPartialContent("\"sdkVersion\":\"1.0.1\"")
.WithPartialContent("\"strategies\":[\"abc\"]")
.Respond("application/json", "{ 'status': 'ok' }");

var clientRegistration = new ClientRegistration()
{
AppName = GetType().Name,
InstanceId = "instance1",
AppName = "SomeTestAppName",
Interval = 1000,
SdkVersion = "sdk101",
Started = DateTimeOffset.UtcNow,
SdkVersion = "1.0.1",
Strategies = new List<string>
{
"abc"
}
};

var result = await api.RegisterClient(clientRegistration, CancellationToken.None);
result.Should().Be(true);
var result = await client.RegisterClient(clientRegistration, CancellationToken.None);
Assert.IsTrue(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework;
using Unleash.Metrics;
using RichardSzalay.MockHttp;
using NUnit.Framework.Internal;

namespace Unleash.Tests.Communication
{
public class UnleashApiClient_SendMetrics_Tests : BaseUnleashApiClientTest
public class UnleashApiClient_SendMetrics_Tests
{
private const string BASE_URL = "http://some-mock-url/api/client";

[Test]
[Ignore("Requires a valid accesstoken")]
public async Task SendMetrics_Success()
{
var (mockHttp, client) = MockHttpClient.MakeMockClient(BASE_URL);

mockHttp.When($"{BASE_URL}/metrics")
.WithPartialContent("appName")
.WithPartialContent("instanceId")
.WithPartialContent("\"no\":0")
.WithPartialContent("\"yes\":1")
.Respond("application/json", "{ 'status': 'ok' }");

var metricsBucket = new ThreadSafeMetricsBucket();
metricsBucket.RegisterCount("Demo123", true);
metricsBucket.RegisterCount("Demo123", false);

var result = await api.SendMetrics(metricsBucket, CancellationToken.None);
result.Should().Be(true);

// Check result:
// http://unleash.herokuapp.com/#/features/view/Demo123
// http://unleash.herokuapp.com/api/admin/metrics/feature-toggles
metricsBucket.RegisterCount("someTestToggle", true);

var result = await client.SendMetrics(metricsBucket, CancellationToken.None);
Assert.IsTrue(result);
}

}
}
1 change: 1 addition & 0 deletions tests/Unleash.Tests/Unleash.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="NLog" Version="5.2.0" />
<PackageReference Include="NLog.Config" Version="4.7.15" />
<PackageReference Include="NLog.Schema" Version="5.2.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 246fb54

Please sign in to comment.