This commit is contained in:
Dave Tillman 2017-04-20 07:16:15 -06:00
Родитель efc491e541
Коммит eb249c752c
4 изменённых файлов: 0 добавлений и 143 удалений

Просмотреть файл

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
namespace SteelToe.Extensions.Configuration.CloudFoundry
{
public static class CloudFoundryHostBuilderExtensions
{
public static IWebHostBuilder UseCloudFoundryHosting(this IWebHostBuilder webHostBuilder)
{
if(webHostBuilder == null)
{
throw new ArgumentNullException(nameof(webHostBuilder));
}
List<string> urls = new List<string>();
string portStr = Environment.GetEnvironmentVariable("PORT");
if (!string.IsNullOrWhiteSpace(portStr))
{
int port;
if (int.TryParse(portStr, out port))
{
urls.Add($"http://*:{port}");
}
}
if (urls.Count > 0)
{
webHostBuilder.UseUrls(urls.ToArray());
}
return webHostBuilder;
}
}
}

Просмотреть файл

@ -1,60 +0,0 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace SteelToe.Extensions.Configuration.CloudFoundry.Test
{
public class CloudFoundryHostBuilderExtensionsTest
{
[Fact]
public void AddCloudFoundry_ThrowsIfHostBuilderNull()
{
// Arrange
IWebHostBuilder webHostBuilder = null;
// Act and Assert
var ex = Assert.Throws<ArgumentNullException>(() => CloudFoundryHostBuilderExtensions.UseCloudFoundryHosting(webHostBuilder));
Assert.Contains(nameof(webHostBuilder), ex.Message);
}
[Fact]
public void AddCloudFoundry_DoNotSetUrlsIfNull()
{
// Arrange
Environment.SetEnvironmentVariable("PORT", null);
var hostBuilder = new WebHostBuilder()
.UseStartup<TestServerStartup>()
.UseKestrel();
// Act and Assert
hostBuilder.UseCloudFoundryHosting();
using (hostBuilder.Build())
{
//No-Op
}
}
[Fact]
public void AddCloudFoundry_MakeSureThePortIsSet()
{
// Arrange
Environment.SetEnvironmentVariable("PORT", "42");
var hostBuilder = new WebHostBuilder()
.UseStartup<TestServerStartup42>()
.UseKestrel();
// Act and Assert
hostBuilder.UseCloudFoundryHosting();
using (hostBuilder.Build())
{
//No-Op
}
}
}
}

Просмотреть файл

@ -1,30 +0,0 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace SteelToe.Extensions.Configuration.CloudFoundry.Test
{
public class TestServerStartup
{
public List<string> ExpectedAddresses = new List<string>();
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
var addresses = ExpectedAddresses.DefaultIfEmpty("http://localhost:5000");
Assert.Equal(addresses, app.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses);
app.UseMvc();
}
}
}

Просмотреть файл

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SteelToe.Extensions.Configuration.CloudFoundry.Test
{
public class TestServerStartup42 : TestServerStartup
{
public TestServerStartup42()
{
this.ExpectedAddresses.Add("http://*:42");
}
}
}