Skip to content

Commit ca7b61a

Browse files
wosciatkoBurgyn
authored andcommitted
Added "Test" environment support (#50)
* Added exception handlig GatewayAuthorizationMiddleware * Little refactor + fixed and added tests for exceptions * Blank line removed * Added Test environment and extensions IHostedEnvironment * Comments changes and little refactor in tests
1 parent d60d3de commit ca7b61a

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Microsoft.AspNetCore.Hosting
2+
{
3+
/// <summary>
4+
/// Valid environments.
5+
/// </summary>
6+
public static class Environments
7+
{
8+
/// <summary>
9+
/// Development environment.
10+
/// </summary>
11+
public const string Development = "Development";
12+
13+
/// <summary>
14+
/// Test environment.
15+
/// </summary>
16+
public const string Test = "Test";
17+
18+
/// <summary>
19+
/// Staging environment.
20+
/// </summary>
21+
public const string Staging = "Staging";
22+
23+
/// <summary>
24+
/// Production environment.
25+
/// </summary>
26+
public const string Production = "Production";
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Microsoft.AspNetCore.Hosting
2+
{
3+
/// <summary>
4+
/// Extension for IHostingEnvironment.
5+
/// </summary>
6+
public static class IHostingEnvironmentExtensions
7+
{
8+
/// <summary>
9+
/// Check if current environment is <see cref="Environments.Test"/>.
10+
/// </summary>
11+
/// <param name="env">Current environment.</param>
12+
/// <returns><see langword="true" /> if current environment is <see cref="Environments.Test"/>,
13+
/// <see langword="false" /> otherwise.</returns>
14+
public static bool IsTest(this IHostingEnvironment env)
15+
=> env.IsEnvironment(Environments.Test);
16+
17+
/// <summary>
18+
/// Check if current environment is <see cref="Environments.Test"/> or <see cref="Environments.Development"/>.
19+
/// </summary>
20+
/// <param name="env">Current environment.</param>
21+
/// <returns><see langword="true" /> if current environment is <see cref="Environments.Test"/> or
22+
/// <see cref="Environments.Development"/>, <see langword="false" /> otherwise.</returns>
23+
public static bool IsTestOrDevelopment(this IHostingEnvironment env)
24+
=> env.IsTest() || env.IsDevelopment();
25+
26+
/// <summary>
27+
/// Check if current environment is <see cref="Environments.Staging"/> or <see cref="Environments.Production"/>.
28+
/// </summary>
29+
/// <param name="env">Current environment.</param>
30+
/// <returns><see langword="True" /> if current environment is <see cref="Environments.Staging"/> or
31+
/// <see cref="Environments.Production"/>, <see langword="false" /> otherwise.</returns>
32+
public static bool IsStagingOrProduction(this IHostingEnvironment env)
33+
=> env.IsStaging() || env.IsProduction();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using FluentAssertions;
2+
using Microsoft.AspNetCore.Hosting;
3+
using NSubstitute;
4+
using Xunit;
5+
6+
namespace Kros.AspNetCore.Tests.Environments
7+
{
8+
public class EnvironmentExtensionsShould
9+
{
10+
[Fact]
11+
public void IsTestReturnTrueIfTestEnvironment()
12+
{
13+
IHostingEnvironment env = Substitute.For<IHostingEnvironment>();
14+
env.EnvironmentName.Returns(Microsoft.AspNetCore.Hosting.Environments.Test);
15+
16+
env.IsTest().Should().BeTrue();
17+
}
18+
19+
[Fact]
20+
public void IsTestReturnFalseIfDevelopmentEnvironment()
21+
{
22+
IHostingEnvironment env = Substitute.For<IHostingEnvironment>();
23+
env.EnvironmentName.Returns(Microsoft.AspNetCore.Hosting.Environments.Development);
24+
env.IsTest().Should().BeFalse();
25+
}
26+
27+
[Fact]
28+
public void IsTestOrDevelopmentReturnTrueIfDevelopmentEnvironment()
29+
{
30+
IHostingEnvironment env = Substitute.For<IHostingEnvironment>();
31+
env.EnvironmentName.Returns(Microsoft.AspNetCore.Hosting.Environments.Development);
32+
env.IsTestOrDevelopment().Should().BeTrue();
33+
}
34+
35+
[Fact]
36+
public void IsTestOrDevelopmentReturnFalseIfStagingEnvironment()
37+
{
38+
IHostingEnvironment env = Substitute.For<IHostingEnvironment>();
39+
env.EnvironmentName.Returns(Microsoft.AspNetCore.Hosting.Environments.Staging);
40+
env.IsTestOrDevelopment().Should().BeFalse();
41+
}
42+
43+
[Fact]
44+
public void IsStagingOrProductionReturnTrueIfStagingEnvironment()
45+
{
46+
IHostingEnvironment env = Substitute.For<IHostingEnvironment>();
47+
env.EnvironmentName.Returns(Microsoft.AspNetCore.Hosting.Environments.Staging);
48+
env.IsStagingOrProduction().Should().BeTrue();
49+
}
50+
51+
[Fact]
52+
public void IsStagingOrProductionReturnFalseIfTestEnvironment()
53+
{
54+
IHostingEnvironment env = Substitute.For<IHostingEnvironment>();
55+
env.EnvironmentName.Returns(Microsoft.AspNetCore.Hosting.Environments.Test);
56+
env.IsStagingOrProduction().Should().BeFalse();
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)