-
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #117 from prom-client-net/test/add-units
test: add & improve unit tests
- Loading branch information
Showing
10 changed files
with
383 additions
and
55 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,15 @@ | ||
namespace Prometheus.Client.HttpRequestDurations; | ||
|
||
internal static class Defaults | ||
{ | ||
internal const string MetricName = "http_request_duration_seconds"; | ||
|
||
internal static class LabelNames | ||
{ | ||
internal const string StatusCode = "status_code"; | ||
internal const string Method = "method"; | ||
internal const string Controller = "controller"; | ||
internal const string Action = "action"; | ||
internal const string Path = "path"; | ||
} | ||
} |
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
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,70 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Prometheus.Client.Collectors; | ||
using Xunit; | ||
|
||
namespace Prometheus.Client.HttpRequestDurations.Tests; | ||
|
||
public class ApplicationBuilderExtensionsTests | ||
{ | ||
private readonly ServiceCollection _services = new(); | ||
|
||
[Fact] | ||
public void AppBuilderIsNull_Throws_ArgumentNullException() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => ((ApplicationBuilder)null).UsePrometheusRequestDurations()); | ||
} | ||
|
||
[Fact] | ||
public void TargetIsType_HttpRequestDurationsMiddleware() | ||
{ | ||
var app = new ApplicationBuilder(_services.BuildServiceProvider()); | ||
_services.AddSingleton<ICollectorRegistry, CollectorRegistry>(); | ||
app.UsePrometheusRequestDurations(); | ||
|
||
Assert.IsType<HttpRequestDurationsMiddleware>(app.Build().Target); | ||
} | ||
|
||
[Fact] | ||
public void With_DefaultCollectorRegistry() | ||
{ | ||
var app = new ApplicationBuilder(_services.BuildServiceProvider()); | ||
app.UsePrometheusRequestDurations(); | ||
app.Build(); | ||
|
||
Assert.True(Metrics.DefaultCollectorRegistry.TryGet(Defaults.MetricName, out var defaultCollector)); | ||
|
||
// Cleanup | ||
Metrics.DefaultCollectorRegistry?.Remove(defaultCollector); | ||
} | ||
|
||
[Fact] | ||
public void With_DICollecorRegistry() | ||
{ | ||
var registry = new CollectorRegistry(); | ||
_services.AddSingleton<ICollectorRegistry>(registry); | ||
var app = new ApplicationBuilder(_services.BuildServiceProvider()); | ||
app.UsePrometheusRequestDurations(); | ||
app.Build(); | ||
|
||
Assert.True(registry.TryGet(Defaults.MetricName, out _)); | ||
|
||
Assert.False(Metrics.DefaultCollectorRegistry.TryGet(Defaults.MetricName, out _)); | ||
} | ||
|
||
[Fact] | ||
public void With_CustomCollecorRegistry() | ||
{ | ||
var registry = new CollectorRegistry(); | ||
|
||
var app = new ApplicationBuilder(_services.BuildServiceProvider()); | ||
app.UsePrometheusRequestDurations(q => q.CollectorRegistry = registry); | ||
app.Build(); | ||
|
||
Assert.True(registry.TryGet(Defaults.MetricName, out _)); | ||
|
||
Assert.False(Metrics.DefaultCollectorRegistry.TryGet(Defaults.MetricName, out _)); | ||
} | ||
} |
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,26 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
using Xunit; | ||
|
||
namespace Prometheus.Client.HttpRequestDurations.Tests; | ||
|
||
public class HttpContextExtensionsTests | ||
{ | ||
[Fact] | ||
public void GetRouteName_When_HttpContextIsNull_Throws_ArgumentNullException() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => ((HttpContext)null).GetRouteName()); | ||
} | ||
|
||
[Fact] | ||
public void GetControllerName_When_HttpContextIsNull_Throws_ArgumentNullException() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => ((HttpContext)null).GetControllerName()); | ||
} | ||
|
||
[Fact] | ||
public void GetActionName_When_HttpContextIsNull_Throws_ArgumentNullException() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => ((HttpContext)null).GetActionName()); | ||
} | ||
} |
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,196 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Prometheus.Client.Collectors; | ||
using Xunit; | ||
|
||
namespace Prometheus.Client.HttpRequestDurations.Tests; | ||
|
||
public class HttpRequestDurationsMiddlewareTests | ||
{ | ||
private readonly ICollectorRegistry _registry; | ||
private readonly IApplicationBuilder _app; | ||
|
||
public HttpRequestDurationsMiddlewareTests() | ||
{ | ||
_registry = new CollectorRegistry(); | ||
|
||
var services = new ServiceCollection(); | ||
services.AddSingleton(_registry); | ||
_app = new ApplicationBuilder(services.BuildServiceProvider()); | ||
} | ||
|
||
[Fact] | ||
public void Use_DefaultCollectorNotNull() | ||
{ | ||
UseBuildApp(); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
|
||
Assert.NotNull(collector); | ||
} | ||
|
||
[Theory] | ||
[InlineData("custom_name")] | ||
[InlineData("http_seconds")] | ||
[InlineData("myapp_http_request_duration_seconds")] | ||
public void Use_WithCustomMetricName_CustomCollectorNotNull(string metricName) | ||
{ | ||
UseBuildApp(q => q.MetricName = metricName); | ||
|
||
_registry.TryGet(metricName, out var collector); | ||
|
||
Assert.NotNull(collector); | ||
} | ||
|
||
[Fact] | ||
public void Use_WithCustomMetricName_DefaultCollectorIsNull() | ||
{ | ||
UseBuildApp(q => q.MetricName = "custom_name"); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
|
||
Assert.Null(collector); | ||
} | ||
|
||
[Fact] | ||
public void Collector_IsHistogram() | ||
{ | ||
UseBuildApp(); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
Assert.IsAssignableFrom<IMetricFamily<IHistogram>>(collector); | ||
} | ||
|
||
[Fact] | ||
public void Metric_ContainsStatusCodeLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeStatusCode = true); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.Contains(Defaults.LabelNames.StatusCode, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_DoesNotContainStatusCodeLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeStatusCode = false); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.DoesNotContain(Defaults.LabelNames.StatusCode, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_ContainsMethodLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeMethod = true); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.Contains(Defaults.LabelNames.Method, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_DoesNotContainMethodLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeMethod = false); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.DoesNotContain(Defaults.LabelNames.Method, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_ContainsControllerLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeController = true); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.Contains(Defaults.LabelNames.Controller, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_DoesNotContainControllerLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeController = false); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.DoesNotContain(Defaults.LabelNames.Controller, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_ContainsActionLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeAction = true); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.Contains(Defaults.LabelNames.Action, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_DoesNotContainActionLabel() | ||
{ | ||
UseBuildApp(q => q.IncludeAction = false); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.DoesNotContain(Defaults.LabelNames.Action, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_ContainsPathLabel() | ||
{ | ||
UseBuildApp(q => q.IncludePath = true); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.Contains(Defaults.LabelNames.Path, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_DoesNotContainPathLabel() | ||
{ | ||
UseBuildApp(q => q.IncludePath = false); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.DoesNotContain(Defaults.LabelNames.Path, metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_ContainsCustomLabel() | ||
{ | ||
UseBuildApp(q => q.CustomLabels = new Dictionary<string, Func<string>> | ||
{ | ||
{ | ||
"custom_label", () => "custom_value" | ||
} | ||
}); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.Contains("custom_label", metric.LabelNames); | ||
Assert.DoesNotContain("custom_value", metric.LabelNames); | ||
} | ||
|
||
[Fact] | ||
public void Metric_DoesNotContainCustomLabel() | ||
{ | ||
UseBuildApp(); | ||
|
||
_registry.TryGet(Defaults.MetricName, out var collector); | ||
var metric = (IMetricFamily<IHistogram>)collector; | ||
Assert.DoesNotContain("custom_label", metric.LabelNames); | ||
} | ||
|
||
private void UseBuildApp(Action<HttpRequestDurationsOptions> setupOptions = null) | ||
{ | ||
_app.UsePrometheusRequestDurations(setupOptions).Build(); | ||
} | ||
} |
Oops, something went wrong.