Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test failing due to too long http line exception #5278

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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