@@ -123,10 +123,15 @@ private ElasticsearchBuilder WithUsername(string username)
123123 private sealed class WaitUntil : IWaitUntil
124124 {
125125 private readonly ElasticsearchConfiguration _configuration ;
126+ private readonly string _credentials ;
126127
127128 public WaitUntil ( ElasticsearchConfiguration configuration )
128129 {
129130 _configuration = configuration ;
131+
132+ var username = _configuration . Username ?? DefaultUsername ;
133+ var password = _configuration . Password ?? DefaultPassword ;
134+ _credentials = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( $ "{ username } :{ password } ") ) ;
130135 }
131136
132137 /// <inheritdoc />
@@ -135,23 +140,32 @@ public async Task<bool> UntilAsync(IContainer container)
135140 using var httpMessageHandler = new HttpClientHandler ( ) ;
136141 httpMessageHandler . ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true ;
137142
138- var creds = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( _configuration . Username + ":" + _configuration . Password ) ) ;
139-
140143 var httpWaitStrategy = new HttpWaitStrategy ( )
141144 . UsingHttpMessageHandler ( httpMessageHandler )
142145 . UsingTls ( _configuration . HttpsEnabled )
143146 . ForPath ( "/_cluster/health" )
144147 . ForPort ( ElasticsearchHttpsPort )
145148 . ForStatusCode ( HttpStatusCode . OK )
146- . WithHeader ( "Authorization" , "Basic " + creds )
149+ . WithHeader ( "Authorization" , "Basic " + _credentials )
147150 . ForResponseMessageMatching ( async ( m ) =>
148151 {
149- var response = await m . Content . ReadAsStringAsync ( ) ;
150- return response . Contains ( "\" status\" :\" yellow\" " ) || response . Contains ( "\" status\" :\" green\" " ) ;
152+ var content = await m . Content . ReadAsStringAsync ( ) ;
153+ var response = JsonSerializer . Deserialize < ElasticHealthResponse > ( content ) ;
154+ return string . Equals ( ElasticHealthResponse . YellowStatus , response . Status , StringComparison . OrdinalIgnoreCase ) ||
155+ string . Equals ( ElasticHealthResponse . GreenStatus , response . Status , StringComparison . OrdinalIgnoreCase ) ;
151156 } ) ;
152157
153158 return await httpWaitStrategy . UntilAsync ( container )
154159 . ConfigureAwait ( false ) ;
155160 }
161+
162+ private class ElasticHealthResponse
163+ {
164+ public const string YellowStatus = "yellow" ;
165+ public const string GreenStatus = "green" ;
166+
167+ [ JsonPropertyName ( "status" ) ]
168+ public string Status { get ; set ; }
169+ }
156170 }
157171}
0 commit comments