Skip to content

Commit

Permalink
Fix host builder case sensitivity and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
halter73 committed Apr 16, 2021
1 parent 8a24d52 commit eb4ef57
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/DefaultBuilder/src/ConfigureWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class ConfigureWebHostBuilder : IWebHostBuilder

private readonly WebHostEnvironment _environment;
private readonly Configuration _configuration;
private readonly Dictionary<string, string?> _settings = new Dictionary<string, string?>();
private readonly Dictionary<string, string?> _settings = new(StringComparer.OrdinalIgnoreCase);
private readonly IServiceCollection _services;

internal ConfigureWebHostBuilder(Configuration configuration, WebHostEnvironment environment, IServiceCollection services)
Expand Down Expand Up @@ -78,22 +78,22 @@ public IWebHostBuilder UseSetting(string key, string? value)
return this;
}

if (key == WebHostDefaults.ApplicationKey)
if (string.Equals(key, WebHostDefaults.ApplicationKey, StringComparison.OrdinalIgnoreCase))
{
_environment.ApplicationName = value;
}
else if (key == WebHostDefaults.ContentRootKey)
else if (string.Equals(key, WebHostDefaults.ContentRootKey, StringComparison.OrdinalIgnoreCase))
{
_environment.ContentRootPath = value;
_environment.ResolveFileProviders(_configuration);

_configuration.ChangeBasePath(value);
}
else if (key == WebHostDefaults.EnvironmentKey)
else if (string.Equals(key, WebHostDefaults.EnvironmentKey, StringComparison.OrdinalIgnoreCase))
{
_environment.EnvironmentName = value;
}
else if (key == WebHostDefaults.WebRootKey)
else if (string.Equals(key, WebHostDefaults.WebRootKey, StringComparison.OrdinalIgnoreCase))
{
_environment.WebRootPath = value;
_environment.ResolveFileProviders(_configuration);
Expand Down
2 changes: 1 addition & 1 deletion src/DefaultBuilder/src/WebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Builder
/// <summary>
/// The web application used to configure the http pipeline, and routes.
/// </summary>
public sealed class WebApplication : IHost, IDisposable, IApplicationBuilder, IEndpointRouteBuilder, IAsyncDisposable
public sealed class WebApplication : IHost, IApplicationBuilder, IEndpointRouteBuilder, IAsyncDisposable
{
internal const string EndpointRouteBuilder = "__EndpointRouteBuilder";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -65,6 +66,44 @@ public void WebApplicationBuilderWebHost_ThrowsWhenBuiltDirectly()
Assert.Throws<NotSupportedException>(() => ((IWebHostBuilder)WebApplication.CreateBuilder().WebHost).Build());
}

[Fact]
public void WebApplicationBuilderWebHostUseSettings_IsCaseInsensitive()
{
var builder = WebApplication.CreateBuilder();

var contentRoot = Path.GetTempPath().ToString();
var webRoot = Path.GetTempPath().ToString();
var envName = $"{nameof(WebApplicationTests)}_ENV";

builder.WebHost.UseSetting("applicationname", nameof(WebApplicationTests));
builder.WebHost.UseSetting("ENVIRONMENT", envName);
builder.WebHost.UseSetting("CONTENTROOT", contentRoot);
builder.WebHost.UseSetting("WEBROOT", webRoot);

Assert.Equal(nameof(WebApplicationTests), builder.WebHost.GetSetting("APPLICATIONNAME"));
Assert.Equal(envName, builder.WebHost.GetSetting("environment"));
Assert.Equal(contentRoot, builder.WebHost.GetSetting("contentroot"));
Assert.Equal(webRoot, builder.WebHost.GetSetting("webroot"));

var app = builder.Build();

Assert.Equal(nameof(WebApplicationTests), app.Environment.ApplicationName);
Assert.Equal(envName, app.Environment.EnvironmentName);
Assert.Equal(contentRoot, app.Environment.ContentRootPath);
Assert.Equal(webRoot, app.Environment.WebRootPath);
}

[Fact]
public void WebApplicationBuilderHostProperties_IsCaseSensitive()
{
var builder = WebApplication.CreateBuilder();

builder.Host.Properties["lowercase"] = nameof(WebApplicationTests);

Assert.Equal(nameof(WebApplicationTests), builder.Host.Properties["lowercase"]);
Assert.False(builder.Host.Properties.ContainsKey("Lowercase"));
}

[Fact]
public async Task WebApplicationConfiguration_HostFilterOptionsAreReloadable()
{
Expand Down

0 comments on commit eb4ef57

Please sign in to comment.