|
@@ -19,7 +19,6 @@
|
|
|
|
|
|
package org.elasticsearch.client;
|
|
|
|
|
|
-import com.carrotsearch.randomizedtesting.generators.RandomInts;
|
|
|
import org.apache.http.Header;
|
|
|
import org.apache.http.HttpHost;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
@@ -28,8 +27,10 @@ import org.apache.http.message.BasicHeader;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
+import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
+import static org.junit.Assert.assertThat;
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
public class RestClientBuilderTests extends RestClientTestCase {
|
|
@@ -38,8 +39,8 @@ public class RestClientBuilderTests extends RestClientTestCase {
|
|
|
try {
|
|
|
RestClient.builder((HttpHost[])null);
|
|
|
fail("should have failed");
|
|
|
- } catch(IllegalArgumentException e) {
|
|
|
- assertEquals("no hosts provided", e.getMessage());
|
|
|
+ } catch(NullPointerException e) {
|
|
|
+ assertEquals("hosts must not be null", e.getMessage());
|
|
|
}
|
|
|
|
|
|
try {
|
|
@@ -62,7 +63,7 @@ public class RestClientBuilderTests extends RestClientTestCase {
|
|
|
|
|
|
try {
|
|
|
RestClient.builder(new HttpHost("localhost", 9200))
|
|
|
- .setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
|
|
|
+ .setMaxRetryTimeoutMillis(randomIntBetween(Integer.MIN_VALUE, 0));
|
|
|
fail("should have failed");
|
|
|
} catch(IllegalArgumentException e) {
|
|
|
assertEquals("maxRetryTimeoutMillis must be greater than 0", e.getMessage());
|
|
@@ -103,13 +104,13 @@ public class RestClientBuilderTests extends RestClientTestCase {
|
|
|
assertEquals("requestConfigCallback must not be null", e.getMessage());
|
|
|
}
|
|
|
|
|
|
- int numNodes = RandomInts.randomIntBetween(getRandom(), 1, 5);
|
|
|
+ int numNodes = randomIntBetween(1, 5);
|
|
|
HttpHost[] hosts = new HttpHost[numNodes];
|
|
|
for (int i = 0; i < numNodes; i++) {
|
|
|
hosts[i] = new HttpHost("localhost", 9200 + i);
|
|
|
}
|
|
|
RestClientBuilder builder = RestClient.builder(hosts);
|
|
|
- if (getRandom().nextBoolean()) {
|
|
|
+ if (randomBoolean()) {
|
|
|
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
|
|
|
@Override
|
|
|
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
|
|
@@ -117,7 +118,7 @@ public class RestClientBuilderTests extends RestClientTestCase {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
- if (getRandom().nextBoolean()) {
|
|
|
+ if (randomBoolean()) {
|
|
|
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
|
|
|
@Override
|
|
|
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
|
|
@@ -125,19 +126,55 @@ public class RestClientBuilderTests extends RestClientTestCase {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
- if (getRandom().nextBoolean()) {
|
|
|
- int numHeaders = RandomInts.randomIntBetween(getRandom(), 1, 5);
|
|
|
+ if (randomBoolean()) {
|
|
|
+ int numHeaders = randomIntBetween(1, 5);
|
|
|
Header[] headers = new Header[numHeaders];
|
|
|
for (int i = 0; i < numHeaders; i++) {
|
|
|
headers[i] = new BasicHeader("header" + i, "value");
|
|
|
}
|
|
|
builder.setDefaultHeaders(headers);
|
|
|
}
|
|
|
- if (getRandom().nextBoolean()) {
|
|
|
- builder.setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
|
|
|
+ if (randomBoolean()) {
|
|
|
+ builder.setMaxRetryTimeoutMillis(randomIntBetween(1, Integer.MAX_VALUE));
|
|
|
+ }
|
|
|
+ if (randomBoolean()) {
|
|
|
+ String pathPrefix = (randomBoolean() ? "/" : "") + randomAsciiOfLengthBetween(2, 5);
|
|
|
+ while (pathPrefix.length() < 20 && randomBoolean()) {
|
|
|
+ pathPrefix += "/" + randomAsciiOfLengthBetween(3, 6);
|
|
|
+ }
|
|
|
+ builder.setPathPrefix(pathPrefix + (randomBoolean() ? "/" : ""));
|
|
|
}
|
|
|
try (RestClient restClient = builder.build()) {
|
|
|
assertNotNull(restClient);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void testSetPathPrefixNull() {
|
|
|
+ try {
|
|
|
+ RestClient.builder(new HttpHost("localhost", 9200)).setPathPrefix(null);
|
|
|
+ fail("pathPrefix set to null should fail!");
|
|
|
+ } catch (final NullPointerException e) {
|
|
|
+ assertEquals("pathPrefix must not be null", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testSetPathPrefixEmpty() {
|
|
|
+ assertSetPathPrefixThrows("/");
|
|
|
+ assertSetPathPrefixThrows("");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testSetPathPrefixMalformed() {
|
|
|
+ assertSetPathPrefixThrows("//");
|
|
|
+ assertSetPathPrefixThrows("base/path//");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void assertSetPathPrefixThrows(final String pathPrefix) {
|
|
|
+ try {
|
|
|
+ RestClient.builder(new HttpHost("localhost", 9200)).setPathPrefix(pathPrefix);
|
|
|
+ fail("path prefix [" + pathPrefix + "] should have failed");
|
|
|
+ } catch (final IllegalArgumentException e) {
|
|
|
+ assertThat(e.getMessage(), containsString(pathPrefix));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|