[TEST] create HttpEntity earlier in REST tests

This allows to set content-type together with the body itself. At the moment it is always json, but this change allows makes it easier to randomize it later
This commit is contained in:
javanna 2017-02-11 08:33:19 +01:00 committed by Luca Cavanna
parent 04aaedc083
commit ca858befab
2 changed files with 36 additions and 26 deletions

View File

@ -22,15 +22,15 @@ import com.carrotsearch.randomizedtesting.RandomizedTest;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.Version;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi;
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestPath;
@ -52,6 +52,8 @@ import java.util.Objects;
public class ClientYamlTestClient {
private static final Logger logger = Loggers.getLogger(ClientYamlTestClient.class);
private static final ContentType YAML_CONTENT_TYPE = ContentType.create("application/yaml");
private final ClientYamlSuiteRestSpec restSpec;
private final RestClient restClient;
private final Version esVersion;
@ -71,7 +73,7 @@ public class ClientYamlTestClient {
/**
* Calls an api with the provided parameters and body
*/
public ClientYamlTestResponse callApi(String apiName, Map<String, String> params, String body, Map<String, String> headers)
public ClientYamlTestResponse callApi(String apiName, Map<String, String> params, HttpEntity entity, Map<String, String> headers)
throws IOException {
if ("raw".equals(apiName)) {
@ -79,10 +81,6 @@ public class ClientYamlTestClient {
Map<String, String> queryStringParams = new HashMap<>(params);
String method = Objects.requireNonNull(queryStringParams.remove("method"), "Method must be set to use raw request");
String path = "/"+ Objects.requireNonNull(queryStringParams.remove("path"), "Path must be set to use raw request");
HttpEntity entity = null;
if (body != null && body.length() > 0) {
entity = new StringEntity(body, ContentType.APPLICATION_JSON);
}
// And everything else is a url parameter!
try {
Response response = restClient.performRequest(method, path, queryStringParams, entity);
@ -113,20 +111,20 @@ public class ClientYamlTestClient {
List<String> supportedMethods = restApi.getSupportedMethods(pathParts.keySet());
String requestMethod;
StringEntity requestBody = null;
if (Strings.hasLength(body)) {
if (entity != null) {
if (!restApi.isBodySupported()) {
throw new IllegalArgumentException("body is not supported by [" + restApi.getName() + "] api");
}
String contentType = entity.getContentType().getValue();
//randomly test the GET with source param instead of GET/POST with body
if (supportedMethods.contains("GET") && RandomizedTest.rarely()) {
if (sendBodyAsSourceParam(supportedMethods, contentType)) {
logger.debug("sending the request body as source param with GET method");
queryStringParams.put("source", body);
queryStringParams.put("source_content_type", ContentType.APPLICATION_JSON.toString());
requestMethod = "GET";
queryStringParams.put("source", EntityUtils.toString(entity));
queryStringParams.put("source_content_type", contentType);
requestMethod = HttpGet.METHOD_NAME;
entity = null;
} else {
requestMethod = RandomizedTest.randomFrom(supportedMethods);
requestBody = new StringEntity(body, ContentType.APPLICATION_JSON);
}
} else {
if (restApi.isBodyRequired()) {
@ -168,13 +166,23 @@ public class ClientYamlTestClient {
logger.debug("calling api [{}]", apiName);
try {
Response response = restClient.performRequest(requestMethod, requestPath, queryStringParams, requestBody, requestHeaders);
Response response = restClient.performRequest(requestMethod, requestPath, queryStringParams, entity, requestHeaders);
return new ClientYamlTestResponse(response);
} catch(ResponseException e) {
throw new ClientYamlTestResponseException(e);
}
}
private static boolean sendBodyAsSourceParam(List<String> supportedMethods, String contentType) {
if (supportedMethods.contains(HttpGet.METHOD_NAME)) {
if (contentType.startsWith(ContentType.APPLICATION_JSON.getMimeType()) ||
contentType.startsWith(YAML_CONTENT_TYPE.getMimeType())) {
return RandomizedTest.rarely();
}
}
return false;
}
private ClientYamlSuiteRestApi restApi(String apiName) {
ClientYamlSuiteRestApi restApi = restSpec.getApi(apiName);
if (restApi == null) {

View File

@ -18,6 +18,9 @@
*/
package org.elasticsearch.test.rest.yaml;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.Version;
import org.elasticsearch.common.logging.Loggers;
@ -62,9 +65,9 @@ public class ClientYamlTestExecutionContext {
}
}
String body = actualBody(bodies);
HttpEntity entity = createEntity(bodies);
try {
response = callApiInternal(apiName, requestParams, body, headers);
response = callApiInternal(apiName, requestParams, entity, headers);
return response;
} catch(ClientYamlTestResponseException e) {
response = e.getRestTestResponse();
@ -77,29 +80,28 @@ public class ClientYamlTestExecutionContext {
}
}
private String actualBody(List<Map<String, Object>> bodies) throws IOException {
private HttpEntity createEntity(List<Map<String, Object>> bodies) throws IOException {
if (bodies.isEmpty()) {
return "";
return null;
}
if (bodies.size() == 1) {
return bodyAsString(stash.replaceStashedValues(bodies.get(0)));
String bodyAsString = bodyAsString(stash.replaceStashedValues(bodies.get(0)));
return new StringEntity(bodyAsString, ContentType.APPLICATION_JSON);
}
StringBuilder bodyBuilder = new StringBuilder();
for (Map<String, Object> body : bodies) {
bodyBuilder.append(bodyAsString(stash.replaceStashedValues(body))).append("\n");
}
return bodyBuilder.toString();
return new StringEntity(bodyBuilder.toString(), ContentType.APPLICATION_JSON);
}
private String bodyAsString(Map<String, Object> body) throws IOException {
return XContentFactory.jsonBuilder().map(body).string();
}
private ClientYamlTestResponse callApiInternal(String apiName, Map<String, String> params, String body, Map<String, String> headers)
throws IOException {
return clientYamlTestClient.callApi(apiName, params, body, headers);
private ClientYamlTestResponse callApiInternal(String apiName, Map<String, String> params,
HttpEntity entity, Map<String, String> headers) throws IOException {
return clientYamlTestClient.callApi(apiName, params, entity, headers);
}
/**