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

convert to form format for testing #1472

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 21 additions & 12 deletions libs/dao/src/main/java/com/akto/util/HttpRequestResponseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public static String convertFormUrlEncodedToJson(String rawRequest) {
}

public static String jsonToFormUrlEncoded(String requestPayload) {
return jsonToFormUrlEncoded(requestPayload, true);
}
public static String jsonToFormUrlEncoded(String requestPayload, boolean replaceSpecialChars) {
JSONObject jsonObject = new JSONObject(requestPayload);

StringBuilder formUrlEncoded = new StringBuilder();
Expand All @@ -126,9 +129,9 @@ public static String jsonToFormUrlEncoded(String requestPayload) {
for (String key : jsonObject.keySet()) {
// Encode the key and value, and append them to the string builder
try {
formUrlEncoded.append(encode(key))
formUrlEncoded.append(encode(key, replaceSpecialChars))
.append("=")
.append(encode(jsonObject.getString(key)))
.append(encode(jsonObject.getString(key), replaceSpecialChars))
.append("&");
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -142,16 +145,22 @@ public static String jsonToFormUrlEncoded(String requestPayload) {

return formUrlEncoded.toString();
}
public static String encode(String s) throws UnsupportedEncodingException {
return URLEncoder.encode(s, java.nio.charset.StandardCharsets.UTF_8.name())
.replaceAll("\\+", "%20")
.replaceAll("\\%21", "!")
.replaceAll("\\%27", "'")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\%7E", "~")
.replaceAll("\\%5B", "[")
.replaceAll("\\%5D", "]");
public static String encode(String s, boolean replaceSpecialChars) throws UnsupportedEncodingException {
String ret = URLEncoder.encode(s, java.nio.charset.StandardCharsets.UTF_8.name());

if (replaceSpecialChars) {
ret = ret.replaceAll("\\+", "%20")
.replaceAll("\\%21", "!")
.replaceAll("\\%27", "'")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\%7E", "~")
.replaceAll("\\%5B", "[")
.replaceAll("\\%5D", "]");

}

return ret;
}

}
5 changes: 5 additions & 0 deletions libs/utils/src/main/java/com/akto/testing/ApiExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ private static OriginalHttpResponse sendWithRequestBody(OriginalHttpRequest requ
if (payload == null) payload = "{}";
payload = payload.trim();
if (!payload.startsWith("[") && !payload.startsWith("{")) payload = "{}";
} else if (contentType.contains(HttpRequestResponseUtils.FORM_URL_ENCODED_CONTENT_TYPE)) {
if (payload.startsWith("{")) {
payload = HttpRequestResponseUtils.jsonToFormUrlEncoded(payload, false);
}

} else if (contentType.contains(HttpRequestResponseUtils.GRPC_CONTENT_TYPE)) {
try {
loggerMaker.infoAndAddToDb("encoding to grpc payload:" + payload, LogDb.TESTING);
Expand Down
Loading