-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: make metrics and regsitration tests actually useful (#227)
- Loading branch information
Showing
6 changed files
with
75 additions
and
134 deletions.
There are no files selected for viewing
52 changes: 0 additions & 52 deletions
52
tests/Unleash.Tests/Communication/BaseUnleashApiClientTest.cs
This file was deleted.
Oops, something went wrong.
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,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); | ||
|
||
} | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
tests/Unleash.Tests/Communication/UnleashApiClient_FetchToggles_Tests.cs
This file was deleted.
Oops, something went wrong.
32 changes: 18 additions & 14 deletions
32
tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
34 changes: 19 additions & 15 deletions
34
tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.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 |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
} |
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