[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:
parent
04aaedc083
commit
ca858befab
|
@ -22,15 +22,15 @@ import com.carrotsearch.randomizedtesting.RandomizedTest;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.apache.http.message.BasicHeader;
|
import org.apache.http.message.BasicHeader;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.client.Response;
|
import org.elasticsearch.client.Response;
|
||||||
import org.elasticsearch.client.ResponseException;
|
import org.elasticsearch.client.ResponseException;
|
||||||
import org.elasticsearch.client.RestClient;
|
import org.elasticsearch.client.RestClient;
|
||||||
import org.elasticsearch.common.Strings;
|
|
||||||
import org.elasticsearch.common.logging.Loggers;
|
import org.elasticsearch.common.logging.Loggers;
|
||||||
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi;
|
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi;
|
||||||
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestPath;
|
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestPath;
|
||||||
|
@ -52,6 +52,8 @@ import java.util.Objects;
|
||||||
public class ClientYamlTestClient {
|
public class ClientYamlTestClient {
|
||||||
private static final Logger logger = Loggers.getLogger(ClientYamlTestClient.class);
|
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 ClientYamlSuiteRestSpec restSpec;
|
||||||
private final RestClient restClient;
|
private final RestClient restClient;
|
||||||
private final Version esVersion;
|
private final Version esVersion;
|
||||||
|
@ -71,7 +73,7 @@ public class ClientYamlTestClient {
|
||||||
/**
|
/**
|
||||||
* Calls an api with the provided parameters and body
|
* 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 {
|
throws IOException {
|
||||||
|
|
||||||
if ("raw".equals(apiName)) {
|
if ("raw".equals(apiName)) {
|
||||||
|
@ -79,10 +81,6 @@ public class ClientYamlTestClient {
|
||||||
Map<String, String> queryStringParams = new HashMap<>(params);
|
Map<String, String> queryStringParams = new HashMap<>(params);
|
||||||
String method = Objects.requireNonNull(queryStringParams.remove("method"), "Method must be set to use raw request");
|
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");
|
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!
|
// And everything else is a url parameter!
|
||||||
try {
|
try {
|
||||||
Response response = restClient.performRequest(method, path, queryStringParams, entity);
|
Response response = restClient.performRequest(method, path, queryStringParams, entity);
|
||||||
|
@ -113,20 +111,20 @@ public class ClientYamlTestClient {
|
||||||
|
|
||||||
List<String> supportedMethods = restApi.getSupportedMethods(pathParts.keySet());
|
List<String> supportedMethods = restApi.getSupportedMethods(pathParts.keySet());
|
||||||
String requestMethod;
|
String requestMethod;
|
||||||
StringEntity requestBody = null;
|
if (entity != null) {
|
||||||
if (Strings.hasLength(body)) {
|
|
||||||
if (!restApi.isBodySupported()) {
|
if (!restApi.isBodySupported()) {
|
||||||
throw new IllegalArgumentException("body is not supported by [" + restApi.getName() + "] api");
|
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
|
//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");
|
logger.debug("sending the request body as source param with GET method");
|
||||||
queryStringParams.put("source", body);
|
queryStringParams.put("source", EntityUtils.toString(entity));
|
||||||
queryStringParams.put("source_content_type", ContentType.APPLICATION_JSON.toString());
|
queryStringParams.put("source_content_type", contentType);
|
||||||
requestMethod = "GET";
|
requestMethod = HttpGet.METHOD_NAME;
|
||||||
|
entity = null;
|
||||||
} else {
|
} else {
|
||||||
requestMethod = RandomizedTest.randomFrom(supportedMethods);
|
requestMethod = RandomizedTest.randomFrom(supportedMethods);
|
||||||
requestBody = new StringEntity(body, ContentType.APPLICATION_JSON);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (restApi.isBodyRequired()) {
|
if (restApi.isBodyRequired()) {
|
||||||
|
@ -168,13 +166,23 @@ public class ClientYamlTestClient {
|
||||||
|
|
||||||
logger.debug("calling api [{}]", apiName);
|
logger.debug("calling api [{}]", apiName);
|
||||||
try {
|
try {
|
||||||
Response response = restClient.performRequest(requestMethod, requestPath, queryStringParams, requestBody, requestHeaders);
|
Response response = restClient.performRequest(requestMethod, requestPath, queryStringParams, entity, requestHeaders);
|
||||||
return new ClientYamlTestResponse(response);
|
return new ClientYamlTestResponse(response);
|
||||||
} catch(ResponseException e) {
|
} catch(ResponseException e) {
|
||||||
throw new ClientYamlTestResponseException(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) {
|
private ClientYamlSuiteRestApi restApi(String apiName) {
|
||||||
ClientYamlSuiteRestApi restApi = restSpec.getApi(apiName);
|
ClientYamlSuiteRestApi restApi = restSpec.getApi(apiName);
|
||||||
if (restApi == null) {
|
if (restApi == null) {
|
||||||
|
|
|
@ -18,6 +18,9 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.test.rest.yaml;
|
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.apache.logging.log4j.Logger;
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.common.logging.Loggers;
|
import org.elasticsearch.common.logging.Loggers;
|
||||||
|
@ -62,9 +65,9 @@ public class ClientYamlTestExecutionContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String body = actualBody(bodies);
|
HttpEntity entity = createEntity(bodies);
|
||||||
try {
|
try {
|
||||||
response = callApiInternal(apiName, requestParams, body, headers);
|
response = callApiInternal(apiName, requestParams, entity, headers);
|
||||||
return response;
|
return response;
|
||||||
} catch(ClientYamlTestResponseException e) {
|
} catch(ClientYamlTestResponseException e) {
|
||||||
response = e.getRestTestResponse();
|
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()) {
|
if (bodies.isEmpty()) {
|
||||||
return "";
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bodies.size() == 1) {
|
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();
|
StringBuilder bodyBuilder = new StringBuilder();
|
||||||
for (Map<String, Object> body : bodies) {
|
for (Map<String, Object> body : bodies) {
|
||||||
bodyBuilder.append(bodyAsString(stash.replaceStashedValues(body))).append("\n");
|
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 {
|
private String bodyAsString(Map<String, Object> body) throws IOException {
|
||||||
return XContentFactory.jsonBuilder().map(body).string();
|
return XContentFactory.jsonBuilder().map(body).string();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClientYamlTestResponse callApiInternal(String apiName, Map<String, String> params, String body, Map<String, String> headers)
|
private ClientYamlTestResponse callApiInternal(String apiName, Map<String, String> params,
|
||||||
throws IOException {
|
HttpEntity entity, Map<String, String> headers) throws IOException {
|
||||||
return clientYamlTestClient.callApi(apiName, params, body, headers);
|
return clientYamlTestClient.callApi(apiName, params, entity, headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue