btcpayserver/BTCPayServer.Tests/TestUtils.cs

140 lines
4.5 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
2019-05-09 10:21:51 +02:00
using System.IO;
2020-06-28 10:55:27 +02:00
using System.Linq;
using System.Net.Http;
2019-05-09 10:21:51 +02:00
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using NBitcoin;
2021-11-11 13:03:08 +01:00
using OpenQA.Selenium;
using Xunit;
2020-06-28 10:55:27 +02:00
using Xunit.Sdk;
2019-05-09 10:21:51 +02:00
namespace BTCPayServer.Tests
{
public static class TestUtils
{
2019-10-31 07:10:00 +01:00
#if DEBUG && !SHORT_TIMEOUT
public const int TestTimeout = 600_000;
2019-10-08 08:21:30 +02:00
#else
public const int TestTimeout = 90_000;
2019-10-08 08:21:30 +02:00
#endif
public static DirectoryInfo TryGetSolutionDirectoryInfo()
{
var directory = new DirectoryInfo(TestDirectory);
while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
}
return directory;
}
2019-11-08 08:10:49 +01:00
static TestUtils()
{
TestDirectory = ((OutputPathAttribute)typeof(TestUtils).Assembly.GetCustomAttributes(typeof(OutputPathAttribute), true)[0]).BuiltPath;
}
public readonly static string TestDirectory;
2019-11-08 08:10:49 +01:00
public static string GetTestDataFullPath(string relativeFilePath)
{
var directory = new DirectoryInfo(TestDirectory);
2019-11-08 08:10:49 +01:00
while (directory != null && !directory.GetFiles("*.csproj").Any())
{
directory = directory.Parent;
}
return Path.Combine(directory.FullName, "TestData", relativeFilePath);
}
public static T AssertType<T>(this object obj)
{
Assert.IsType<T>(obj);
return (T)obj;
}
2020-06-28 10:55:27 +02:00
2019-05-09 10:21:51 +02:00
public static FormFile GetFormFile(string filename, string content)
{
File.WriteAllText(filename, content);
var fileInfo = new FileInfo(filename);
FormFile formFile = new FormFile(
new FileStream(filename, FileMode.OpenOrCreate),
0,
fileInfo.Length, fileInfo.Name, fileInfo.Name)
{
Headers = new HeaderDictionary()
};
formFile.ContentType = "text/plain";
formFile.ContentDisposition = $"form-data; name=\"file\"; filename=\"{fileInfo.Name}\"";
2019-05-12 07:51:24 +02:00
return formFile;
}
public static FormFile GetFormFile(string filename, byte[] content)
{
File.WriteAllBytes(filename, content);
var fileInfo = new FileInfo(filename);
FormFile formFile = new FormFile(
new FileStream(filename, FileMode.OpenOrCreate),
0,
fileInfo.Length, fileInfo.Name, fileInfo.Name)
{
Headers = new HeaderDictionary()
};
formFile.ContentType = "application/octet-stream";
formFile.ContentDisposition = $"form-data; name=\"file\"; filename=\"{fileInfo.Name}\"";
2019-05-09 10:21:51 +02:00
return formFile;
}
2019-10-06 11:47:49 +02:00
public static void Eventually(Action act, int ms = 20_000)
2019-05-09 10:21:51 +02:00
{
CancellationTokenSource cts = new CancellationTokenSource(ms);
2019-05-09 10:21:51 +02:00
while (true)
{
try
{
act();
break;
}
2021-11-11 13:03:08 +01:00
catch (WebDriverException) when (!cts.Token.IsCancellationRequested)
{
cts.Token.WaitHandle.WaitOne(500);
}
2019-05-09 10:21:51 +02:00
catch (XunitException) when (!cts.Token.IsCancellationRequested)
{
cts.Token.WaitHandle.WaitOne(500);
}
}
}
public static async Task EventuallyAsync(Func<Task> act, int delay = 20000)
2019-05-09 10:21:51 +02:00
{
CancellationTokenSource cts = new CancellationTokenSource(delay);
2019-05-09 10:21:51 +02:00
while (true)
{
try
{
await act();
break;
}
catch (XunitException) when (!cts.Token.IsCancellationRequested)
{
bool timeout =false;
try
{
await Task.Delay(500, cts.Token);
}
catch { timeout = true; }
if (timeout)
throw;
2019-05-09 10:21:51 +02:00
}
}
}
internal static IHttpClientFactory CreateHttpFactory()
{
var services = new ServiceCollection();
services.AddHttpClient();
return services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>();
}
2019-05-09 10:21:51 +02:00
}
}