Skip to content

Commit 556fd41

Browse files
authored
fix: Do not dispose a custom HTTP message handler in an HTTP wait strategy (#958)
1 parent ad14e24 commit 556fd41

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Testcontainers/Configurations/WaitStrategies/HttpWaitStrategy.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task<bool> UntilAsync(IContainer container)
6666
return false;
6767
}
6868

69-
using (var httpClient = new HttpClient(_httpMessageHandler ?? new HttpClientHandler()))
69+
using (var httpClient = new HttpClient(_httpMessageHandler ?? new HttpClientHandler(), disposeHandler: _httpMessageHandler == null))
7070
{
7171
using (var httpRequestMessage = new HttpRequestMessage(_httpMethod, new UriBuilder(_schemeName, host, port, _pathValue).Uri))
7272
{
@@ -82,7 +82,7 @@ public async Task<bool> UntilAsync(IContainer container)
8282
httpResponseMessage = await httpClient.SendAsync(httpRequestMessage)
8383
.ConfigureAwait(false);
8484
}
85-
catch
85+
catch (HttpRequestException)
8686
{
8787
return false;
8888
}

tests/Testcontainers.Tests/Unit/Configurations/WaitUntilHttpRequestIsSucceededTest.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class WaitUntilHttpRequestIsSucceededTest : IAsyncLifetime
1919
private readonly IContainer _container = new ContainerBuilder()
2020
.WithImage(CommonImages.Alpine)
2121
.WithEntrypoint("/bin/sh", "-c")
22-
.WithCommand($"echo \"HTTP/1.1 200 OK\r\n\" | nc -l -p {HttpPort}")
22+
.WithCommand($"while true; do echo \"HTTP/1.1 200 OK\r\n\" | nc -l -p {HttpPort}; done")
2323
.WithPortBinding(HttpPort, true)
2424
.Build();
2525

@@ -110,5 +110,22 @@ await Task.Delay(TimeSpan.FromSeconds(1))
110110
Assert.Contains("Cookie", stdout);
111111
Assert.Contains("Key1=Value1", stdout);
112112
}
113+
114+
[Fact]
115+
public async Task HttpWaitStrategyCanReuseCustomHttpClientHandler()
116+
{
117+
// Given
118+
var httpWaitStrategy = new HttpWaitStrategy().UsingHttpMessageHandler(new HttpClientHandler());
119+
120+
// When
121+
await httpWaitStrategy.UntilAsync(_container)
122+
.ConfigureAwait(false);
123+
124+
var exceptionOnSubsequentCall = await Record.ExceptionAsync(() => httpWaitStrategy.UntilAsync(_container))
125+
.ConfigureAwait(false);
126+
127+
// Then
128+
Assert.Null(exceptionOnSubsequentCall);
129+
}
113130
}
114131
}

0 commit comments

Comments
 (0)