diff --git a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java index a33f445c15f..1edb5b114fb 100755 --- a/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java +++ b/chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java @@ -346,6 +346,10 @@ public static byte[] getOwner(Transaction.Contract contract) { } } return owner.toByteArray(); + } catch (InvalidProtocolBufferException invalidProtocolBufferException) { + logger.warn("InvalidProtocolBufferException occurred because {}, please verify the interface " + + "input parameters", invalidProtocolBufferException.getMessage()); + return new byte[0]; } catch (Exception ex) { logger.error(ex.getMessage()); return new byte[0]; diff --git a/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java b/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java index 7757ecd8722..01ec00dbef3 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetAssetIssueListServlet.java @@ -1,5 +1,8 @@ package org.tron.core.services.http; +import static org.tron.core.services.http.Util.existVisible; + +import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -8,7 +11,6 @@ import org.tron.api.GrpcAPI.AssetIssueList; import org.tron.core.Wallet; - @Component @Slf4j(topic = "API") public class GetAssetIssueListServlet extends RateLimiterServlet { @@ -16,21 +18,36 @@ public class GetAssetIssueListServlet extends RateLimiterServlet { @Autowired private Wallet wallet; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); - AssetIssueList reply = wallet.getAssetIssueList(); - if (reply != null) { - response.getWriter().println(JsonFormat.printToString(reply, visible)); - } else { - response.getWriter().println("{}"); - } + response(response, visible); } catch (Exception e) { Util.processError(e, response); } } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - doGet(request, response); + try { + PostParams params = PostParams.getPostParams(request); + boolean visible = Util.getVisible(request); + if (!existVisible(request)) { + visible = params.isVisible(); + } + response(response, visible); + } catch (Exception e) { + Util.processError(e, response); + } + } + + private void response(HttpServletResponse response, boolean visible) throws IOException { + AssetIssueList reply = wallet.getAssetIssueList(); + if (reply != null) { + response.getWriter().println(JsonFormat.printToString(reply, visible)); + } else { + response.getWriter().println("{}"); + } } } diff --git a/framework/src/main/java/org/tron/core/services/http/GetAvailableUnfreezeCountServlet.java b/framework/src/main/java/org/tron/core/services/http/GetAvailableUnfreezeCountServlet.java index 28fec9f3ee3..51f78fc4390 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetAvailableUnfreezeCountServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetAvailableUnfreezeCountServlet.java @@ -23,6 +23,9 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); String ownerAddress = request.getParameter("ownerAddress"); + if (ownerAddress == null) { + ownerAddress = request.getParameter("owner_address"); + } if (visible) { ownerAddress = Util.getHexAddress(ownerAddress); } diff --git a/framework/src/main/java/org/tron/core/services/http/GetBlockByNumServlet.java b/framework/src/main/java/org/tron/core/services/http/GetBlockByNumServlet.java index 44babc69551..800b421ace0 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetBlockByNumServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetBlockByNumServlet.java @@ -18,14 +18,21 @@ public class GetBlockByNumServlet extends RateLimiterServlet { @Autowired private Wallet wallet; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { - fillResponse(Util.getVisible(request), Long.parseLong(request.getParameter("num")), response); + long num = 0; + String numStr = request.getParameter("num"); + if (numStr != null) { + num = Long.parseLong(numStr); + } + fillResponse(Util.getVisible(request), num, response); } catch (Exception e) { Util.processError(e, response); } } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { try { PostParams params = PostParams.getPostParams(request); diff --git a/framework/src/main/java/org/tron/core/services/http/GetCanDelegatedMaxSizeServlet.java b/framework/src/main/java/org/tron/core/services/http/GetCanDelegatedMaxSizeServlet.java index 81dd52400b5..924306a6a3f 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetCanDelegatedMaxSizeServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetCanDelegatedMaxSizeServlet.java @@ -18,10 +18,15 @@ public class GetCanDelegatedMaxSizeServlet extends RateLimiterServlet { @Autowired private Wallet wallet; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); - int type = Integer.parseInt(request.getParameter("type")); + int type = 0; + String typeStr = request.getParameter("type"); + if (typeStr != null) { + type = Integer.parseInt(typeStr); + } String ownerAddress = request.getParameter("owner_address"); if (visible) { ownerAddress = Util.getHexAddress(ownerAddress); @@ -34,6 +39,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) { } } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { try { PostParams params = PostParams.getPostParams(request); diff --git a/framework/src/main/java/org/tron/core/services/http/GetCanWithdrawUnfreezeAmountServlet.java b/framework/src/main/java/org/tron/core/services/http/GetCanWithdrawUnfreezeAmountServlet.java index 4c1bbaf4f99..435cca9e5fb 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetCanWithdrawUnfreezeAmountServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetCanWithdrawUnfreezeAmountServlet.java @@ -18,23 +18,29 @@ public class GetCanWithdrawUnfreezeAmountServlet extends RateLimiterServlet { @Autowired private Wallet wallet; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); String ownerAddress = request.getParameter("owner_address"); - long timestamp = Long.valueOf(request.getParameter("timestamp")); + long timestamp = 0; + String timestampStr = request.getParameter("timestamp"); + if (timestampStr != null) { + timestamp = Long.parseLong(timestampStr); + } if (visible) { ownerAddress = Util.getHexAddress(ownerAddress); } fillResponse(visible, - ByteString.copyFrom(ByteArray.fromHexString(ownerAddress)), - timestamp, - response); + ByteString.copyFrom(ByteArray.fromHexString(ownerAddress)), + timestamp, + response); } catch (Exception e) { Util.processError(e, response); } } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { try { PostParams params = PostParams.getPostParams(request); diff --git a/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java b/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java index f7d9700aeb9..78e7178cfe3 100644 --- a/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/GetNowBlockServlet.java @@ -1,5 +1,9 @@ package org.tron.core.services.http; +import static org.tron.core.services.http.Util.existVisible; +import static org.tron.core.services.http.Util.getVisible; + +import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -8,7 +12,6 @@ import org.tron.core.Wallet; import org.tron.protos.Protocol.Block; - @Component @Slf4j(topic = "API") public class GetNowBlockServlet extends RateLimiterServlet { @@ -16,21 +19,36 @@ public class GetNowBlockServlet extends RateLimiterServlet { @Autowired private Wallet wallet; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { - boolean visible = Util.getVisible(request); - Block reply = wallet.getNowBlock(); - if (reply != null) { - response.getWriter().println(Util.printBlock(reply, visible)); - } else { - response.getWriter().println("{}"); - } + boolean visible = getVisible(request); + response(response, visible); } catch (Exception e) { Util.processError(e, response); } } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - doGet(request, response); + try { + PostParams params = PostParams.getPostParams(request); + boolean visible = getVisible(request); + if (!existVisible(request)) { + visible = params.isVisible(); + } + response(response, visible); + } catch (Exception e) { + Util.processError(e, response); + } + } + + private void response(HttpServletResponse response, boolean visible) throws IOException { + Block reply = wallet.getNowBlock(); + if (reply != null) { + response.getWriter().println(Util.printBlock(reply, visible)); + } else { + response.getWriter().println("{}"); + } } } \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java b/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java index 94fd568a3f4..fb0b877832b 100644 --- a/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/ListNodesServlet.java @@ -1,5 +1,8 @@ package org.tron.core.services.http; +import static org.tron.core.services.http.Util.existVisible; + +import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -8,7 +11,6 @@ import org.tron.api.GrpcAPI.NodeList; import org.tron.core.Wallet; - @Component @Slf4j(topic = "API") public class ListNodesServlet extends RateLimiterServlet { @@ -16,21 +18,36 @@ public class ListNodesServlet extends RateLimiterServlet { @Autowired private Wallet wallet; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); - NodeList reply = wallet.listNodes(); - if (reply != null) { - response.getWriter().println(JsonFormat.printToString(reply, visible)); - } else { - response.getWriter().println("{}"); - } + response(response, visible); } catch (Exception e) { Util.processError(e, response); } } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - doGet(request, response); + try { + PostParams params = PostParams.getPostParams(request); + boolean visible = Util.getVisible(request); + if (!existVisible(request)) { + visible = params.isVisible(); + } + response(response, visible); + } catch (Exception e) { + Util.processError(e, response); + } + } + + private void response(HttpServletResponse response, boolean visible) throws IOException { + NodeList reply = wallet.listNodes(); + if (reply != null) { + response.getWriter().println(JsonFormat.printToString(reply, visible)); + } else { + response.getWriter().println("{}"); + } } } diff --git a/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java b/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java index f8c67395b1b..e309963edda 100644 --- a/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/ListProposalsServlet.java @@ -1,5 +1,8 @@ package org.tron.core.services.http; +import static org.tron.core.services.http.Util.existVisible; + +import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -8,7 +11,6 @@ import org.tron.api.GrpcAPI.ProposalList; import org.tron.core.Wallet; - @Component @Slf4j(topic = "API") public class ListProposalsServlet extends RateLimiterServlet { @@ -16,21 +18,36 @@ public class ListProposalsServlet extends RateLimiterServlet { @Autowired private Wallet wallet; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { boolean visible = Util.getVisible(request); - ProposalList reply = wallet.getProposalList(); - if (reply != null) { - response.getWriter().println(JsonFormat.printToString(reply, visible)); - } else { - response.getWriter().println("{}"); - } + response(response, visible); } catch (Exception e) { Util.processError(e, response); } } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { - doGet(request, response); + try { + PostParams params = PostParams.getPostParams(request); + boolean visible = Util.getVisible(request); + if (!existVisible(request)) { + visible = params.isVisible(); + } + response(response, visible); + } catch (Exception e) { + Util.processError(e, response); + } + } + + private void response(HttpServletResponse response, boolean visible) throws IOException { + ProposalList reply = wallet.getProposalList(); + if (reply != null) { + response.getWriter().println(JsonFormat.printToString(reply, visible)); + } else { + response.getWriter().println("{}"); + } } } diff --git a/framework/src/main/java/org/tron/core/services/http/PostParams.java b/framework/src/main/java/org/tron/core/services/http/PostParams.java index a2077fb3c78..7dcb0be6ae3 100644 --- a/framework/src/main/java/org/tron/core/services/http/PostParams.java +++ b/framework/src/main/java/org/tron/core/services/http/PostParams.java @@ -1,5 +1,8 @@ package org.tron.core.services.http; +import static org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED; +import static org.tron.core.services.http.Util.getJsonString; + import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import lombok.Getter; @@ -21,6 +24,9 @@ public PostParams(String params, boolean visible) { public static PostParams getPostParams(HttpServletRequest request) throws Exception { String input = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); Util.checkBodySize(input); + if (APPLICATION_FORM_URLENCODED.getMimeType().equals(request.getContentType())) { + input = getJsonString(input); + } boolean visible = Util.getVisiblePost(input); return new PostParams(input, visible); } diff --git a/framework/src/main/java/org/tron/core/services/http/Util.java b/framework/src/main/java/org/tron/core/services/http/Util.java index 06c4f8ef26a..186f77cf3e8 100644 --- a/framework/src/main/java/org/tron/core/services/http/Util.java +++ b/framework/src/main/java/org/tron/core/services/http/Util.java @@ -1,5 +1,6 @@ package org.tron.core.services.http; +import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.tron.common.utils.Commons.decodeFromBase58Check; import com.alibaba.fastjson.JSON; @@ -21,13 +22,17 @@ import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Objects; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.bouncycastle.util.encoders.Hex; import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.util.MultiMap; import org.eclipse.jetty.util.StringUtil; +import org.eclipse.jetty.util.UrlEncoded; import org.tron.api.GrpcAPI; import org.tron.api.GrpcAPI.BlockList; import org.tron.api.GrpcAPI.TransactionApprovedList; @@ -338,12 +343,16 @@ public static boolean getVisible(final HttpServletRequest request) { return visible; } + public static boolean existVisible(final HttpServletRequest request) { + return Objects.nonNull(request.getParameter(VISIBLE)); + } + public static boolean getVisiblePost(final String input) { boolean visible = false; if (StringUtil.isNotBlank(input)) { JSONObject jsonObject = JSON.parseObject(input); if (jsonObject.containsKey(VISIBLE)) { - visible = jsonObject.getBoolean(VISIBLE); + visible = Boolean.parseBoolean(jsonObject.getString(VISIBLE)); } } @@ -606,4 +615,23 @@ public static void validateParameter(String contract) throws InvalidParameterExc } } + public static String getJsonString(String str) { + if (StringUtils.isEmpty(str)) { + return EMPTY; + } + MultiMap params = new MultiMap<>(); + UrlEncoded.decodeUtf8To(str, params); + JSONObject json = new JSONObject(); + for (Map.Entry> entry : params.entrySet()) { + String key = entry.getKey(); + List values = entry.getValue(); + if (values.size() == 1) { + json.put(key, values.get(0)); + } else { + json.put(key, values); + } + } + return json.toString(); + } + } diff --git a/framework/src/test/java/org/tron/core/services/http/GetAssetIssueListServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetAssetIssueListServletTest.java new file mode 100644 index 00000000000..141c349e252 --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/http/GetAssetIssueListServletTest.java @@ -0,0 +1,83 @@ +package org.tron.core.services.http; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.fail; + +import com.alibaba.fastjson.JSONObject; +import java.io.UnsupportedEncodingException; +import javax.annotation.Resource; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.config.args.Args; + +public class GetAssetIssueListServletTest extends BaseTest { + + @Resource + private GetAssetIssueListServlet getAssetIssueListServlet; + + static { + dbPath = "db_GetNowBlockServlet_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, Constant.TEST_CONF + ); + } + + public MockHttpServletRequest createRequest(String contentType) { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setMethod("POST"); + request.setContentType(contentType); + request.setCharacterEncoding("UTF-8"); + return request; + } + + @Test + public void testGetAssetIssueListByJson() { + String jsonParam = "{\"visible\": true}"; + MockHttpServletRequest request = createRequest("application/json"); + request.setContent(jsonParam.getBytes()); + MockHttpServletResponse response = new MockHttpServletResponse(); + getAssetIssueListServlet.doPost(request, response); + try { + String contentAsString = response.getContentAsString(); + JSONObject.parseObject(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + + @Test + public void testGetAssetIssueListValue() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + try { + String params = "visible=true"; + request.setContent(params.getBytes(UTF_8)); + MockHttpServletResponse response = new MockHttpServletResponse(); + getAssetIssueListServlet.doPost(request, response); + String contentAsString = response.getContentAsString(); + JSONObject.parseObject(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + + @Test + public void testGetAssetIssueListEmptyParam() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + String params = "visible="; + request.setContent(params.getBytes(UTF_8)); + MockHttpServletResponse response = new MockHttpServletResponse(); + getAssetIssueListServlet.doPost(request, response); + try { + String contentAsString = response.getContentAsString(); + JSONObject.parseObject(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + +} diff --git a/framework/src/test/java/org/tron/core/services/http/GetNowBlockServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetNowBlockServletTest.java new file mode 100644 index 00000000000..682f780b249 --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/http/GetNowBlockServletTest.java @@ -0,0 +1,86 @@ +package org.tron.core.services.http; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.alibaba.fastjson.JSONObject; +import java.io.UnsupportedEncodingException; +import javax.annotation.Resource; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.config.args.Args; + +public class GetNowBlockServletTest extends BaseTest { + + @Resource + private GetNowBlockServlet getNowBlockServlet; + + static { + dbPath = "db_GetNowBlockServlet_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, Constant.TEST_CONF + ); + } + + public MockHttpServletRequest createRequest(String contentType) { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setMethod("POST"); + request.setContentType(contentType); + request.setCharacterEncoding("UTF-8"); + return request; + } + + @Test + public void testGetNowBlockByJson() { + String jsonParam = "{\"visible\": true}"; + MockHttpServletRequest request = createRequest("application/json"); + 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 testGetNowBlockValue() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + try { + String params = "visible=true"; + request.setContent(params.getBytes(UTF_8)); + MockHttpServletResponse response = new MockHttpServletResponse(); + getNowBlockServlet.doPost(request, response); + String contentAsString = response.getContentAsString(); + JSONObject result = JSONObject.parseObject(contentAsString); + assertTrue(result.containsKey("blockID")); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + + @Test + public void testGetNowBlockEmptyParam() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + String params = "visible="; + request.setContent(params.getBytes(UTF_8)); + 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()); + } + } +} diff --git a/framework/src/test/java/org/tron/core/services/http/ListNodesServletTest.java b/framework/src/test/java/org/tron/core/services/http/ListNodesServletTest.java new file mode 100644 index 00000000000..e2b8e7e9b6e --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/http/ListNodesServletTest.java @@ -0,0 +1,83 @@ +package org.tron.core.services.http; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.UnsupportedEncodingException; +import javax.annotation.Resource; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.config.args.Args; + +public class ListNodesServletTest extends BaseTest { + + @Resource + private ListNodesServlet listNodesServlet; + + static { + dbPath = "db_GetNowBlockServlet_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, Constant.TEST_CONF + ); + } + + public MockHttpServletRequest createRequest(String contentType) { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setMethod("POST"); + request.setContentType(contentType); + request.setCharacterEncoding("UTF-8"); + return request; + } + + @Test + public void testListNodesByJson() { + String jsonParam = "{\"visible\": true}"; + MockHttpServletRequest request = createRequest("application/json"); + request.setContent(jsonParam.getBytes()); + MockHttpServletResponse response = new MockHttpServletResponse(); + listNodesServlet.doPost(request, response); + try { + String contentAsString = response.getContentAsString(); + assertNotNull(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + + @Test + public void testListNodesValue() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + try { + String params = "visible=true"; + request.setContent(params.getBytes(UTF_8)); + MockHttpServletResponse response = new MockHttpServletResponse(); + listNodesServlet.doPost(request, response); + String contentAsString = response.getContentAsString(); + assertNotNull(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + + @Test + public void testListNodesEmptyParam() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + String params = "visible="; + request.setContent(params.getBytes(UTF_8)); + MockHttpServletResponse response = new MockHttpServletResponse(); + listNodesServlet.doPost(request, response); + try { + String contentAsString = response.getContentAsString(); + assertNotNull(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + +} diff --git a/framework/src/test/java/org/tron/core/services/http/ListProposalsServletTest.java b/framework/src/test/java/org/tron/core/services/http/ListProposalsServletTest.java new file mode 100644 index 00000000000..0879d02004b --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/http/ListProposalsServletTest.java @@ -0,0 +1,83 @@ +package org.tron.core.services.http; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.fail; + +import com.alibaba.fastjson.JSONObject; +import java.io.UnsupportedEncodingException; +import javax.annotation.Resource; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.tron.common.BaseTest; +import org.tron.core.Constant; +import org.tron.core.config.args.Args; + +public class ListProposalsServletTest extends BaseTest { + + @Resource + private ListProposalsServlet listProposalsServlet; + + static { + dbPath = "db_GetNowBlockServlet_test"; + Args.setParam( + new String[]{ + "--output-directory", dbPath, + }, Constant.TEST_CONF + ); + } + + public MockHttpServletRequest createRequest(String contentType) { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setMethod("POST"); + request.setContentType(contentType); + request.setCharacterEncoding("UTF-8"); + return request; + } + + @Test + public void testListProposalsByJson() { + String jsonParam = "{\"visible\": true}"; + MockHttpServletRequest request = createRequest("application/json"); + request.setContent(jsonParam.getBytes()); + MockHttpServletResponse response = new MockHttpServletResponse(); + listProposalsServlet.doPost(request, response); + try { + String contentAsString = response.getContentAsString(); + JSONObject.parseObject(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + + @Test + public void testListProposalsValue() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + try { + String params = "visible=true"; + request.setContent(params.getBytes(UTF_8)); + MockHttpServletResponse response = new MockHttpServletResponse(); + listProposalsServlet.doPost(request, response); + String contentAsString = response.getContentAsString(); + JSONObject.parseObject(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + + @Test + public void testListProposalsEmptyParam() { + MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); + String params = "visible="; + request.setContent(params.getBytes(UTF_8)); + MockHttpServletResponse response = new MockHttpServletResponse(); + listProposalsServlet.doPost(request, response); + try { + String contentAsString = response.getContentAsString(); + JSONObject.parseObject(contentAsString); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + } + +}