diff --git a/rest-api-spec/test/script/30_expressions.yaml b/rest-api-spec/test/script/30_expressions.yaml index 46ba090cd77..a0953a25972 100644 --- a/rest-api-spec/test/script/30_expressions.yaml +++ b/rest-api-spec/test/script/30_expressions.yaml @@ -22,6 +22,6 @@ setup: --- "Expressions scripting test": - - do: { search: { body: { script_fields : { my_field : { lang: expression, script: 'doc["age"].value' } } } } } - - match: { hits.hits.0.fields.my_field.0: 23.0 } + - do: { search: { body: { script_fields : { my_field : { lang: expression, script: 'doc["age"].value + 19' } } } } } + - match: { hits.hits.0.fields.my_field.0: 42.0 } diff --git a/src/test/java/org/elasticsearch/test/rest/client/http/HttpRequestBuilder.java b/src/test/java/org/elasticsearch/test/rest/client/http/HttpRequestBuilder.java index 797a78db178..09f79a0fc7c 100644 --- a/src/test/java/org/elasticsearch/test/rest/client/http/HttpRequestBuilder.java +++ b/src/test/java/org/elasticsearch/test/rest/client/http/HttpRequestBuilder.java @@ -31,8 +31,10 @@ import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.http.HttpServerTransport; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLEncoder; import java.nio.charset.Charset; import java.util.Map; @@ -89,8 +91,13 @@ public class HttpRequestBuilder { } public HttpRequestBuilder addParam(String name, String value) { - this.params.put(name, value); - return this; + try { + //manually url encode params, since URI does it only partially (e.g. '+' stays as is) + this.params.put(name, URLEncoder.encode(value, "utf-8")); + return this; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } } public HttpRequestBuilder addHeaders(Headers headers) { @@ -173,16 +180,18 @@ public class HttpRequestBuilder { } private URI buildUri() { - String query; - if (params.size() == 0) { - query = null; - } else { - query = Joiner.on('&').withKeyValueSeparator("=").join(params); - } try { - return new URI(protocol, null, host, port, path, query, null); - } catch (URISyntaxException e) { - throw new IllegalArgumentException(e); + //url encode rules for path and query params are different. We use URI to encode the path, but we manually encode each query param through URLEncoder. + URI uri = new URI(protocol, null, host, port, path, null, null); + //String concatenation FTW. If we use the nicer multi argument URI constructor query parameters will get only partially encoded + //(e.g. '+' will stay as is) hence when trying to properly encode params manually they will end up double encoded (+ becomes %252B instead of %2B). + StringBuilder uriBuilder = new StringBuilder(protocol).append("://").append(host).append(":").append(port).append(uri.getRawPath()); + if (params.size() > 0) { + uriBuilder.append("?").append(Joiner.on('&').withKeyValueSeparator("=").join(params)); + } + return URI.create(uriBuilder.toString()); + } catch(URISyntaxException e) { + throw new IllegalArgumentException("unable to build uri", e); } }