Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Add new constructor to TestSever which allows providing preconfigured…
Browse files Browse the repository at this point in the history
… FeatureCollection to use before Build / Start is invoked. (#967)
  • Loading branch information
mattmazzola authored and Tratcher committed Mar 21, 2017
1 parent 95de690 commit 120fec7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/Microsoft.AspNetCore.TestHost/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ public class TestServer : IServer
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;
Expand All @@ -37,7 +53,7 @@ public IWebHost Host
}
}

IFeatureCollection IServer.Features { get; }
public IFeatureCollection Features { get; }

public HttpMessageHandler CreateHandler()
{
Expand Down Expand Up @@ -114,4 +130,4 @@ public Task ProcessRequestAsync(TContext context)
}
}
}
}
}
36 changes: 35 additions & 1 deletion test/Microsoft.AspNetCore.TestHost.Tests/TestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -119,6 +120,39 @@ public async Task CustomServiceProviderSetsApplicationServices()
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
Expand Down Expand Up @@ -628,4 +662,4 @@ public void ConfigureFoo(IApplicationBuilder app)
}
}
}
}
}

0 comments on commit 120fec7

Please sign in to comment.