Add TestUrlHelper class and GetAddress() extension method (#1387)

This commit is contained in:
Mike Harder 2018-04-18 14:04:16 -07:00 committed by GitHub
parent 1cdd9bab0e
commit 970bc8a30d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 14 deletions

View File

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Hosting.Server.Features;
using System.Linq;
namespace Microsoft.AspNetCore.Hosting
{
public static class IWebHostExtensions
{
public static string GetAddress(this IWebHost host)
{
return host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First();
}
}
}

View File

@ -7,22 +7,20 @@ using System.Net.Sockets;
namespace Microsoft.AspNetCore.Server.IntegrationTesting.Common
{
public static class TestUriHelper
internal static class TestUriHelper
{
public static Uri BuildTestUri()
internal static Uri BuildTestUri(ServerType serverType)
{
return BuildTestUri(null);
return BuildTestUri(serverType, hint: null);
}
public static Uri BuildTestUri(string hint)
internal static Uri BuildTestUri(ServerType serverType, string hint)
{
// If this method is called directly, there is no way to know the server type or whether status messages
// are enabled. It's safest to assume the server is WebListener (which doesn't support binding to dynamic
// port "0") and status messages are not enabled (so the assigned port cannot be scraped from console output).
return BuildTestUri(hint, serverType: ServerType.WebListener, statusMessagesEnabled: false);
// Assume status messages are enabled for Kestrel and disabled for all other servers.
return BuildTestUri(serverType, hint, statusMessagesEnabled: serverType == ServerType.Kestrel);
}
internal static Uri BuildTestUri(string hint, ServerType serverType, bool statusMessagesEnabled)
internal static Uri BuildTestUri(ServerType serverType, string hint, bool statusMessagesEnabled)
{
if (string.IsNullOrEmpty(hint))
{
@ -73,7 +71,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.Common
// (with status messages enabled) should directly bind to dynamic port "0" and scrape
// the assigned port from the status message, which should be 100% reliable since the port
// is bound once and never released.
public static int GetNextPort()
internal static int GetNextPort()
{
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{

View File

@ -0,0 +1,11 @@
namespace Microsoft.AspNetCore.Server.IntegrationTesting.Common
{
// Public for use in other test projects
public static class TestUrlHelper
{
public static string GetTestUrl(ServerType serverType)
{
return TestUriHelper.BuildTestUri(serverType).ToString();
}
}
}

View File

@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting
var contentRoot = DeploymentParameters.PublishApplicationBeforeDeployment ? DeploymentParameters.PublishedApplicationRootPath : DeploymentParameters.ApplicationPath;
var testUri = TestUriHelper.BuildTestUri(DeploymentParameters.ApplicationBaseUriHint);
var testUri = TestUriHelper.BuildTestUri(ServerType.IISExpress, DeploymentParameters.ApplicationBaseUriHint);
// Launch the host process.
var (actualUri, hostExitToken) = await StartIISExpressAsync(testUri, contentRoot);

View File

@ -30,10 +30,10 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting
{
_configFile = Path.GetTempFileName();
var uri = string.IsNullOrEmpty(DeploymentParameters.ApplicationBaseUriHint) ?
TestUriHelper.BuildTestUri() :
TestUriHelper.BuildTestUri(ServerType.Nginx) :
new Uri(DeploymentParameters.ApplicationBaseUriHint);
var redirectUri = TestUriHelper.BuildTestUri();
var redirectUri = TestUriHelper.BuildTestUri(ServerType.Nginx);
if (DeploymentParameters.PublishApplicationBeforeDeployment)
{

View File

@ -42,8 +42,8 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting
}
var hintUrl = TestUriHelper.BuildTestUri(
DeploymentParameters.ApplicationBaseUriHint,
DeploymentParameters.ServerType,
DeploymentParameters.ApplicationBaseUriHint,
DeploymentParameters.StatusMessagesEnabled);
// Launch the host process.

View File

@ -29,4 +29,8 @@
<PackageReference Include="Serilog.Sinks.File" Version="$(SerilogSinksFilePackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.AspNetCore.Hosting.Abstractions\Microsoft.AspNetCore.Hosting.Abstractions.csproj" />
</ItemGroup>
</Project>