Skip to content

Commit c88a73e

Browse files
authored
fix: Send valid HTTP test responses (#1505)
1 parent 6c5b160 commit c88a73e

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

tests/Testcontainers.Commons/CommonImages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class CommonImages
99

1010
public static readonly IImage Alpine = new DockerImage("alpine:3.17");
1111

12-
public static readonly IImage Socat = new DockerImage("alpine/socat:1.8.0.0");
12+
public static readonly IImage Socat = new DockerImage("alpine/socat:1.8.0.3");
1313

1414
public static readonly IImage Curl = new DockerImage("curlimages/curl:8.00.1");
1515

tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ await container.CopyAsync(_testFile.Directory!.FullName, targetDirectoryPath5, c
170170
Assert.All(execResults, result => Assert.Equal(0, result.ExitCode));
171171
}
172172

173+
[UsedImplicitly]
173174
public sealed class HttpFixture : IAsyncLifetime
174175
{
175176
private const ushort HttpPort = 80;
@@ -178,18 +179,17 @@ public sealed class HttpFixture : IAsyncLifetime
178179
.WithImage(CommonImages.Socat)
179180
.WithCommand("-v")
180181
.WithCommand($"TCP-LISTEN:{HttpPort},crlf,reuseaddr,fork")
181-
.WithCommand("EXEC:\"echo -e 'HTTP/1.1 200 OK'\n\n\"")
182+
.WithCommand("SYSTEM:'echo -e \"HTTP/1.1 200 OK\\nContent-Length: 0\\n\\n\"'")
182183
.WithPortBinding(HttpPort, true)
183184
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request))
184185
.Build();
185186

186187
public string BaseAddress
187188
=> new UriBuilder(Uri.UriSchemeHttp, _container.Hostname, _container.GetMappedPublicPort(HttpPort)).ToString();
188189

189-
public async ValueTask InitializeAsync()
190+
public ValueTask InitializeAsync()
190191
{
191-
await _container.StartAsync()
192-
.ConfigureAwait(false);
192+
return new ValueTask(_container.StartAsync());
193193
}
194194

195195
public ValueTask DisposeAsync()

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

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)