@@ -10,46 +10,33 @@ namespace DotNet.Testcontainers.Tests.Unit
1010 using DotNet . Testcontainers . Commons ;
1111 using DotNet . Testcontainers . Configurations ;
1212 using DotNet . Testcontainers . Containers ;
13+ using JetBrains . Annotations ;
1314 using Xunit ;
1415
15- public sealed class WaitUntilHttpRequestIsSucceededTest : IAsyncLifetime
16+ public sealed class WaitUntilHttpRequestIsSucceededTest : IClassFixture < WaitUntilHttpRequestIsSucceededTest . HttpFixture >
1617 {
1718 private const ushort HttpPort = 80 ;
1819
19- private readonly IContainer _container = new ContainerBuilder ( )
20- . WithImage ( CommonImages . Socat )
21- . WithCommand ( "-v" )
22- . WithCommand ( $ "TCP-LISTEN:{ HttpPort } ,crlf,reuseaddr,fork")
23- . WithCommand ( "EXEC:\" echo -e 'HTTP/1.1 200 OK'\n \n \" " )
24- . WithPortBinding ( HttpPort , true )
25- . WithWaitStrategy ( Wait . ForUnixContainer ( ) . UntilHttpRequestIsSucceeded ( request => request ) )
26- . Build ( ) ;
20+ private readonly IContainer _container ;
2721
28- public static TheoryData < HttpWaitStrategy > GetHttpWaitStrategies ( )
22+ public WaitUntilHttpRequestIsSucceededTest ( HttpFixture httpFixture )
2923 {
30- var theoryData = new TheoryData < HttpWaitStrategy > ( ) ;
31- theoryData . Add ( new HttpWaitStrategy ( ) ) ;
32- theoryData . Add ( new HttpWaitStrategy ( ) . ForPort ( HttpPort ) ) ;
33- theoryData . Add ( new HttpWaitStrategy ( ) . ForStatusCode ( HttpStatusCode . OK ) ) ;
34- theoryData . Add ( new HttpWaitStrategy ( ) . ForStatusCodeMatching ( statusCode => HttpStatusCode . OK . Equals ( statusCode ) ) ) ;
35- theoryData . Add ( new HttpWaitStrategy ( ) . ForResponseMessageMatching ( response => Task . FromResult ( response . IsSuccessStatusCode ) ) ) ;
36- theoryData . Add ( new HttpWaitStrategy ( ) . ForStatusCode ( HttpStatusCode . MovedPermanently ) . ForStatusCodeMatching ( statusCode => HttpStatusCode . OK . Equals ( statusCode ) ) ) ;
37- return theoryData ;
24+ _container = httpFixture . Container ;
3825 }
3926
40- public async ValueTask InitializeAsync ( )
41- {
42- await _container . StartAsync ( )
43- . ConfigureAwait ( false ) ;
44- }
45-
46- public ValueTask DisposeAsync ( )
47- {
48- return _container . DisposeAsync ( ) ;
49- }
27+ public static TheoryData < HttpWaitStrategy > HttpWaitStrategies { get ; }
28+ = new TheoryData < HttpWaitStrategy >
29+ {
30+ new HttpWaitStrategy ( ) ,
31+ new HttpWaitStrategy ( ) . ForPort ( HttpPort ) ,
32+ new HttpWaitStrategy ( ) . ForStatusCode ( HttpStatusCode . OK ) ,
33+ new HttpWaitStrategy ( ) . ForStatusCodeMatching ( statusCode => HttpStatusCode . OK . Equals ( statusCode ) ) ,
34+ new HttpWaitStrategy ( ) . ForResponseMessageMatching ( response => Task . FromResult ( response . IsSuccessStatusCode ) ) ,
35+ new HttpWaitStrategy ( ) . ForStatusCode ( HttpStatusCode . MovedPermanently ) . ForStatusCodeMatching ( statusCode => HttpStatusCode . OK . Equals ( statusCode ) ) ,
36+ } ;
5037
5138 [ Theory ]
52- [ MemberData ( nameof ( GetHttpWaitStrategies ) ) ]
39+ [ MemberData ( nameof ( HttpWaitStrategies ) ) ]
5340 public async Task HttpWaitStrategyReceivesStatusCode ( HttpWaitStrategy httpWaitStrategy )
5441 {
5542 var succeeded = await httpWaitStrategy . UntilAsync ( _container )
@@ -134,5 +121,28 @@ await httpWaitStrategy.UntilAsync(_container)
134121 // Then
135122 Assert . Null ( exceptionOnSubsequentCall ) ;
136123 }
124+
125+ [ UsedImplicitly ]
126+ public sealed class HttpFixture : IAsyncLifetime
127+ {
128+ public IContainer Container { get ; } = new ContainerBuilder ( )
129+ . WithImage ( CommonImages . Socat )
130+ . WithCommand ( "-v" )
131+ . WithCommand ( $ "TCP-LISTEN:{ HttpPort } ,crlf,reuseaddr,fork")
132+ . WithCommand ( "SYSTEM:'echo -e \" HTTP/1.1 200 OK\\ nContent-Length: 0\\ n\\ n\" '" )
133+ . WithPortBinding ( HttpPort , true )
134+ . WithWaitStrategy ( Wait . ForUnixContainer ( ) . UntilHttpRequestIsSucceeded ( request => request ) )
135+ . Build ( ) ;
136+
137+ public ValueTask InitializeAsync ( )
138+ {
139+ return new ValueTask ( Container . StartAsync ( ) ) ;
140+ }
141+
142+ public ValueTask DisposeAsync ( )
143+ {
144+ return Container . DisposeAsync ( ) ;
145+ }
146+ }
137147 }
138148}
0 commit comments