From 31ecf1db98ba9822ca5eccd23dc3a09a06e91818 Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 31 Mar 2014 12:35:50 +0200 Subject: [PATCH] [TEST] Test sending the request body as source parameter with GET method through REST tests For around 10% of the calls that support the GET method, the request body gets now sent as `source` parameter with GET method (no randomized method in this case). This way we can find bugs like #5556 (missing support for source param). --- .../test/rest/client/RestClient.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/elasticsearch/test/rest/client/RestClient.java b/src/test/java/org/elasticsearch/test/rest/client/RestClient.java index d038b1407f9..e1e916b24fc 100644 --- a/src/test/java/org/elasticsearch/test/rest/client/RestClient.java +++ b/src/test/java/org/elasticsearch/test/rest/client/RestClient.java @@ -177,17 +177,6 @@ public class RestClient implements Closeable { HttpRequestBuilder httpRequestBuilder = httpRequestBuilder(); - if (Strings.hasLength(body)) { - if (!restApi.isBodySupported()) { - throw new IllegalArgumentException("body is not supported by [" + restApi.getName() + "] api"); - } - httpRequestBuilder.body(body); - } else { - if (restApi.isBodyRequired()) { - throw new IllegalArgumentException("body is required by [" + restApi.getName() + "] api"); - } - } - //divide params between ones that go within query string and ones that go within path Map pathParts = Maps.newHashMap(); if (params != null) { @@ -207,9 +196,27 @@ public class RestClient implements Closeable { httpRequestBuilder.addParam("op_type", "create"); } + List supportedMethods = restApi.getSupportedMethods(pathParts.keySet()); + if (Strings.hasLength(body)) { + if (!restApi.isBodySupported()) { + throw new IllegalArgumentException("body is not supported by [" + restApi.getName() + "] api"); + } + //test the GET with source param instead of GET/POST with body + if (supportedMethods.contains("GET") && RandomizedTest.rarely()) { + logger.debug("sending the request body as source param with GET method"); + httpRequestBuilder.addParam("source", body).method("GET"); + } else { + httpRequestBuilder.body(body).method(RandomizedTest.randomFrom(supportedMethods)); + } + } else { + if (restApi.isBodyRequired()) { + throw new IllegalArgumentException("body is required by [" + restApi.getName() + "] api"); + } + httpRequestBuilder.method(RandomizedTest.randomFrom(supportedMethods)); + } + //the http method is randomized (out of the available ones with the chosen api) - return httpRequestBuilder.method(RandomizedTest.randomFrom(restApi.getSupportedMethods(pathParts.keySet()))) - .path(RandomizedTest.randomFrom(restApi.getFinalPaths(pathParts))); + return httpRequestBuilder.path(RandomizedTest.randomFrom(restApi.getFinalPaths(pathParts))); } private RestApi restApi(String apiName) {