|
| 1 | +// Copyright 2017 the original author or authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using Microsoft.AspNetCore; |
| 16 | +using Microsoft.AspNetCore.Hosting; |
| 17 | +using Microsoft.AspNetCore.Hosting.Server.Features; |
| 18 | +using Microsoft.AspNetCore.TestHost; |
| 19 | +using Microsoft.Extensions.Configuration; |
| 20 | +using Microsoft.Extensions.DependencyInjection; |
| 21 | +using Microsoft.Extensions.Hosting; |
| 22 | +using Steeltoe.Extensions.Configuration.CloudFoundry; |
| 23 | +using Steeltoe.Extensions.Configuration.ConfigServer; |
| 24 | +using System; |
| 25 | +using System.Linq; |
| 26 | +using Xunit; |
| 27 | + |
| 28 | +namespace Steeltoe.Extensions.Configuration.ConfigServerCore.Test |
| 29 | +{ |
| 30 | + public class ConfigServerHostBuilderExtensionsTest |
| 31 | + { |
| 32 | + [Fact] |
| 33 | + public void AddConfigServer_DefaultWebHost_AddsConfigServer() |
| 34 | + { |
| 35 | + // Arrange |
| 36 | + var hostBuilder = WebHost.CreateDefaultBuilder().UseStartup<TestConfigServerStartup>(); |
| 37 | + |
| 38 | + // Act |
| 39 | + hostBuilder.AddConfigServer(); |
| 40 | + var config = hostBuilder.Build().Services.GetServices<IConfiguration>().SingleOrDefault() as ConfigurationRoot; |
| 41 | + |
| 42 | + // Assert |
| 43 | + Assert.Single(config.Providers.OfType<CloudFoundryConfigurationProvider>()); |
| 44 | + Assert.Single(config.Providers.OfType<ConfigServerConfigurationProvider>()); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void AddConfigServer_New_WebHostBuilder_AddsConfigServer() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var hostBuilder = new WebHostBuilder().UseStartup<TestConfigServerStartup>(); |
| 52 | + |
| 53 | + // Act |
| 54 | + hostBuilder.AddConfigServer(); |
| 55 | + var config = hostBuilder.Build().Services.GetServices<IConfiguration>().SingleOrDefault() as ConfigurationRoot; |
| 56 | + |
| 57 | + // Assert |
| 58 | + Assert.Single(config.Providers.OfType<CloudFoundryConfigurationProvider>()); |
| 59 | + Assert.Single(config.Providers.OfType<ConfigServerConfigurationProvider>()); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void AddConfigServer_IHostBuilder_AddsConfigServer() |
| 64 | + { |
| 65 | + // Arrange |
| 66 | + var hostBuilder = new HostBuilder().AddConfigServer(); |
| 67 | + |
| 68 | + // Act |
| 69 | + var host = hostBuilder.Build(); |
| 70 | + var config = host.Services.GetServices<IConfiguration>().SingleOrDefault() as ConfigurationRoot; |
| 71 | + |
| 72 | + // Assert |
| 73 | + Assert.Single(config.Providers.OfType<CloudFoundryConfigurationProvider>()); |
| 74 | + Assert.Single(config.Providers.OfType<ConfigServerConfigurationProvider>()); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void UseCloudFoundryHosting_ThrowsIfHostBuilderNull() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + IWebHostBuilder webHostBuilder = null; |
| 82 | + |
| 83 | + // Act and Assert |
| 84 | + var ex = Assert.Throws<ArgumentNullException>(() => ConfigServerHostBuilderExtensions.UseCloudFoundryHosting(webHostBuilder)); |
| 85 | + Assert.Contains(nameof(webHostBuilder), ex.Message); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void UseCloudFoundryHosting_DoNotSetUrlsIfNull() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + Environment.SetEnvironmentVariable("PORT", null); |
| 93 | + var hostBuilder = WebHost.CreateDefaultBuilder().UseStartup<TestConfigServerStartup>(); |
| 94 | + |
| 95 | + // Act |
| 96 | + hostBuilder.UseCloudFoundryHosting(); |
| 97 | + var server = new TestServer(hostBuilder); |
| 98 | + |
| 99 | + // Assert |
| 100 | + var addresses = server.Host.ServerFeatures.Get<IServerAddressesFeature>(); |
| 101 | + Assert.Null(addresses); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void UseCloudFoundryHosting_MakeSureThePortIsSet() |
| 106 | + { |
| 107 | + // Arrange |
| 108 | + Environment.SetEnvironmentVariable("PORT", "42"); |
| 109 | + var hostBuilder = WebHost.CreateDefaultBuilder().UseStartup<TestConfigServerStartup>(); |
| 110 | + |
| 111 | + // Act |
| 112 | + hostBuilder.UseCloudFoundryHosting(); |
| 113 | + var server = hostBuilder.Build(); |
| 114 | + |
| 115 | + // Assert |
| 116 | + var addresses = server.ServerFeatures.Get<IServerAddressesFeature>(); |
| 117 | + Assert.Contains("http://*:42", addresses.Addresses); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments