-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSentryAspNetCoreOptionsSetupTests.cs
More file actions
147 lines (122 loc) · 6.52 KB
/
SentryAspNetCoreOptionsSetupTests.cs
File metadata and controls
147 lines (122 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Sentry.Internal;
#if NETCOREAPP3_1_OR_GREATER
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IWebHostEnvironment;
#else
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
namespace Sentry.AspNetCore.Tests;
public class SentryAspNetCoreOptionsSetupTests
{
private class Fixture
{
public Dictionary<string, string> Configuration { get; set; } = new();
public SentryAspNetCoreOptionsSetup GetSut()
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(Configuration)
.Build();
var loggingConfig = Substitute.For<ILoggerProviderConfiguration<SentryAspNetCoreLoggerProvider>>();
loggingConfig.Configuration.Returns(config);
return new(loggingConfig);
}
}
private readonly Fixture _fixture = new();
private readonly SentryAspNetCoreOptions _target = new();
[Fact]
public void Configure_RegistersTraceIgnoreStatusCodeTransactionProcessor()
{
var sut = _fixture.GetSut();
sut.Configure(_target);
Assert.Contains(_target.GetAllTransactionProcessors(), p => p is TraceIgnoreStatusCodeTransactionProcessor);
}
[Fact]
public void Filters_KestrelApplicationEvent_NoException_Filtered()
{
// Arrange
var sut = _fixture.GetSut();
// Act
sut.Configure(_target);
//Assert
Assert.Contains(_target.Filters, f => f.Filter("Microsoft.AspNetCore.Server.Kestrel", LogLevel.Critical, 13, null));
}
[Fact]
public void Filters_KestrelApplicationEvent_WithException_Filtered()
{
// Arrange
var sut = _fixture.GetSut();
// Act
sut.Configure(_target);
// Assert
Assert.Contains(_target.Filters, f => f.Filter("Microsoft.AspNetCore.Server.Kestrel", LogLevel.Critical, 13, new Exception()));
}
[Fact]
public void Filters_KestrelEventId1_WithException_NotFiltered()
{
// Arrange
var sut = _fixture.GetSut();
// Act
sut.Configure(_target);
// Assert
Assert.DoesNotContain(_target.Filters, f => f.Filter("Microsoft.AspNetCore.Server.Kestrel", LogLevel.Trace, 1, null));
}
[Theory]
[InlineData("foo", "Production", "foo")] // Custom - set. Rest ignored.
[InlineData("Production", "Production", "Production")] // Custom - set. Rest ignored. NOTE: Custom value _happens_ to be the common ASPNET_ENVIRONMENT PROD setting.
[InlineData(null, "Production", "production")] // Custom - nothing set. ASPNET_ENVIRONMENT is default PROD.
[InlineData("", "Production", "production")] // Custom - nothing set. ASPNET_ENVIRONMENT is default PROD.
[InlineData(null, "Development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV.
[InlineData("", "Development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV.
[InlineData(null, "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default STAGING.
[InlineData("", "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default STAGING.
[InlineData(null, "production", "production")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 'p').
[InlineData(null, "development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 'd').
[InlineData(null, "staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 's').
public void Filters_Environment_CustomOrASPNETEnvironment_Set(string environment, string hostingEnvironmentSetting, string expectedEnvironment)
{
// Arrange.
var hostingEnvironment = Substitute.For<IHostingEnvironment>();
hostingEnvironment.EnvironmentName = hostingEnvironmentSetting;
_target.Environment = environment;
// Act.
_target.SetEnvironment(hostingEnvironment);
// Assert.
Assert.Equal(expectedEnvironment, _target.Environment);
}
[Theory]
[InlineData("Foo", "Production", true, "Foo")] // Custom - set. Don't modify.
[InlineData(null, "Foo", true, "Foo")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom. Don't modify.
[InlineData(null, "Production", true, "production")] // Custom - nothing set. Adjust standard casing - true. ASPNET_ENVIRONMENT is default PROD.
[InlineData(null, "Development", true, "development")] // Custom - nothing set. Adjust standard casing - true. ASPNET_ENVIRONMENT is default DEV.
[InlineData(null, "Staging", true, "staging")] // Custom - nothing set. Adjust standard casing - true. ASPNET_ENVIRONMENT is default STAGING.
[InlineData(null, "Production", false, "Production")] // Custom - nothing set. Adjust standard casing - false. ASPNET_ENVIRONMENT is default PROD.
[InlineData(null, "Development", false, "Development")] // Custom - nothing set. Adjust standard casing - false. ASPNET_ENVIRONMENT is default DEV.
[InlineData(null, "Staging", false, "Staging")] // Custom - nothing set. Adjust standard casing - false. ASPNET_ENVIRONMENT is default STAGING.
public void Filters_Environment_AdjustStandardEnvironmentNameCasing_AffectsSentryEnvironment(string environment, string hostingEnvironmentSetting, bool adjustStandardEnvironmentNameCasingSetting, string expectedEnvironment)
{
// Arrange.
var hostingEnvironment = Substitute.For<IHostingEnvironment>();
hostingEnvironment.EnvironmentName = hostingEnvironmentSetting;
_target.Environment = environment;
_target.AdjustStandardEnvironmentNameCasing = adjustStandardEnvironmentNameCasingSetting;
// Act.
_target.SetEnvironment(hostingEnvironment);
// Assert.
Assert.Equal(expectedEnvironment, _target.Environment);
}
[Theory]
[InlineData("foo")] // Random setting.
[InlineData("Production")] // Custom setting which is the same as ASPNET_ENVIRONMENT. But because this is manually set, don't change it.
public void Filters_Environment_SentryEnvironment_Set(string environment)
{
// Arrange.
var hostingEnvironment = Substitute.For<IHostingEnvironment>();
_target.FakeSettings().EnvironmentVariables[Internal.Constants.EnvironmentEnvironmentVariable] = environment;
// Act.
_target.SetEnvironment(hostingEnvironment);
// Assert.
Assert.Equal(environment, _target.Environment);
}
}