From 80bc8bbbe1de75ef4ca322504f7c433ab589148b Mon Sep 17 00:00:00 2001 From: Troy Dai Date: Thu, 14 Jan 2016 14:47:15 -0800 Subject: [PATCH] Fix test break caused by null ApplicationBaseUrlHint In addtion to the fix here are the changes to improve the helper: 1. Rename the helper to better represent its function; 2. Add overload to generate free port without hint; 3. Add overload to generate local test url; --- .../{FreePortHelper.cs => TestUriHelper.cs} | 28 ++++++++++++++----- .../Deployers/IISDeployer.cs | 4 +-- .../Deployers/IISExpressDeployer.cs | 2 +- .../Deployers/SelfHostDeployer.cs | 2 +- 4 files changed, 24 insertions(+), 12 deletions(-) rename src/Microsoft.AspNet.Server.Testing/Common/{FreePortHelper.cs => TestUriHelper.cs} (59%) diff --git a/src/Microsoft.AspNet.Server.Testing/Common/FreePortHelper.cs b/src/Microsoft.AspNet.Server.Testing/Common/TestUriHelper.cs similarity index 59% rename from src/Microsoft.AspNet.Server.Testing/Common/FreePortHelper.cs rename to src/Microsoft.AspNet.Server.Testing/Common/TestUriHelper.cs index f21ae3d001..d64101f713 100644 --- a/src/Microsoft.AspNet.Server.Testing/Common/FreePortHelper.cs +++ b/src/Microsoft.AspNet.Server.Testing/Common/TestUriHelper.cs @@ -7,16 +7,29 @@ using System.Net.Sockets; namespace Microsoft.AspNet.Server.Testing.Common { - public static class FreePortHelper + public static class TestUriHelper { - public static Uri FindFreeUrl(string urlHint) + public static Uri BuildTestUri() { - var uriHint = new Uri(urlHint); - var builder = new UriBuilder(uriHint) + return new UriBuilder("http", "localhost", FindFreePort()).Uri; + } + + public static Uri BuildTestUri(string hint) + { + if (string.IsNullOrEmpty(hint)) { - Port = FindFreePort(uriHint.Port) - }; - return builder.Uri; + return BuildTestUri(); + } + else + { + var uriHint = new Uri(hint); + return new UriBuilder(uriHint) { Port = FindFreePort(uriHint.Port) }.Uri; + } + } + + public static int FindFreePort() + { + return FindFreePort(0); } public static int FindFreePort(int initialPort) @@ -31,6 +44,7 @@ namespace Microsoft.AspNet.Server.Testing.Common { socket.Bind(new IPEndPoint(IPAddress.Loopback, 0)); } + return ((IPEndPoint)socket.LocalEndPoint).Port; } } diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/IISDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/IISDeployer.cs index f292252842..0c167587a5 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/IISDeployer.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/IISDeployer.cs @@ -5,8 +5,6 @@ using System; using System.Linq; -using System.Net; -using System.Net.Sockets; using System.Threading; using Microsoft.AspNet.Server.Testing.Common; using Microsoft.Extensions.Logging; @@ -46,7 +44,7 @@ namespace Microsoft.AspNet.Server.Testing // Drop a json file instead of setting environment variable. SetAspEnvironmentWithJson(); - var uri = FreePortHelper.FindFreeUrl(DeploymentParameters.ApplicationBaseUriHint); + var uri = TestUriHelper.BuildTestUri(); lock (_syncObject) { diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/IISExpressDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/IISExpressDeployer.cs index 7146908c0c..f7441bf9e0 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/IISExpressDeployer.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/IISExpressDeployer.cs @@ -36,7 +36,7 @@ namespace Microsoft.AspNet.Server.Testing DnuPublish(); } - var uri = FreePortHelper.FindFreeUrl(DeploymentParameters.ApplicationBaseUriHint); + var uri = TestUriHelper.BuildTestUri(DeploymentParameters.ApplicationBaseUriHint); // Launch the host process. var hostExitToken = StartIISExpress(uri); diff --git a/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs b/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs index 4c502189d7..144a9b3a8a 100644 --- a/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs +++ b/src/Microsoft.AspNet.Server.Testing/Deployers/SelfHostDeployer.cs @@ -34,7 +34,7 @@ namespace Microsoft.AspNet.Server.Testing DnuPublish(); } - var uri = FreePortHelper.FindFreeUrl(DeploymentParameters.ApplicationBaseUriHint); + var uri = TestUriHelper.BuildTestUri(DeploymentParameters.ApplicationBaseUriHint); // Launch the host process. var hostExitToken = StartSelfHost(uri);