1313namespace FubarDev . FtpServer . Tests
1414{
1515 /// <summary>
16- /// Fixture that initializes an FTP server
16+ /// Fixture that initializes an FTP server.
1717 /// </summary>
1818 public class FtpServerFixture : IAsyncLifetime
1919 {
20- private readonly IServiceProvider _serviceProvider ;
20+ private ServiceProvider ? _serviceProvider ;
2121 private IFtpServer ? _server ;
2222
2323 /// <summary>
24- /// Initializes a new instance of the <see cref="FtpServerFixture"/> class .
24+ /// Gets the FTP server .
2525 /// </summary>
26- public FtpServerFixture ( )
26+ public IFtpServer Server => _server ?? throw new InvalidOperationException ( ) ;
27+
28+ /// <inheritdoc />
29+ public Task InitializeAsync ( )
2730 {
2831 var services = new ServiceCollection ( )
2932 . AddLogging (
@@ -36,33 +39,50 @@ public FtpServerFixture()
3639 lb . AddFilter ( "FubarDev.FtpServer" , LogLevel . Trace ) ;
3740 } )
3841 . AddFtpServer (
39- opt => opt . EnableAnonymousAuthentication ( )
40- . UseSingleRoot ( )
41- . UseInMemoryFileSystem ( ) )
42- . Configure < FtpServerOptions > ( opt =>
43- {
44- // IPv4 localhost
45- opt . ServerAddress = "127.0.0.1" ;
46-
47- // Dynamic port
48- opt . Port = 0 ;
49- } ) ;
42+ opt => Configure ( opt ) ) ;
43+ services = Configure ( services ) ;
5044 _serviceProvider = services . BuildServiceProvider ( true ) ;
45+ _server = _serviceProvider . GetRequiredService < IFtpServer > ( ) ;
46+ return _server . StartAsync ( default ) ;
5147 }
5248
53- public IFtpServer Server => _server ?? throw new InvalidOperationException ( ) ;
54-
5549 /// <inheritdoc />
56- public Task InitializeAsync ( )
50+ public async Task DisposeAsync ( )
5751 {
58- _server = _serviceProvider . GetRequiredService < IFtpServer > ( ) ;
59- return _server . StartAsync ( default ) ;
52+ await Server . StopAsync ( default ) . ConfigureAwait ( false ) ;
53+ _serviceProvider ? . Dispose ( ) ;
6054 }
6155
62- /// <inheritdoc />
63- public Task DisposeAsync ( )
56+ /// <summary>
57+ /// Basic FTP server configuration for the basic configuration through <see cref="Configure(IFtpServerBuilder)"/>.
58+ /// </summary>
59+ /// <param name="services">The service collection.</param>
60+ /// <returns>The modified service collection.</returns>
61+ protected virtual IServiceCollection Configure ( IServiceCollection services )
62+ {
63+ return services
64+ . Configure < FtpServerOptions > (
65+ opt =>
66+ {
67+ // IPv4 localhost
68+ opt . ServerAddress = "127.0.0.1" ;
69+
70+ // Dynamic port
71+ opt . Port = 0 ;
72+ } ) ;
73+ }
74+
75+ /// <summary>
76+ /// Basic configuration using the FTP server builder.
77+ /// </summary>
78+ /// <param name="builder">The FTP server builder used for the configuration.</param>
79+ /// <returns>The modified FTP server builder.</returns>
80+ protected virtual IFtpServerBuilder Configure ( IFtpServerBuilder builder )
6481 {
65- return Server . StopAsync ( default ) ;
82+ return builder
83+ . EnableAnonymousAuthentication ( )
84+ . UseSingleRoot ( )
85+ . UseInMemoryFileSystem ( ) ;
6686 }
6787 }
6888}
0 commit comments