Files
tgstation-server/tests/Tgstation.Server.Host.Tests/IO/TestFileDownloader.cs
T
Dominion e596110018 Clean up Tgstation.Server.Common
- Move classes to `Http` namespace.
- Give it a unique package version.
- Comment to make me not rage update Nuget libs to .NET 6.
2023-06-10 17:04:04 -04:00

100 lines
2.7 KiB
C#

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Tgstation.Server.Common.Http;
using Tgstation.Server.Host.System;
namespace Tgstation.Server.Host.IO.Tests
{
[TestClass]
public sealed class TestFileDownloader
{
const string ExpectedData = @"Going forward, the .NET team is using https://github.com/dotnet/runtime to
develop the code and issues formerly in this repository.
Please see the following for more context:
[dotnet/announcements#119 ""Consolidating .NET GitHub repos""](https://github.com/dotnet/announcements/issues/119)";
[TestMethod]
public void TestConstructor()
{
Assert.ThrowsException<ArgumentNullException>(() => new FileDownloader(null, null));
Assert.ThrowsException<ArgumentNullException>(() => new FileDownloader(Mock.Of<IAbstractHttpClientFactory>(), null));
_ = new FileDownloader(Mock.Of<IAbstractHttpClientFactory>(), Mock.Of<ILogger<FileDownloader>>());
}
[TestMethod]
public async Task TestDownloadWithoutToken()
{
await RunTest(null);
}
[TestMethod]
public async Task TestDownloadWithToken()
{
var gitHubToken = Environment.GetEnvironmentVariable("TGS_TEST_GITHUB_TOKEN");
if (String.IsNullOrWhiteSpace(gitHubToken))
Assert.Inconclusive("TGS_TEST_GITHUB_TOKEN not set");
await RunTest(gitHubToken);
}
[TestMethod]
public void TestDownloadThrows()
{
var downloader = CreateDownloader(out var loggerFactory);
using (loggerFactory)
{
Assert.ThrowsException<ArgumentNullException>(() => downloader.DownloadFile(null, null));
}
}
static FileDownloader CreateDownloader(out ILoggerFactory loggerFactory)
{
loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Trace);
});
try
{
return new FileDownloader(
new HttpClientFactory(
new AssemblyInformationProvider().ProductInfoHeaderValue),
loggerFactory.CreateLogger<FileDownloader>());
}
catch
{
loggerFactory.Dispose();
throw;
}
}
static async Task RunTest(string gitHubToken)
{
var downloader = CreateDownloader(out var loggerFactory);
using (loggerFactory)
{
await using var ms = new MemoryStream();
await using (var provider = downloader.DownloadFile(
new Uri("https://raw.githubusercontent.com/dotnet/corefx/archive/README.md"),
gitHubToken))
await using (var s = await provider.GetResult(default))
await s.CopyToAsync(ms);
var stringData = Encoding.UTF8.GetString(ms.GetBuffer());
Assert.AreEqual(ExpectedData, stringData);
}
}
}
}