Closed w1am closed 1 month ago
After further investigation, I discovered that FluentDocker already has a solution for this scenario that I wasn't aware of initially. There's a method called Wait
that allows for custom wait conditions. This method can be used to implement waiting with basic authentication. Here's an example of how I solved my problem:
public static class FluentDockerExtensions {
public static CompositeBuilder WaitForHttp(
this CompositeBuilder builder,
string serviceName,
string url,
int timeout,
string username,
string password
) {
return builder.Wait(
serviceName,
(service, attempt) => {
var client = new HttpClient();
var authToken = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authToken);
try {
var response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode) {
Console.WriteLine($"Successfully connected to {url} on attempt {attempt}");
return 0;
}
} catch (Exception ex) {
Console.WriteLine($"Attempt {attempt} failed to connect to {url}: {ex.Message}");
}
if (attempt >= timeout / 1000) {
Console.WriteLine($"Max attempts reached. Failed to connect to {url}.");
return -1;
}
Thread.Sleep(1000);
return 1;
}
);
}
}
I'm using FluentDocker in my test setup and need to wait for the Kafka Schema Registry to be ready before proceeding. The Schema Registry service requires Basic Authentication. When I try to pass the credentials in the URL using the
WaitForHttp
method, I get a401 Unauthorized
error.Here’s a snippet of my code:
In the console, I see the following error message:
However, using the
curl
command with the same credentials works perfectly:The response from the server indicates that the credentials are correctly accepted:
Question:
Is there a way to pass Basic Authentication credentials in the
WaitForHttp
method within FluentDocker? If not, would it be possible to add support for this feature?