Skip to content

Commit

Permalink
Fix test failing due to too long http line exception (#5278)
Browse files Browse the repository at this point in the history
The `sendBodyAsSourceParam` method determines whether it should send the body as query parameter. It used to check the content length of the entity which in some cases (e.g. regex expression) is much shorter than the encoded-url and may result in a TooLongHttpLineException.

This commit makes an additional check on the length of the url-encoded string. To avoid unnecessary computation it is only checked when the RandomizedTest.rarely() returns true.

Signed-off-by: Rabi Panda <adnapibar@gmail.com>
  • Loading branch information
adnapibar authored Nov 16, 2022
1 parent 9fc3f40 commit 9d4aac2
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -253,11 +255,13 @@ protected static void setOptions(Request request, Map<String, String> headers) {
request.setOptions(options);
}

private static boolean sendBodyAsSourceParam(List<String> supportedMethods, String contentType, long contentLength) {
private static boolean sendBodyAsSourceParam(List<String> 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;
Expand All @@ -271,7 +275,18 @@ private static boolean sendBodyAsSourceParam(List<String> 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) {
Expand Down

0 comments on commit 9d4aac2

Please sign in to comment.