[TEST] Expose ability to provide http headers when sending requests in our REST tests

ElasticsearchRestTests has now a `restClientSettings` method that can be overriden to provide headers as settings (similarly to what we do with transport client). Those headers will be sent together with every REST requests within the tests.

Closes #7710
This commit is contained in:
javanna 2014-09-12 17:11:35 +02:00 committed by Luca Cavanna
parent f96bfd3773
commit ec5ceecb97
5 changed files with 33 additions and 8 deletions

View File

@ -53,9 +53,12 @@ public class Headers {
return message; return message;
} }
public Settings headers() {
return headers;
}
static Settings resolveHeaders(Settings settings) { static Settings resolveHeaders(Settings settings) {
Settings headers = settings.getAsSettings(PREFIX); Settings headers = settings.getAsSettings(PREFIX);
return headers != null ? headers : ImmutableSettings.EMPTY; return headers != null ? headers : ImmutableSettings.EMPTY;
} }
} }

View File

@ -24,6 +24,8 @@ import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.test.rest.client.RestException; import org.elasticsearch.test.rest.client.RestException;
@ -173,6 +175,13 @@ public class ElasticsearchRestTests extends ElasticsearchIntegrationTest {
restTestExecutionContext = null; restTestExecutionContext = null;
} }
/**
* Used to obtain settings for the REST client that is used to send REST requests.
*/
protected Settings restClientSettings() {
return ImmutableSettings.EMPTY;
}
@Before @Before
public void reset() throws IOException, RestException { public void reset() throws IOException, RestException {
//skip test if it matches one of the blacklist globs //skip test if it matches one of the blacklist globs
@ -183,7 +192,7 @@ public class ElasticsearchRestTests extends ElasticsearchIntegrationTest {
assumeFalse("[" + testCandidate.getTestPath() + "] skipped, reason: blacklisted", blacklistedPathMatcher.matches(Paths.get(testPath))); assumeFalse("[" + testCandidate.getTestPath() + "] skipped, reason: blacklisted", blacklistedPathMatcher.matches(Paths.get(testPath)));
} }
restTestExecutionContext.resetClient(cluster().httpAddresses()); restTestExecutionContext.resetClient(cluster().httpAddresses(), restClientSettings());
restTestExecutionContext.clear(); restTestExecutionContext.clear();
//skip test if the whole suite (yaml file) is disabled //skip test if the whole suite (yaml file) is disabled

View File

@ -21,6 +21,7 @@ package org.elasticsearch.test.rest;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.rest.client.RestClient; import org.elasticsearch.test.rest.client.RestClient;
import org.elasticsearch.test.rest.client.RestException; import org.elasticsearch.test.rest.client.RestException;
@ -115,11 +116,11 @@ public class RestTestExecutionContext implements Closeable {
} }
/** /**
* Recreates the embedded REST client which will point to the given addresses * Resets (or creates) the embedded REST client which will point to the given addresses
*/ */
public void resetClient(InetSocketAddress[] addresses) throws IOException, RestException { public void resetClient(InetSocketAddress[] addresses, Settings settings) throws IOException, RestException {
if (restClient == null) { if (restClient == null) {
restClient = new RestClient(addresses, restSpec); restClient = new RestClient(restSpec, settings, addresses);
} else { } else {
restClient.updateAddresses(addresses); restClient.updateAddresses(addresses);
} }

View File

@ -23,9 +23,11 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.rest.client.http.HttpRequestBuilder; import org.elasticsearch.test.rest.client.http.HttpRequestBuilder;
import org.elasticsearch.test.rest.client.http.HttpResponse; import org.elasticsearch.test.rest.client.http.HttpResponse;
import org.elasticsearch.test.rest.spec.RestApi; import org.elasticsearch.test.rest.spec.RestApi;
@ -47,14 +49,16 @@ public class RestClient implements Closeable {
private final RestSpec restSpec; private final RestSpec restSpec;
private final CloseableHttpClient httpClient; private final CloseableHttpClient httpClient;
private final Headers headers;
private InetSocketAddress[] addresses; private InetSocketAddress[] addresses;
private final String esVersion; private final String esVersion;
public RestClient(InetSocketAddress[] addresses, RestSpec restSpec) throws IOException, RestException { public RestClient(RestSpec restSpec, Settings settings, InetSocketAddress[] addresses) throws IOException, RestException {
assert addresses.length > 0; assert addresses.length > 0;
this.restSpec = restSpec; this.restSpec = restSpec;
this.headers = new Headers(settings);
this.httpClient = createHttpClient(); this.httpClient = createHttpClient();
this.addresses = addresses; this.addresses = addresses;
this.esVersion = readAndCheckVersion(); this.esVersion = readAndCheckVersion();
@ -70,7 +74,7 @@ public class RestClient implements Closeable {
String version = null; String version = null;
for (InetSocketAddress address : addresses) { for (InetSocketAddress address : addresses) {
RestResponse restResponse = new RestResponse(new HttpRequestBuilder(httpClient) RestResponse restResponse = new RestResponse(new HttpRequestBuilder(httpClient).addHeaders(headers)
.host(address.getHostName()).port(address.getPort()) .host(address.getHostName()).port(address.getPort())
.path(restApi.getPaths().get(0)) .path(restApi.getPaths().get(0))
.method(restApi.getMethods().get(0)).execute()); .method(restApi.getMethods().get(0)).execute());
@ -219,7 +223,7 @@ public class RestClient implements Closeable {
protected HttpRequestBuilder httpRequestBuilder() { protected HttpRequestBuilder httpRequestBuilder() {
//the address used is randomized between the available ones //the address used is randomized between the available ones
InetSocketAddress address = RandomizedTest.randomFrom(addresses); InetSocketAddress address = RandomizedTest.randomFrom(addresses);
return new HttpRequestBuilder(httpClient).host(address.getHostName()).port(address.getPort()); return new HttpRequestBuilder(httpClient).addHeaders(headers).host(address.getHostName()).port(address.getPort());
} }
protected CloseableHttpClient createHttpClient() { protected CloseableHttpClient createHttpClient() {

View File

@ -23,6 +23,7 @@ import com.google.common.collect.Maps;
import org.apache.http.client.methods.*; import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
@ -92,6 +93,13 @@ public class HttpRequestBuilder {
return this; return this;
} }
public HttpRequestBuilder addHeaders(Headers headers) {
for (String header : headers.headers().names()) {
this.headers.put(header, headers.headers().get(header));
}
return this;
}
public HttpRequestBuilder addHeader(String name, String value) { public HttpRequestBuilder addHeader(String name, String value) {
this.headers.put(name, value); this.headers.put(name, value);
return this; return this;