Tests: Add test for templatable URLs, that are URL encoded

This is just a workaround at the moment, but allows to use
mustache if you only provide the `url` part of a request,
instead of scheme, port, path, host, etc.

Original commit: elastic/x-pack-elasticsearch@3a4aa26665
This commit is contained in:
Alexander Reelsen 2017-06-26 18:24:51 +02:00
parent 806b0bc710
commit 0912a85778
2 changed files with 15 additions and 20 deletions

View File

@ -6,7 +6,6 @@
package org.elasticsearch.xpack.common.http;
import io.netty.handler.codec.http.HttpHeaders;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
@ -167,30 +166,26 @@ public class HttpRequestTemplateTests extends ESTestCase {
}
public void testParsingEmptyUrl() throws Exception {
try {
HttpRequestTemplate.builder().fromUrl("");
fail("Expected exception due to empty URL");
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), containsString("Configured URL is empty, please configure a valid URL"));
}
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> HttpRequestTemplate.builder().fromUrl(""));
assertThat(e.getMessage(), containsString("Configured URL is empty, please configure a valid URL"));
}
public void testInvalidUrlsWithMissingScheme() throws Exception {
try {
HttpRequestTemplate.builder().fromUrl("www.test.de");
fail("Expected exception due to missing scheme");
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), containsString("URL [www.test.de] does not contain a scheme"));
}
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class,
() -> HttpRequestTemplate.builder().fromUrl("www.test.de"));
assertThat(e.getMessage(), containsString("URL [www.test.de] does not contain a scheme"));
}
public void testInvalidUrlsWithHost() throws Exception {
try {
HttpRequestTemplate.builder().fromUrl("https://");
fail("Expected exception due to missing host");
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), containsString("Malformed URL [https://]"));
}
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class,
() -> HttpRequestTemplate.builder().fromUrl("https://"));
assertThat(e.getMessage(), containsString("Malformed URL [https://]"));
}
public void testThatPartsFromUrlAreTemplatable() throws Exception {
HttpRequestTemplate template = HttpRequestTemplate.builder().fromUrl("http://www.test.de/%7B%7Bfoo%7D%7D").build();
HttpRequest request = template.render(new MockTextTemplateEngine(), emptyMap());
assertThat(request.path(), is("/{{foo}}"));
}
private void assertThatManualBuilderEqualsParsingFromUrl(String url, HttpRequestTemplate.Builder builder) throws Exception {

View File

@ -271,7 +271,7 @@ public class HttpInputTests extends ESTestCase {
}
public void testThatExpectedContentTypeOverridesReturnedContentType() throws Exception {
HttpRequestTemplate template = HttpRequestTemplate.builder("localhost", 9200).fromUrl("http:://127.0.0.1:12345").build();
HttpRequestTemplate template = HttpRequestTemplate.builder("http:://127.0.0.1:12345").build();
HttpInput httpInput = new HttpInput(template, HttpContentType.TEXT, null);
ExecutableHttpInput input = new ExecutableHttpInput(httpInput, logger, httpClient, templateEngine);