forked from FubarDevelopment/FtpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFtpServerTestsBase.cs
105 lines (93 loc) · 3.68 KB
/
FtpServerTestsBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// <copyright file="FtpServerTestsBase.cs" company="Fubar Development Junker">
// Copyright (c) Fubar Development Junker. All rights reserved.
// </copyright>
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;
namespace FubarDev.FtpServer.Tests
{
public class FtpServerTestsBase : IAsyncLifetime
{
private readonly ITestOutputHelper _testOutputHelper;
private ServiceProvider? _serviceProvider;
private IFtpServer? _server;
/// <summary>
/// Initializes a new instance of the <see cref="FtpServerTestsBase"/> class.
/// </summary>
/// <param name="testOutputHelper">The test output helper.</param>
protected FtpServerTestsBase(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
/// <summary>
/// Gets the FTP server.
/// </summary>
public IFtpServer Server => _server ?? throw new InvalidOperationException();
/// <summary>
/// Gets the service provider.
/// </summary>
public IServiceProvider ServiceProvider => _serviceProvider ?? throw new InvalidOperationException();
/// <inheritdoc />
public virtual Task InitializeAsync()
{
var services = new ServiceCollection()
.AddLogging(
lb =>
{
// lb.AddConsole();
lb.AddXunit(_testOutputHelper, LogLevel.Trace);
lb.SetMinimumLevel(LogLevel.Trace);
lb.AddFilter("System", LogLevel.Warning);
lb.AddFilter("Microsoft", LogLevel.Warning);
lb.AddFilter("FubarDev.FtpServer", LogLevel.Trace);
})
.AddFtpServer(
opt => Configure(opt));
services = Configure(services);
_serviceProvider = services.BuildServiceProvider(true);
_server = _serviceProvider.GetRequiredService<IFtpServer>();
return _server.StartAsync(default);
}
/// <inheritdoc />
public virtual async Task DisposeAsync()
{
await Server.StopAsync(default).ConfigureAwait(false);
if (_serviceProvider != null)
{
await _serviceProvider.DisposeAsync();
}
}
/// <summary>
/// Basic FTP server configuration for the basic configuration through <see cref="Configure(IFtpServerBuilder)"/>.
/// </summary>
/// <param name="services">The service collection.</param>
/// <returns>The modified service collection.</returns>
protected virtual IServiceCollection Configure(IServiceCollection services)
{
return services
.Configure<FtpServerOptions>(
opt =>
{
// IPv4 localhost
opt.ServerAddress = "127.0.0.1";
// Dynamic port
opt.Port = 0;
});
}
/// <summary>
/// Basic configuration using the FTP server builder.
/// </summary>
/// <param name="builder">The FTP server builder used for the configuration.</param>
/// <returns>The modified FTP server builder.</returns>
protected virtual IFtpServerBuilder Configure(IFtpServerBuilder builder)
{
return builder
.EnableAnonymousAuthentication()
.UseSingleRoot()
.UseInMemoryFileSystem();
}
}
}