React to addition of PreferHostingUrls

This commit is contained in:
John Luo 2017-04-02 18:04:22 -07:00
Родитель fd27c7fcc6
Коммит bc003985c6
2 изменённых файлов: 80 добавлений и 10 удалений

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

@ -2,11 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
@ -69,24 +67,39 @@ namespace Microsoft.AspNetCore.Server.HttpSys
throw new ArgumentNullException(nameof(application));
}
var serverAdressesPresent = _serverAddresses.Addresses.Count > 0;
var hostingUrlsPresent = _serverAddresses.Addresses.Count > 0;
if (_options.UrlPrefixes.Count > 0)
if (_serverAddresses.PreferHostingUrls && hostingUrlsPresent)
{
if (serverAdressesPresent)
if (_options.UrlPrefixes.Count > 0)
{
LogHelper.LogWarning(_logger, $"Overriding endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} since {nameof(IServerAddressesFeature.PreferHostingUrls)} is set to true." +
$" Binding to address(es) '{string.Join(", ", _serverAddresses.Addresses)}' instead. ");
Listener.Options.UrlPrefixes.Clear();
}
foreach (var value in _serverAddresses.Addresses)
{
Listener.Options.UrlPrefixes.Add(value);
}
}
else if (_options.UrlPrefixes.Count > 0)
{
if (hostingUrlsPresent)
{
LogHelper.LogWarning(_logger, $"Overriding address(es) '{string.Join(", ", _serverAddresses.Addresses)}'. " +
$"Binding to endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} instead.");
_serverAddresses.Addresses.Clear();
}
foreach (var prefix in _options.UrlPrefixes)
{
_serverAddresses.Addresses.Add(prefix.FullPrefix);
}
foreach (var prefix in _options.UrlPrefixes)
{
_serverAddresses.Addresses.Add(prefix.FullPrefix);
}
}
else if (serverAdressesPresent)
else if (hostingUrlsPresent)
{
foreach (var value in _serverAddresses.Addresses)
{

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

@ -12,6 +12,63 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{
public class MessagePumpTests
{
[ConditionalFact]
public void OverridingDirectConfigurationWithIServerAddressesFeatureSucceeds()
{
var serverAddress = "http://localhost:11001/";
var overrideAddress = "http://localhost:11002/";
using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory()))
{
var serverAddressesFeature = server.Features.Get<IServerAddressesFeature>();
serverAddressesFeature.Addresses.Add(overrideAddress);
serverAddressesFeature.PreferHostingUrls = true;
server.Listener.Options.UrlPrefixes.Add(serverAddress);
server.Start(new DummyApplication());
Assert.Equal(overrideAddress, serverAddressesFeature.Addresses.Single());
}
}
[ConditionalTheory]
[InlineData("http://localhost:11001/")]
[InlineData("invalid address")]
[InlineData("")]
[InlineData(null)]
public void DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfPreferHostinUrlsFalse(string overrideAddress)
{
var serverAddress = "http://localhost:11002/";
using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory()))
{
var serverAddressesFeature = server.Features.Get<IServerAddressesFeature>();
serverAddressesFeature.Addresses.Add(overrideAddress);
server.Listener.Options.UrlPrefixes.Add(serverAddress);
server.Start(new DummyApplication());
Assert.Equal(serverAddress, serverAddressesFeature.Addresses.Single());
}
}
[ConditionalFact]
public void DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfAddressesIsEmpty()
{
var serverAddress = "http://localhost:11002/";
using (var server = new MessagePump(Options.Create(new HttpSysOptions()), new LoggerFactory()))
{
var serverAddressesFeature = server.Features.Get<IServerAddressesFeature>();
serverAddressesFeature.PreferHostingUrls = true;
server.Listener.Options.UrlPrefixes.Add(serverAddress);
server.Start(new DummyApplication());
Assert.Equal(serverAddress, serverAddressesFeature.Addresses.Single());
}
}
[ConditionalTheory]
[InlineData("http://localhost:11001/")]
[InlineData("invalid address")]