Skip to content

Commit 51ee33b

Browse files
renatomefibsideup
andauthored
Support HTTP headers on HttpWaitStrategy (#2549)
This enables webservers that depend on headers for healthchecks to be able to correctly be queried by the HttpWaitStrategy. Co-authored-by: Sergei Egorov <[email protected]>
1 parent 1f9b44e commit 51ee33b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

core/src/main/java/org/testcontainers/containers/wait/strategy/HttpWaitStrategy.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.base.Strings;
44
import com.google.common.io.BaseEncoding;
5+
import java.util.HashMap;
6+
import java.util.Map;
57
import lombok.extern.slf4j.Slf4j;
68
import org.rnorth.ducttape.TimeoutException;
79
import org.testcontainers.containers.ContainerLaunchException;
@@ -40,6 +42,7 @@ public class HttpWaitStrategy extends AbstractWaitStrategy {
4042
private boolean tlsEnabled;
4143
private String username;
4244
private String password;
45+
private final Map<String, String> headers = new HashMap<>();
4346
private Predicate<String> responsePredicate;
4447
private Predicate<Integer> statusCodePredicate = null;
4548
private Optional<Integer> livenessPort = Optional.empty();
@@ -122,6 +125,27 @@ public HttpWaitStrategy withBasicCredentials(String username, String password) {
122125
return this;
123126
}
124127

128+
/**
129+
* Add a custom HTTP Header to the call.
130+
* @param name The HTTP Header name
131+
* @param value The HTTP Header value
132+
* @return this
133+
*/
134+
public HttpWaitStrategy withHeader(String name, String value) {
135+
this.headers.put(name, value);
136+
return this;
137+
}
138+
139+
/**
140+
* Add multiple custom HTTP Headers to the call.
141+
* @param headers Headers map of name/value
142+
* @return this
143+
*/
144+
public HttpWaitStrategy withHeaders(Map<String, String> headers) {
145+
this.headers.putAll(headers);
146+
return this;
147+
}
148+
125149
/**
126150
* Set the HTTP connections read timeout.
127151
*
@@ -191,6 +215,8 @@ protected void waitUntilReady() {
191215
connection.setUseCaches(false);
192216
}
193217

218+
// Add user configured headers
219+
this.headers.forEach(connection::setRequestProperty);
194220
connection.setRequestMethod(method);
195221
connection.connect();
196222

core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package org.testcontainers.junit.wait.strategy;
22

3+
import static org.hamcrest.CoreMatchers.containsString;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.HashMap;
37
import org.jetbrains.annotations.NotNull;
48
import org.junit.Test;
59
import org.rnorth.ducttape.RetryCountExceededException;
10+
import org.testcontainers.containers.GenericContainer;
611
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
712

813
import java.time.Duration;
@@ -30,6 +35,28 @@ public void testWaitUntilReadyWithSuccess() {
3035
waitUntilReadyAndSucceed(createShellCommand("200 OK", GOOD_RESPONSE_BODY));
3136
}
3237

38+
/**
39+
* Ensures that HTTP requests made with the HttpWaitStrategy can be enriched with user defined headers,
40+
* although the test web server does not depend on the header to response with a 200, by checking the
41+
* logs we can ensure the HTTP request was correctly sent.
42+
*/
43+
@Test
44+
public void testWaitUntilReadyWithSuccessWithCustomHeaders() {
45+
HashMap<String, String> headers = new HashMap<>();
46+
headers.put("baz", "boo");
47+
GenericContainer container = startContainerWithCommand(createShellCommand("200 OK", GOOD_RESPONSE_BODY),
48+
createHttpWaitStrategy(ready)
49+
.withHeader("foo", "bar")
50+
.withHeaders(headers)
51+
);
52+
waitUntilReadyAndSucceed(container);
53+
54+
String logs = container.getLogs();
55+
56+
assertThat(logs, containsString("foo: bar"));
57+
assertThat(logs, containsString("baz: boo"));
58+
}
59+
3360
/**
3461
* Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.
3562
* This 401 response is checked with a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}

0 commit comments

Comments
 (0)