Skip to content

Commit

Permalink
Merge pull request #5504 from lxcmyf/release_v4.7.3
Browse files Browse the repository at this point in the history
fix(api): optimize API call compatibility
  • Loading branch information
jwrct authored Sep 20, 2023
2 parents 52283dc + 0bacdaa commit 3e6eb02
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions framework/src/main/java/org/tron/core/services/http/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ public static String getJsonString(String str) {
if (StringUtils.isEmpty(str)) {
return EMPTY;
}
if (isValidJson(str)) {
return str;
}
MultiMap<String> params = new MultiMap<>();
UrlEncoded.decodeUtf8To(str, params);
JSONObject json = new JSONObject();
Expand All @@ -631,4 +634,12 @@ public static String getJsonString(String str) {
return json.toString();
}

public static boolean isValidJson(String json) {
try {
JSON.parse(json);
return true;
} catch (Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.tron.core.services.http;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -30,7 +33,9 @@ public class GetNowBlockServletTest extends BaseTest {
public MockHttpServletRequest createRequest(String contentType) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType(contentType);
if (isNotEmpty(contentType)) {
request.setContentType(contentType);
}
request.setCharacterEncoding("UTF-8");
return request;
}
Expand All @@ -51,6 +56,37 @@ public void testGetNowBlockByJson() {
}
}

@Test
public void testGetNowBlockByJson2() {
String jsonParam = "{\"visible\": true}";
MockHttpServletRequest request = createRequest(APPLICATION_FORM_URLENCODED.getMimeType());
request.setContent(jsonParam.getBytes());
MockHttpServletResponse response = new MockHttpServletResponse();
getNowBlockServlet.doPost(request, response);
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
assertTrue(result.containsKey("blockID"));
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}
}

@Test
public void testGetNowBlockByEmpty() {
MockHttpServletRequest request = createRequest(APPLICATION_FORM_URLENCODED.getMimeType());
request.setContent(EMPTY.getBytes());
MockHttpServletResponse response = new MockHttpServletResponse();
getNowBlockServlet.doPost(request, response);
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
assertTrue(result.containsKey("blockID"));
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}
}

@Test
public void testGetNowBlockValue() {
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
Expand Down

0 comments on commit 3e6eb02

Please sign in to comment.