2017-03-03 05:04:03 +00:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
2016-04-05 00:51:36 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FluentAssertions;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
{
|
|
|
|
public class NetworkHelper
|
|
|
|
{
|
|
|
|
// in milliseconds
|
2016-04-08 19:42:43 +00:00
|
|
|
private const int Timeout = 50000;
|
2016-04-05 00:51:36 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-04-08 10:07:33 +00:00
|
|
|
Thread.Sleep(100);
|
2016-04-05 00:51:36 +00:00
|
|
|
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 string GetLocalhostUrlWithFreePort()
|
|
|
|
{
|
2016-04-05 20:12:07 +00:00
|
|
|
return $"{Localhost}:{PortManager.GetPort()}/";
|
2016-04-05 00:51:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|