From 0912a8577855d5e90eb7cf2a431915dcfe8b7c85 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Mon, 26 Jun 2017 18:24:51 +0200 Subject: [PATCH] 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@3a4aa26665ad0cc32614a6e82597e2894c1c36c5 --- .../common/http/HttpRequestTemplateTests.java | 33 ++++++++----------- .../watcher/input/http/HttpInputTests.java | 2 +- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/plugin/src/test/java/org/elasticsearch/xpack/common/http/HttpRequestTemplateTests.java b/plugin/src/test/java/org/elasticsearch/xpack/common/http/HttpRequestTemplateTests.java index 860ff54231a..f2556db71d5 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/common/http/HttpRequestTemplateTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/common/http/HttpRequestTemplateTests.java @@ -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 { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java index c2d1b663534..a53e023649c 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java @@ -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);