Skip to content

Commit a167980

Browse files
committed
Add new assertion for response headers
Add a new assertion to synchronously execute a request and check the response contains a specific warning header
1 parent fd6ecbf commit a167980

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import org.elasticsearch.ElasticsearchException;
1515
import org.elasticsearch.ExceptionsHelper;
1616
import org.elasticsearch.action.ActionFuture;
17+
import org.elasticsearch.action.ActionListener;
1718
import org.elasticsearch.action.ActionRequest;
19+
import org.elasticsearch.action.ActionRequestBuilder;
1820
import org.elasticsearch.action.ActionResponse;
1921
import org.elasticsearch.action.RequestBuilder;
2022
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequestBuilder;
@@ -873,6 +875,74 @@ public static void awaitLatch(CountDownLatch latch, long timeout, TimeUnit unit)
873875
assertThat(message, isCountedDown, is(true));
874876
}
875877

878+
/**
879+
* Check the response of a client request to ensure it has a warning header that contains the provided string
880+
*
881+
* Currently, this allows a fixed 10 seconds for response to be received
882+
* @param client Client making the request - Required to access the response headers
883+
* @param requestBuilder Request to be made
884+
* @param toMatch String to match in the warning headers
885+
* @param <T> Type of the response
886+
* @throws InterruptedException If the request times out
887+
*/
888+
public static <T extends ActionResponse> void assertWarningHeaderOnResponse(
889+
Client client,
890+
ActionRequestBuilder<?, T> requestBuilder,
891+
String toMatch
892+
) throws InterruptedException {
893+
assertWarningHeaderMatchOnResponse(client, requestBuilder, hasItem(containsString(toMatch)));
894+
}
895+
896+
/**
897+
* Check the response of a client request to ensure it does not have a warning header that contains the provided string
898+
*
899+
* Currently, this allows a fixed 10 seconds for response to be received
900+
* @param client Client making the request - Required to access the response headers
901+
* @param requestBuilder Request to be made
902+
* @param toMatch String to not match in the warning headers
903+
* @param <T> Type of the response
904+
* @throws InterruptedException If the request times out
905+
*/
906+
public static <T extends ActionResponse> void assertNoWarningHeaderOnResponse(
907+
Client client,
908+
ActionRequestBuilder<?, T> requestBuilder,
909+
String toMatch
910+
) throws InterruptedException {
911+
assertWarningHeaderMatchOnResponse(client, requestBuilder, not(hasItem(containsString(toMatch))));
912+
}
913+
914+
private static <T extends ActionResponse> void assertWarningHeaderMatchOnResponse(
915+
Client client,
916+
ActionRequestBuilder<?, T> requestBuilder,
917+
Matcher<? super List<String>> matcher
918+
) throws InterruptedException {
919+
CountDownLatch latch = new CountDownLatch(2);
920+
requestBuilder.execute(new ActionListener<>() {
921+
@Override
922+
public void onResponse(T response) {
923+
try {
924+
final var warningHeaders = client.threadPool().getThreadContext().getResponseHeaders().get("Warning");
925+
assertThat(warningHeaders, matcher);
926+
} finally {
927+
latch.countDown();
928+
}
929+
}
930+
931+
@Override
932+
public void onFailure(Exception e) {
933+
try {
934+
throw new AssertionError("Failed to execute request", e);
935+
} finally {
936+
latch.countDown();
937+
}
938+
}
939+
});
940+
latch.countDown();
941+
if (latch.await(10, TimeUnit.SECONDS) == false) {
942+
fail("Did not receive request response before timeout");
943+
}
944+
}
945+
876946
/**
877947
* Compares two maps recursively, using arrays comparisons for byte[] through Arrays.equals(byte[], byte[])
878948
*/

0 commit comments

Comments
 (0)