Make sure that in SitePluginTests http client connects to the correct node and closes the node after the test

This commit is contained in:
Igor Motov 2013-02-19 14:42:24 -05:00
parent f96c1f1e10
commit 5b9e9a004a
2 changed files with 45 additions and 11 deletions

View File

@ -19,12 +19,15 @@
package org.elasticsearch.test.integration.plugin;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.http.HttpServerTransport;
import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.elasticsearch.test.integration.rest.helper.HttpClient;
import org.elasticsearch.test.integration.rest.helper.HttpClientResponse;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
@ -37,23 +40,38 @@ import static org.hamcrest.Matchers.equalTo;
*/
public class SitePluginTests extends AbstractNodesTests {
@BeforeClass
public void setupPluginDirectory() {
putDefaultSettings(settingsBuilder()
.put("path.plugins", "target/test-classes/org/elasticsearch/test/integration/plugin/")
.build());
}
@BeforeMethod
public void startNodes() {
startNode("test");
}
@AfterMethod
public void closeNodes() {
closeAllNodes();
}
public HttpClient httpClient(String id) {
HttpServerTransport httpServerTransport = ((InternalNode) node(id)).injector().getInstance(HttpServerTransport.class);
return new HttpClient(httpServerTransport.boundAddress().publishAddress());
}
@Test
public void testRedirectSitePlugin() throws Exception {
Settings settings = settingsBuilder()
.put("path.plugins", "target/test-classes/org/elasticsearch/test/integration/plugin/")
.build();
InternalNode node = (InternalNode) buildNode("test", settings);
node.start();
// We use an HTTP Client to test redirection
HttpClientResponse response = new HttpClient("http://localhost:9200").request("/_plugin/dummy");
HttpClientResponse response = httpClient("test").request("/_plugin/dummy");
assertThat(response.errorCode(), equalTo(RestStatus.MOVED_PERMANENTLY.getStatus()));
assertThat(response.response(), containsString("/_plugin/dummy/"));
// We test the real URL
response = new HttpClient("http://localhost:9200").request("/_plugin/dummy/");
response = httpClient("test").request("/_plugin/dummy/");
assertThat(response.errorCode(), equalTo(RestStatus.OK.getStatus()));
assertThat(response.response(), containsString("<title>Dummy Site Plugin</title>"));
}

View File

@ -20,11 +20,14 @@ package org.elasticsearch.test.integration.rest.helper;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
@ -32,6 +35,15 @@ public class HttpClient {
private final URL baseUrl;
public HttpClient(TransportAddress transportAddress) {
InetSocketAddress address = ((InetSocketTransportAddress) transportAddress).address();
try {
baseUrl = new URL("http", address.getHostName(), address.getPort(), "/");
} catch (MalformedURLException e) {
throw new ElasticSearchException("", e);
}
}
public HttpClient(String url) {
try {
baseUrl = new URL(url);
@ -40,6 +52,10 @@ public class HttpClient {
}
}
public HttpClient(URL url) {
baseUrl = url;
}
public HttpClientResponse request(String path) {
return request("GET", path);
}