Skip to content

Commit

Permalink
[TEST] Expose ability to provide http headers when sending requests i…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
javanna committed Sep 15, 2014
1 parent af34c08 commit 6dbca6f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/elasticsearch/client/support/Headers.java
Expand Up @@ -53,9 +53,12 @@ public <M extends TransportMessage<?>> M applyTo(M message) {
return message;
}

public Settings headers() {
return headers;
}

static Settings resolveHeaders(Settings settings) {
Settings headers = settings.getAsSettings(PREFIX);
return headers != null ? headers : ImmutableSettings.EMPTY;
}

}
Expand Up @@ -24,6 +24,8 @@
import com.google.common.collect.Lists;
import org.elasticsearch.Version;
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.ClusterScope;
import org.elasticsearch.test.rest.client.RestException;
Expand Down Expand Up @@ -173,6 +175,13 @@ public static void close() {
restTestExecutionContext = null;
}

/**
* Used to obtain settings for the REST client that is used to send REST requests.
*/
protected Settings restClientSettings() {
return ImmutableSettings.EMPTY;
}

@Before
public void reset() throws IOException, RestException {
//skip test if it matches one of the blacklist globs
Expand All @@ -183,7 +192,7 @@ public void reset() throws IOException, RestException {
assumeFalse("[" + testCandidate.getTestPath() + "] skipped, reason: blacklisted", blacklistedPathMatcher.matches(Paths.get(testPath)));
}

restTestExecutionContext.resetClient(cluster().httpAddresses());
restTestExecutionContext.resetClient(cluster().httpAddresses(), restClientSettings());
restTestExecutionContext.clear();

//skip test if the whole suite (yaml file) is disabled
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.Maps;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.rest.client.RestClient;
import org.elasticsearch.test.rest.client.RestException;
Expand Down Expand Up @@ -115,11 +116,11 @@ public Object response(String path) throws IOException {
}

/**
* 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) {
restClient = new RestClient(addresses, restSpec);
restClient = new RestClient(restSpec, settings, addresses);
} else {
restClient.updateAddresses(addresses);
}
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/org/elasticsearch/test/rest/client/RestClient.java
Expand Up @@ -23,9 +23,11 @@
import com.google.common.collect.Maps;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.ESLogger;
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.HttpResponse;
import org.elasticsearch.test.rest.spec.RestApi;
Expand All @@ -47,14 +49,16 @@ public class RestClient implements Closeable {

private final RestSpec restSpec;
private final CloseableHttpClient httpClient;
private final Headers headers;

private InetSocketAddress[] addresses;

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;
this.restSpec = restSpec;
this.headers = new Headers(settings);
this.httpClient = createHttpClient();
this.addresses = addresses;
this.esVersion = readAndCheckVersion();
Expand All @@ -70,7 +74,7 @@ private String readAndCheckVersion() throws IOException, RestException {

String version = null;
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())
.path(restApi.getPaths().get(0))
.method(restApi.getMethods().get(0)).execute());
Expand Down Expand Up @@ -219,7 +223,7 @@ private RestApi restApi(String apiName) {
protected HttpRequestBuilder httpRequestBuilder() {
//the address used is randomized between the available ones
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() {
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
Expand Down Expand Up @@ -92,6 +93,13 @@ public HttpRequestBuilder addParam(String name, String value) {
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) {
this.headers.put(name, value);
return this;
Expand Down

0 comments on commit 6dbca6f

Please sign in to comment.