This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 527
Honor PreferHostingUrls #1639
Closed
Closed
Honor PreferHostingUrls #1639
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,10 +153,7 @@ private async Task RegisterDefaultServerAddresses_Success(IEnumerable<string> ad | |
|
||
var hostBuilder = new WebHostBuilder() | ||
.UseKestrel() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddSingleton<ILoggerFactory>(new KestrelTestLoggerFactory(testLogger)); | ||
}) | ||
.UseLoggerFactory(_ => new KestrelTestLoggerFactory(testLogger)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the overload that takes an instance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were thinking of obsoleting that overload. aspnet/Hosting#1007. We want to use the overloads that also take in the WebHostBuilderContext. |
||
.Configure(ConfigureEchoAddress); | ||
|
||
using (var host = hostBuilder.Build()) | ||
|
@@ -218,6 +215,71 @@ public void ThrowsWhenBindingToIPv6AddressInUse() | |
} | ||
} | ||
|
||
[ConditionalFact] | ||
[PortSupportedCondition(5002)] | ||
public async Task OverrideDirectConfigurationWithIServerAddressesFeature_Succeeds() | ||
{ | ||
var overrideAddress = "http://localhost:5002"; | ||
var testLogger = new TestApplicationErrorLogger(); | ||
var hostBuilder = new WebHostBuilder() | ||
.UseKestrel(options => options.Listen(IPAddress.Loopback, 5001)) | ||
.UseUrls(overrideAddress) | ||
.PreferHostingUrls(true) | ||
.UseLoggerFactory(_ => new KestrelTestLoggerFactory(testLogger)) | ||
.Configure(ConfigureEchoAddress); | ||
|
||
using (var host = hostBuilder.Build()) | ||
{ | ||
host.Start(); | ||
|
||
Assert.Equal(5002, host.GetPort()); | ||
Assert.Single(testLogger.Messages, log => log.LogLevel == LogLevel.Warning && | ||
string.Equals($"Overriding endpoints defined in UseKestrel() since {nameof(IServerAddressesFeature.PreferHostingUrls)} is set to true. Binding to address(es) '{overrideAddress}' instead.", | ||
log.Message, StringComparison.Ordinal)); | ||
|
||
Assert.Equal(new Uri(overrideAddress).ToString(), await HttpClientSlim.GetStringAsync(overrideAddress)); | ||
} | ||
} | ||
|
||
[ConditionalFact] | ||
[PortSupportedCondition(5001)] | ||
public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfPreferHostingUrlsFalse() | ||
{ | ||
var endPointAddress = "http://localhost:5001"; | ||
var hostBuilder = new WebHostBuilder() | ||
.UseKestrel(options => options.Listen(IPAddress.Loopback, 5001)) | ||
.UseUrls("http://localhost:5002") | ||
.PreferHostingUrls(false) | ||
.Configure(ConfigureEchoAddress); | ||
|
||
using (var host = hostBuilder.Build()) | ||
{ | ||
host.Start(); | ||
|
||
Assert.Equal(5001, host.GetPort()); | ||
Assert.Equal(new Uri(endPointAddress).ToString(), await HttpClientSlim.GetStringAsync(endPointAddress)); | ||
} | ||
} | ||
|
||
[ConditionalFact] | ||
[PortSupportedCondition(5000)] | ||
public async Task DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfAddressesEmpty() | ||
{ | ||
var endPointAddress = "http://localhost:5001"; | ||
var hostBuilder = new WebHostBuilder() | ||
.UseKestrel(options => options.Listen(IPAddress.Loopback, 5001)) | ||
.PreferHostingUrls(true) | ||
.Configure(ConfigureEchoAddress); | ||
|
||
using (var host = hostBuilder.Build()) | ||
{ | ||
host.Start(); | ||
|
||
Assert.Equal(5001, host.GetPort()); | ||
Assert.Equal(new Uri(endPointAddress).ToString(), await HttpClientSlim.GetStringAsync(endPointAddress)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ThrowsWhenBindingLocalhostToIPv4AddressInUse() | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big fan of how these if else are structured since it's somewhat convoluted. As far as I can tell, it's to reduce code duplication for translating server addresses to listen options and binding to the listen options.
I have an alternative approach added to the branch at https://github.com/aspnet/KestrelHttpServer/compare/johluo/use-hosting-urls?diff=split&expand=1&name=johluo%2Fuse-hosting-urls but there's a bit of moving things around so I'm not sure if it's worth the effort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the alternative approach; it's easier to follow the logic in that version.