React to addition of PreferHostingUrls

This commit is contained in:
John Luo 2017-04-02 18:04:22 -07:00
parent fd27c7fcc6
commit bc003985c6
2 changed files with 80 additions and 10 deletions

View File

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

View File

@ -12,6 +12,63 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
public class MessagePumpTests 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] [ConditionalTheory]
[InlineData("http://localhost:11001/")] [InlineData("http://localhost:11001/")]
[InlineData("invalid address")] [InlineData("invalid address")]