65 lines
1.8 KiB
C#
Raw Normal View History

2023-03-21 08:23:15 +01:00
using NUnit.Framework;
2023-03-21 13:20:21 +01:00
namespace CodexDistTestCore
{
public interface IOnlineCodexNode
{
CodexDebugResponse GetDebugInfo();
2023-03-21 08:23:15 +01:00
ContentId UploadFile(TestFile file);
TestFile? DownloadContent(ContentId contentId);
}
public class OnlineCodexNode : IOnlineCodexNode
{
private readonly IFileManager fileManager;
2023-03-21 15:17:48 +01:00
public OnlineCodexNode(IFileManager fileManager, CodexNodeContainer environment)
{
this.fileManager = fileManager;
2023-03-21 15:44:21 +01:00
Container = environment;
2023-03-19 11:40:05 +01:00
}
2023-03-21 15:44:21 +01:00
public CodexNodeContainer Container { get; }
public CodexDebugResponse GetDebugInfo()
{
2023-03-21 08:23:15 +01:00
return Http().HttpGetJson<CodexDebugResponse>("debug/info");
}
2023-03-21 08:23:15 +01:00
public ContentId UploadFile(TestFile file)
{
2023-03-21 08:23:15 +01:00
using var fileStream = File.OpenRead(file.Filename);
var response = Http().HttpPostStream("upload", fileStream);
if (response.StartsWith("Unable to store block"))
{
2023-03-21 08:23:15 +01:00
Assert.Fail("Node failed to store block.");
}
2023-03-21 08:23:15 +01:00
return new ContentId(response);
}
public TestFile? DownloadContent(ContentId contentId)
{
var file = fileManager.CreateEmptyTestFile();
2023-03-21 08:23:15 +01:00
using var fileStream = File.OpenWrite(file.Filename);
using var downloadStream = Http().HttpGetStream("download/" + contentId.Id);
downloadStream.CopyTo(fileStream);
return file;
}
2023-03-21 08:23:15 +01:00
private Http Http()
{
2023-03-21 15:44:21 +01:00
return new Http(ip: "127.0.0.1", port: Container.ServicePort, baseUrl: "/api/codex/v1");
}
}
public class ContentId
{
public ContentId(string id)
{
Id = id;
}
2023-03-21 13:20:21 +01:00
public string Id { get; }
}
}