diff --git a/src/Microsoft.AspNetCore.TestHost/TestServer.cs b/src/Microsoft.AspNetCore.TestHost/TestServer.cs index cdc7faa4c6..9d7ab9f4a7 100644 --- a/src/Microsoft.AspNetCore.TestHost/TestServer.cs +++ b/src/Microsoft.AspNetCore.TestHost/TestServer.cs @@ -21,7 +21,23 @@ namespace Microsoft.AspNetCore.TestHost private IHttpApplication _application; public TestServer(IWebHostBuilder builder) + : this(builder, new FeatureCollection()) { + } + + public TestServer(IWebHostBuilder builder, IFeatureCollection featureCollection) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + if (featureCollection == null) + { + throw new ArgumentNullException(nameof(featureCollection)); + } + + Features = featureCollection; + var host = builder.UseServer(this).Build(); host.Start(); _hostInstance = host; @@ -37,7 +53,7 @@ namespace Microsoft.AspNetCore.TestHost } } - IFeatureCollection IServer.Features { get; } + public IFeatureCollection Features { get; } public HttpMessageHandler CreateHandler() { @@ -114,4 +130,4 @@ namespace Microsoft.AspNetCore.TestHost } } } -} \ No newline at end of file +} diff --git a/test/Microsoft.AspNetCore.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNetCore.TestHost.Tests/TestServerTests.cs index f6d950a4b8..c89af26114 100644 --- a/test/Microsoft.AspNetCore.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNetCore.TestHost.Tests/TestServerTests.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Testing.xunit; @@ -119,6 +120,39 @@ namespace Microsoft.AspNetCore.TestHost Assert.Equal("ApplicationServicesEqual:True", result); } + [Fact] + public void TestServerConstructorWithFeatureCollectionAllowsInitializingServerFeatures() + { + // Arrange + var url = "http://localhost:8000/appName/serviceName"; + var builder = new WebHostBuilder() + .UseUrls(url) + .Configure(applicationBuilder => + { + var serverAddressesFeature = applicationBuilder.ServerFeatures.Get(); + Assert.Contains(serverAddressesFeature.Addresses, s => string.Equals(s, url, StringComparison.Ordinal)); + }); + + + var featureCollection = new FeatureCollection(); + featureCollection.Set(new ServerAddressesFeature()); + + // Act + new TestServer(builder, featureCollection); + + // Assert + // Is inside configure callback + } + + [Fact] + public void TestServerConstructorWithNullFeatureCollectionThrows() + { + var builder = new WebHostBuilder() + .Configure(b => { }); + + Assert.Throws(() => new TestServer(builder, null)); + } + public class TestService { } public class TestRequestServiceMiddleware @@ -628,4 +662,4 @@ namespace Microsoft.AspNetCore.TestHost } } } -} \ No newline at end of file +}