Skip to content

Commit

Permalink
fix: Do not dispose a custom HTTP message handler in an HTTP wait str…
Browse files Browse the repository at this point in the history
…ategy (#958)
  • Loading branch information
jacobjmarks authored Jul 29, 2023
1 parent ad14e24 commit 556fd41
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<bool> UntilAsync(IContainer container)
return false;
}

using (var httpClient = new HttpClient(_httpMessageHandler ?? new HttpClientHandler()))
using (var httpClient = new HttpClient(_httpMessageHandler ?? new HttpClientHandler(), disposeHandler: _httpMessageHandler == null))
{
using (var httpRequestMessage = new HttpRequestMessage(_httpMethod, new UriBuilder(_schemeName, host, port, _pathValue).Uri))
{
Expand All @@ -82,7 +82,7 @@ public async Task<bool> UntilAsync(IContainer container)
httpResponseMessage = await httpClient.SendAsync(httpRequestMessage)
.ConfigureAwait(false);
}
catch
catch (HttpRequestException)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class WaitUntilHttpRequestIsSucceededTest : IAsyncLifetime
private readonly IContainer _container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint("/bin/sh", "-c")
.WithCommand($"echo \"HTTP/1.1 200 OK\r\n\" | nc -l -p {HttpPort}")
.WithCommand($"while true; do echo \"HTTP/1.1 200 OK\r\n\" | nc -l -p {HttpPort}; done")
.WithPortBinding(HttpPort, true)
.Build();

Expand Down Expand Up @@ -110,5 +110,22 @@ await Task.Delay(TimeSpan.FromSeconds(1))
Assert.Contains("Cookie", stdout);
Assert.Contains("Key1=Value1", stdout);
}

[Fact]
public async Task HttpWaitStrategyCanReuseCustomHttpClientHandler()
{
// Given
var httpWaitStrategy = new HttpWaitStrategy().UsingHttpMessageHandler(new HttpClientHandler());

// When
await httpWaitStrategy.UntilAsync(_container)
.ConfigureAwait(false);

var exceptionOnSubsequentCall = await Record.ExceptionAsync(() => httpWaitStrategy.UntilAsync(_container))
.ConfigureAwait(false);

// Then
Assert.Null(exceptionOnSubsequentCall);
}
}
}

0 comments on commit 556fd41

Please sign in to comment.