Add TestUrlHelper class and GetAddress() extension method (#1387)
This commit is contained in:
parent
1cdd9bab0e
commit
970bc8a30d
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,22 +7,20 @@ using System.Net.Sockets;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Server.IntegrationTesting.Common
|
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
|
// Assume status messages are enabled for Kestrel and disabled for all other servers.
|
||||||
// are enabled. It's safest to assume the server is WebListener (which doesn't support binding to dynamic
|
return BuildTestUri(serverType, hint, statusMessagesEnabled: serverType == ServerType.Kestrel);
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static Uri BuildTestUri(string hint, ServerType serverType, bool statusMessagesEnabled)
|
internal static Uri BuildTestUri(ServerType serverType, string hint, bool statusMessagesEnabled)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(hint))
|
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
|
// (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
|
// the assigned port from the status message, which should be 100% reliable since the port
|
||||||
// is bound once and never released.
|
// 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))
|
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting
|
||||||
|
|
||||||
var contentRoot = DeploymentParameters.PublishApplicationBeforeDeployment ? DeploymentParameters.PublishedApplicationRootPath : DeploymentParameters.ApplicationPath;
|
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.
|
// Launch the host process.
|
||||||
var (actualUri, hostExitToken) = await StartIISExpressAsync(testUri, contentRoot);
|
var (actualUri, hostExitToken) = await StartIISExpressAsync(testUri, contentRoot);
|
||||||
|
|
|
||||||
|
|
@ -30,10 +30,10 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting
|
||||||
{
|
{
|
||||||
_configFile = Path.GetTempFileName();
|
_configFile = Path.GetTempFileName();
|
||||||
var uri = string.IsNullOrEmpty(DeploymentParameters.ApplicationBaseUriHint) ?
|
var uri = string.IsNullOrEmpty(DeploymentParameters.ApplicationBaseUriHint) ?
|
||||||
TestUriHelper.BuildTestUri() :
|
TestUriHelper.BuildTestUri(ServerType.Nginx) :
|
||||||
new Uri(DeploymentParameters.ApplicationBaseUriHint);
|
new Uri(DeploymentParameters.ApplicationBaseUriHint);
|
||||||
|
|
||||||
var redirectUri = TestUriHelper.BuildTestUri();
|
var redirectUri = TestUriHelper.BuildTestUri(ServerType.Nginx);
|
||||||
|
|
||||||
if (DeploymentParameters.PublishApplicationBeforeDeployment)
|
if (DeploymentParameters.PublishApplicationBeforeDeployment)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,8 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting
|
||||||
}
|
}
|
||||||
|
|
||||||
var hintUrl = TestUriHelper.BuildTestUri(
|
var hintUrl = TestUriHelper.BuildTestUri(
|
||||||
DeploymentParameters.ApplicationBaseUriHint,
|
|
||||||
DeploymentParameters.ServerType,
|
DeploymentParameters.ServerType,
|
||||||
|
DeploymentParameters.ApplicationBaseUriHint,
|
||||||
DeploymentParameters.StatusMessagesEnabled);
|
DeploymentParameters.StatusMessagesEnabled);
|
||||||
|
|
||||||
// Launch the host process.
|
// Launch the host process.
|
||||||
|
|
|
||||||
|
|
@ -29,4 +29,8 @@
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="$(SerilogSinksFilePackageVersion)" />
|
<PackageReference Include="Serilog.Sinks.File" Version="$(SerilogSinksFilePackageVersion)" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Microsoft.AspNetCore.Hosting.Abstractions\Microsoft.AspNetCore.Hosting.Abstractions.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue