Add kestrel tests.

Tests will 'build', 'run', 'publish' and 'execute' a Kestrel Hello World server as a PortableFatApp and as a Standalone app.
This commit is contained in:
Sridhar Periyasamy 2016-04-04 17:51:36 -07:00
parent f665c28173
commit 8f00b95783
17 changed files with 766 additions and 25 deletions

View file

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.ProjectModel;
using FluentAssertions;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public class NetworkHelper
{
// in milliseconds
private const int Timeout = 20000;
private static Queue<TcpListener> s_PortPool = new Queue<TcpListener>();
public static string Localhost { get; } = "http://localhost";
public static bool IsServerUp(string url)
{
return SpinWait.SpinUntil(() =>
{
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri(url);
HttpResponseMessage response = client.GetAsync("").Result;
return response.IsSuccessStatusCode;
}
catch (Exception)
{
return false;
}
}
}, Timeout);
}
public static void TestGetRequest(string url, string expectedResponse)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
HttpResponseMessage response = client.GetAsync("").Result;
if (response.IsSuccessStatusCode)
{
var responseString = response.Content.ReadAsStringAsync().Result;
responseString.Should().Contain(expectedResponse);
}
}
}
public static int GetFreePort()
{
lock (s_PortPool)
{
if (s_PortPool.Count == 0)
{
for (int i = 0; i < 20; i++)
{
var tcpl = new TcpListener(IPAddress.Loopback, 0);
tcpl.Start();
s_PortPool.Enqueue(tcpl);
}
Console.WriteLine($"Ports Count >>>>>>>>>>>>>>>>>>> {s_PortPool.Count}");
}
var currentTcpl = s_PortPool.Dequeue();
var port = ((IPEndPoint)currentTcpl.LocalEndpoint).Port;
currentTcpl.Stop();
currentTcpl = null;
GC.Collect();
GC.WaitForPendingFinalizers();
return port;
}
}
public static string GetLocalhostUrlWithFreePort()
{
return $"{Localhost}:{GetFreePort()}/";
}
}
}