Add new constructor to TestSever which allows providing preconfigured FeatureCollection to use before Build / Start is invoked. (#967)
This commit is contained in:
parent
95de690acd
commit
120fec741d
|
|
@ -21,7 +21,23 @@ namespace Microsoft.AspNetCore.TestHost
|
|||
private IHttpApplication<Context> _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
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<IServerAddressesFeature>();
|
||||
Assert.Contains(serverAddressesFeature.Addresses, s => string.Equals(s, url, StringComparison.Ordinal));
|
||||
});
|
||||
|
||||
|
||||
var featureCollection = new FeatureCollection();
|
||||
featureCollection.Set<IServerAddressesFeature>(new ServerAddressesFeature());
|
||||
|
||||
// Act
|
||||
new TestServer(builder, featureCollection);
|
||||
|
||||
// Assert
|
||||
// Is inside configure callback
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestServerConstructorWithNullFeatureCollectionThrows()
|
||||
{
|
||||
var builder = new WebHostBuilder()
|
||||
.Configure(b => { });
|
||||
|
||||
Assert.Throws<ArgumentNullException>(() => new TestServer(builder, null));
|
||||
}
|
||||
|
||||
public class TestService { }
|
||||
|
||||
public class TestRequestServiceMiddleware
|
||||
|
|
@ -628,4 +662,4 @@ namespace Microsoft.AspNetCore.TestHost
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue