From 970bc8a30d66dd6894f8f662e5fdab9e68d57777 Mon Sep 17 00:00:00 2001 From: Mike Harder Date: Wed, 18 Apr 2018 14:04:16 -0700 Subject: [PATCH] Add TestUrlHelper class and GetAddress() extension method (#1387) --- .../Common/IWebHostExtensions.cs | 13 +++++++++++++ .../Common/TestUriHelper.cs | 18 ++++++++---------- .../Common/TestUrlHelper.cs | 11 +++++++++++ .../Deployers/IISExpressDeployer.cs | 2 +- .../Deployers/NginxDeployer.cs | 4 ++-- .../Deployers/SelfHostDeployer.cs | 2 +- ...AspNetCore.Server.IntegrationTesting.csproj | 4 ++++ 7 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/IWebHostExtensions.cs create mode 100644 src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUrlHelper.cs diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/IWebHostExtensions.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/IWebHostExtensions.cs new file mode 100644 index 0000000000..732a598ab8 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/IWebHostExtensions.cs @@ -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().Addresses.First(); + } + } +} diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUriHelper.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUriHelper.cs index 7ebcb30367..c1feec8c6d 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUriHelper.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUriHelper.cs @@ -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)) { diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUrlHelper.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUrlHelper.cs new file mode 100644 index 0000000000..f03321eba6 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/TestUrlHelper.cs @@ -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(); + } + } +} diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/IISExpressDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/IISExpressDeployer.cs index bc7aecb700..85ca880094 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/IISExpressDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/IISExpressDeployer.cs @@ -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); diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/NginxDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/NginxDeployer.cs index 1bc6c4b766..7341624d41 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/NginxDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/NginxDeployer.cs @@ -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) { diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs index 2fcb391f72..4204d7415a 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs @@ -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. diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj index c38a2ded59..02c01e1003 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj @@ -29,4 +29,8 @@ + + + +