2019-05-09 10:21:51 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Xunit.Sdk;
|
2019-10-06 16:38:57 +02:00
|
|
|
|
using System.Linq;
|
2020-01-18 11:23:40 +01:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
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
|
2019-10-08 08:21:30 +02:00
|
|
|
|
public const int TestTimeout = 600_000;
|
|
|
|
|
#else
|
|
|
|
|
public const int TestTimeout = 60_000;
|
|
|
|
|
#endif
|
2019-10-06 16:38:57 +02:00
|
|
|
|
public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = null)
|
|
|
|
|
{
|
|
|
|
|
var directory = new DirectoryInfo(
|
|
|
|
|
currentPath ?? Directory.GetCurrentDirectory());
|
|
|
|
|
while (directory != null && !directory.GetFiles("*.sln").Any())
|
|
|
|
|
{
|
|
|
|
|
directory = directory.Parent;
|
|
|
|
|
}
|
|
|
|
|
return directory;
|
|
|
|
|
}
|
2019-11-08 08:10:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetTestDataFullPath(string relativeFilePath)
|
|
|
|
|
{
|
|
|
|
|
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
|
|
|
|
|
while (directory != null && !directory.GetFiles("*.csproj").Any())
|
|
|
|
|
{
|
|
|
|
|
directory = directory.Parent;
|
|
|
|
|
}
|
|
|
|
|
return Path.Combine(directory.FullName, "TestData", relativeFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
catch (XunitException) when (!cts.Token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cts.Token.WaitHandle.WaitOne(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task EventuallyAsync(Func<Task> act)
|
|
|
|
|
{
|
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource(20000);
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await act();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (XunitException) when (!cts.Token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|