diff --git a/test/framework/src/main/java/org/opensearch/test/rest/yaml/ClientYamlTestClient.java b/test/framework/src/main/java/org/opensearch/test/rest/yaml/ClientYamlTestClient.java index 13ede9d44f1ad..849d7e4685a76 100644 --- a/test/framework/src/main/java/org/opensearch/test/rest/yaml/ClientYamlTestClient.java +++ b/test/framework/src/main/java/org/opensearch/test/rest/yaml/ClientYamlTestClient.java @@ -59,6 +59,8 @@ import java.io.UncheckedIOException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -193,7 +195,7 @@ public ClientYamlTestResponse callApi( String contentType = entity.getContentType(); // randomly test the GET with source param instead of GET/POST with body try { - if (sendBodyAsSourceParam(supportedMethods, contentType, entity.getContentLength())) { + if (sendBodyAsSourceParam(supportedMethods, contentType, entity)) { logger.debug("sending the request body as source param with GET method"); queryStringParams.put("source", EntityUtils.toString(entity)); queryStringParams.put("source_content_type", contentType); @@ -253,11 +255,13 @@ protected static void setOptions(Request request, Map headers) { request.setOptions(options); } - private static boolean sendBodyAsSourceParam(List supportedMethods, String contentType, long contentLength) { + private static boolean sendBodyAsSourceParam(List supportedMethods, String contentType, HttpEntity entity) throws IOException, + ParseException { if (false == supportedMethods.contains(HttpGet.METHOD_NAME)) { // The API doesn't claim to support GET anyway return false; } + long contentLength = entity.getContentLength(); if (contentLength < 0) { // Negative length means "unknown" or "huge" in this case. Either way we can't send it as a parameter return false; @@ -271,7 +275,18 @@ private static boolean sendBodyAsSourceParam(List supportedMethods, Stri // We can only encode JSON or YAML this way. return false; } - return RandomizedTest.rarely(); + + return RandomizedTest.rarely() && isUrlEncodedLengthUnderLimit(entity); + } + + /* + * There is a limit of 4096 bytes for the HTTP line, otherwise there will be too_long_http_line_exception. + * We check if the length of the url-encoded source parameter is less than 3000, leaving remaining for + * url and other params. + */ + private static boolean isUrlEncodedLengthUnderLimit(HttpEntity entity) throws IOException, ParseException { + String encoded = URLEncoder.encode(EntityUtils.toString(entity), StandardCharsets.UTF_8); + return encoded.length() < 3000; } private ClientYamlSuiteRestApi restApi(String apiName) {