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;
This commit is contained in:
Troy Dai 2016-01-14 14:47:15 -08:00
parent 3c79b425f1
commit 80bc8bbbe1
4 changed files with 24 additions and 12 deletions

View File

@ -7,16 +7,29 @@ using System.Net.Sockets;
namespace Microsoft.AspNet.Server.Testing.Common 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); return new UriBuilder("http", "localhost", FindFreePort()).Uri;
var builder = new UriBuilder(uriHint) }
public static Uri BuildTestUri(string hint)
{
if (string.IsNullOrEmpty(hint))
{ {
Port = FindFreePort(uriHint.Port) return BuildTestUri();
}; }
return builder.Uri; 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) public static int FindFreePort(int initialPort)
@ -31,6 +44,7 @@ namespace Microsoft.AspNet.Server.Testing.Common
{ {
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0)); socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
} }
return ((IPEndPoint)socket.LocalEndPoint).Port; return ((IPEndPoint)socket.LocalEndPoint).Port;
} }
} }

View File

@ -5,8 +5,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Server.Testing.Common; using Microsoft.AspNet.Server.Testing.Common;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -46,7 +44,7 @@ namespace Microsoft.AspNet.Server.Testing
// Drop a json file instead of setting environment variable. // Drop a json file instead of setting environment variable.
SetAspEnvironmentWithJson(); SetAspEnvironmentWithJson();
var uri = FreePortHelper.FindFreeUrl(DeploymentParameters.ApplicationBaseUriHint); var uri = TestUriHelper.BuildTestUri();
lock (_syncObject) lock (_syncObject)
{ {

View File

@ -36,7 +36,7 @@ namespace Microsoft.AspNet.Server.Testing
DnuPublish(); DnuPublish();
} }
var uri = FreePortHelper.FindFreeUrl(DeploymentParameters.ApplicationBaseUriHint); var uri = TestUriHelper.BuildTestUri(DeploymentParameters.ApplicationBaseUriHint);
// Launch the host process. // Launch the host process.
var hostExitToken = StartIISExpress(uri); var hostExitToken = StartIISExpress(uri);

View File

@ -34,7 +34,7 @@ namespace Microsoft.AspNet.Server.Testing
DnuPublish(); DnuPublish();
} }
var uri = FreePortHelper.FindFreeUrl(DeploymentParameters.ApplicationBaseUriHint); var uri = TestUriHelper.BuildTestUri(DeploymentParameters.ApplicationBaseUriHint);
// Launch the host process. // Launch the host process.
var hostExitToken = StartSelfHost(uri); var hostExitToken = StartSelfHost(uri);