Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Add extension method for getting environment name
Browse files Browse the repository at this point in the history
Fixes: #100
  • Loading branch information
Praburaj committed Mar 16, 2015
1 parent a10acfd commit 3b0d5fd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Hosting
{
public static class HostingEnvironmentExtensions
{
/// <summary>
/// Compares the current hosting environment name against the specified value.
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/> service.</param>
/// <param name="environmentName">Environment name to validate against.</param>
/// <returns>True if the specified name is same as the current environment.</returns>
public static bool IsEnvironment(
[NotNull]this IHostingEnvironment hostingEnvironment,
[NotNull]string environmentName)
{
return string.Equals(
hostingEnvironment.EnvironmentName,
environmentName,
StringComparison.OrdinalIgnoreCase);
}
}
}
5 changes: 3 additions & 2 deletions src/Microsoft.AspNet.Hosting.Interfaces/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"Microsoft.AspNet.Http": "1.0.0-*",
"Microsoft.AspNet.FeatureModel": "1.0.0-*",
"Microsoft.AspNet.FileProviders.Interfaces": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel": "1.0.0-*"
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }
},
"frameworks": {
"dnx451": {},
"dnxcore50": {}
}
}
}
26 changes: 26 additions & 0 deletions test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ public void WebRootCanBeResolvedFromTheProjectJson()
Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists);
}

[Fact]
public void Validate_Environment_Name()
{
var services = HostingServices.Create().BuildServiceProvider();
var env = services.GetRequiredService<IHostingEnvironment>();
Assert.Equal("Development", env.EnvironmentName);

var config = new Configuration()
.AddCommandLine(new string[] { "--ASPNET_ENV", "Overridden_Environment" });

services = HostingServices.Create(fallbackServices: null, configuration: config)
.BuildServiceProvider();

env = services.GetRequiredService<IHostingEnvironment>();
Assert.Equal("Overridden_Environment", env.EnvironmentName);
}

[Fact]
public void IsEnvironment_Extension_Is_Case_Insensitive()
{
var services = HostingServices.Create().BuildServiceProvider();
var env = services.GetRequiredService<IHostingEnvironment>();
Assert.True(env.IsEnvironment("Development"));
Assert.True(env.IsEnvironment("developMent"));
}

public void Initialize(IApplicationBuilder builder)
{

Expand Down

0 comments on commit 3b0d5fd

Please sign in to comment.