|
14 | 14 | import org.elasticsearch.ElasticsearchException; |
15 | 15 | import org.elasticsearch.ExceptionsHelper; |
16 | 16 | import org.elasticsearch.action.ActionFuture; |
| 17 | +import org.elasticsearch.action.ActionListener; |
17 | 18 | import org.elasticsearch.action.ActionRequest; |
| 19 | +import org.elasticsearch.action.ActionRequestBuilder; |
18 | 20 | import org.elasticsearch.action.ActionResponse; |
19 | 21 | import org.elasticsearch.action.RequestBuilder; |
20 | 22 | import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequestBuilder; |
@@ -873,6 +875,74 @@ public static void awaitLatch(CountDownLatch latch, long timeout, TimeUnit unit) |
873 | 875 | assertThat(message, isCountedDown, is(true)); |
874 | 876 | } |
875 | 877 |
|
| 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 | + |
876 | 946 | /** |
877 | 947 | * Compares two maps recursively, using arrays comparisons for byte[] through Arrays.equals(byte[], byte[]) |
878 | 948 | */ |
|
0 commit comments