Skip to content

Commit

Permalink
Support HTTP headers on HttpWaitStrategy (#2549)
Browse files Browse the repository at this point in the history
This enables webservers that depend on headers for healthchecks to be
able to correctly be queried by the HttpWaitStrategy.

Co-authored-by: Sergei Egorov <bsideup@gmail.com>
  • Loading branch information
renatomefi and bsideup authored Feb 6, 2021
1 parent 1f9b44e commit 51ee33b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.base.Strings;
import com.google.common.io.BaseEncoding;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.rnorth.ducttape.TimeoutException;
import org.testcontainers.containers.ContainerLaunchException;
Expand Down Expand Up @@ -40,6 +42,7 @@ public class HttpWaitStrategy extends AbstractWaitStrategy {
private boolean tlsEnabled;
private String username;
private String password;
private final Map<String, String> headers = new HashMap<>();
private Predicate<String> responsePredicate;
private Predicate<Integer> statusCodePredicate = null;
private Optional<Integer> livenessPort = Optional.empty();
Expand Down Expand Up @@ -122,6 +125,27 @@ public HttpWaitStrategy withBasicCredentials(String username, String password) {
return this;
}

/**
* Add a custom HTTP Header to the call.
* @param name The HTTP Header name
* @param value The HTTP Header value
* @return this
*/
public HttpWaitStrategy withHeader(String name, String value) {
this.headers.put(name, value);
return this;
}

/**
* Add multiple custom HTTP Headers to the call.
* @param headers Headers map of name/value
* @return this
*/
public HttpWaitStrategy withHeaders(Map<String, String> headers) {
this.headers.putAll(headers);
return this;
}

/**
* Set the HTTP connections read timeout.
*
Expand Down Expand Up @@ -191,6 +215,8 @@ protected void waitUntilReady() {
connection.setUseCaches(false);
}

// Add user configured headers
this.headers.forEach(connection::setRequestProperty);
connection.setRequestMethod(method);
connection.connect();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.testcontainers.junit.wait.strategy;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

import java.util.HashMap;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.rnorth.ducttape.RetryCountExceededException;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;

import java.time.Duration;
Expand Down Expand Up @@ -30,6 +35,28 @@ public void testWaitUntilReadyWithSuccess() {
waitUntilReadyAndSucceed(createShellCommand("200 OK", GOOD_RESPONSE_BODY));
}

/**
* Ensures that HTTP requests made with the HttpWaitStrategy can be enriched with user defined headers,
* although the test web server does not depend on the header to response with a 200, by checking the
* logs we can ensure the HTTP request was correctly sent.
*/
@Test
public void testWaitUntilReadyWithSuccessWithCustomHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("baz", "boo");
GenericContainer container = startContainerWithCommand(createShellCommand("200 OK", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready)
.withHeader("foo", "bar")
.withHeaders(headers)
);
waitUntilReadyAndSucceed(container);

String logs = container.getLogs();

assertThat(logs, containsString("foo: bar"));
assertThat(logs, containsString("baz: boo"));
}

/**
* Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.
* This 401 response is checked with a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}
Expand Down

0 comments on commit 51ee33b

Please sign in to comment.