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;
|
2020-01-18 11:23:40 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-11-29 10:51:40 +01:00
|
|
|
using NBitcoin;
|
2021-11-11 13:03:08 +01:00
|
|
|
using OpenQA.Selenium;
|
2020-04-05 13:00:28 +02:00
|
|
|
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
|
2024-09-25 11:23:10 +02:00
|
|
|
public const int TestTimeout = 600_000;
|
2019-10-08 08:21:30 +02:00
|
|
|
#else
|
2023-04-05 01:35:50 +02:00
|
|
|
public const int TestTimeout = 90_000;
|
2019-10-08 08:21:30 +02:00
|
|
|
#endif
|
2024-10-03 09:04:16 +02:00
|
|
|
public static DirectoryInfo TryGetSolutionDirectoryInfo()
|
2019-10-06 16:38:57 +02:00
|
|
|
{
|
2024-10-03 09:04:16 +02:00
|
|
|
var directory = new DirectoryInfo(TestDirectory);
|
2019-10-06 16:38:57 +02:00
|
|
|
while (directory != null && !directory.GetFiles("*.sln").Any())
|
|
|
|
{
|
|
|
|
directory = directory.Parent;
|
|
|
|
}
|
|
|
|
return directory;
|
|
|
|
}
|
2019-11-08 08:10:49 +01:00
|
|
|
|
2024-10-03 09:04:16 +02: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)
|
|
|
|
{
|
2024-10-03 09:04:16 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-04-05 13:00:28 +02:00
|
|
|
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
|
|
|
{
|
2019-09-02 15:37:52 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 12:34:36 +02:00
|
|
|
public static async Task EventuallyAsync(Func<Task> act, int delay = 20000)
|
2019-05-09 10:21:51 +02:00
|
|
|
{
|
2021-08-23 12:34:36 +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)
|
|
|
|
{
|
2023-04-27 03:59:19 +02:00
|
|
|
bool timeout =false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await Task.Delay(500, cts.Token);
|
|
|
|
}
|
|
|
|
catch { timeout = true; }
|
|
|
|
if (timeout)
|
|
|
|
throw;
|
2019-05-09 10:21:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 11:23:40 +01:00
|
|
|
|
|
|
|
internal static IHttpClientFactory CreateHttpFactory()
|
|
|
|
{
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
services.AddHttpClient();
|
|
|
|
return services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>();
|
|
|
|
}
|
2019-05-09 10:21:51 +02:00
|
|
|
}
|
|
|
|
}
|